-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Associated Objects to extend their Active Record #19
Conversation
@seanpdoyle have you tried using this gem yet? Would this be something you'd be interested in? |
I'm not sure if I'd use that feature or not, but it makes sense. Also, I think this feature or not, we should eager load (when eager loading is enabled) AssociatedObject and ActiveJob Performs classes. If they aren't eager loaded in production, we'd end up using more memory than necessary. |
Also, I don't think this would cause me any problems. I've assumed they work like models where they're eager loaded in production and lazy loaded in development. If not, I think we should change that. |
I think I'd have to tinker with it and see how it feels, but just looking at it, it sure seems like a good approach. |
@garrettdimon nice! There's also no rush on this, so take your time. I wonder how we explain it to people, since it could be a bit hidden. I could see an |
@natematykiewicz we're just leaning on
Yeah, that's how it works before this PR. However, if we shift to loading the Associated Object when Basically, using the example from the description, before this PR it'd be common to do something like this: # app/models/post.rb
class Post < ApplicationRecord
include Published
end
# app/models/post/published.rb
module Post::Published
extend ActiveSupport::Concern
included do
has_many :contracts, dependent: :destroy
has_object :publisher
after_create_commit :publish_later_after_signed
end
private def publish_later_after_signed
publisher.publish_later if contracts.all?(&:signed?)
end
end So we're splitting the extension/integration into a wrapping Concern, but what if we could bundle that into the Associated Object now that they're a domain concept and we're already using them? It gets tricky though, especially communicating where things come from. |
I realized yesterday that I'd probably move the callbacks inside the # model
has_object :foo, after_create_commit: :sync_later
after_update_commit -> { foo.sync_later }, if: :saved_change_to_active? But with extensions I'd probably just switch that to: # model
has_object :foo
# associated object
extension do
after_create_commit -> { foo.sync_later }
after_update_commit -> { foo.sync_later }, if: :saved_change_to_active?
end Ship it, Kasper! |
@natematykiewicz nice! Sounds good, I think I'll ship this after we get #20 in then, and cut a new release. |
b17a486
to
853768a
Compare
Kredis now expects its connector to respond to `root`, which is a subtle breaking change when its assigned to something other than the default `Rails.application`.
@kaspth in your rebase, you lost the eager loading of the associated object class. |
PR kaspth#19 previously loaded the associated object class when the has_object was defined, so that extensions would get run. It seems like that code got lost in a rebase. So now, extensions only run on boot if eager loading is enabled (which it is in production). Otherwise they aren't loaded until the first time the associated object gets used.
PR #19 previously loaded the associated object class when the has_object was defined, so that extensions would get run. It seems like that code got lost in a rebase. So now, extensions only run on boot if eager loading is enabled (which it is in production). Otherwise they aren't loaded until the first time the associated object gets used.
If we eager load the Associated Object class when calling
has_object
we can have anextension
class method fire that can extend the Active Record class.I'm still not quite sure how I feel about this. I'll write some documentation later, after I've sat with this some more.
@garrettdimon this doesn't quite get us to the automatically scoped validations, but is this closer to what you were talking about with replacing those wrapping
ActiveSupport::Concern
s ala kaspth/conventional_extensions?@natematykiewicz is this something that would be useful to you? Does this seem clear enough? Would it break your app if we eager-loaded the Associated Objects (due to autoloading it should be fine)?