-
Notifications
You must be signed in to change notification settings - Fork 336
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
Update trackable association to optional #369
base: main
Are you sure you want to change the base?
Conversation
502c624
to
a84401f
Compare
@smridge I’m in 2 minds about this change. I totally get your point about changing As the current behavior to rely on app wide settings has been in place for a while accepting this PR would warrant a major version release IMO. However, I don’t think that’d be a good default in the first place. I’d be keen to learn more about your use case though as not setting Long term, I think that #154 would be a better solution overall, I just haven’t had the time to rebase that longstanding PR yet. Thoughts welcome. |
I definitely agree it's a Separately, for my specific use case, I was not able to override/monkey patch for the life of me. Here are the things I tried: attempt one # lib/public_activity/orm/active_record/activity.rb
module PublicActivity
module ORM
module ActiveRecord
class Activity < ::ActiveRecord::Base
belongs_to :trackable, polymorphic: true, optional: true
end
end
end
end attempt two # app/models/activity.rb (model already in app)
class Activity < PublicActivity::Activity
belongs_to :trackable, polymorphic: true, optional: true
...
end attempt three # config/initializers/public_activity.rb
PublicActivity::Activity.class_eval do
belongs_to :trackable, polymorphic: true, optional: true
end I also tried a |
Neither. The 3rd option is “depending on your app configuration” which is a valid option IMO.
What you want to happen in this case will be application specific.
Point taken.
I haven’t looked into their history for making this choice so I’m not qualified to comment, yet.
#248 mentions a working solution similar to yours. Personally, I use the 3rd variant in one of the apps I maintain. Here’s the code copied verbatim from said app: # frozen_string_literal: true
PublicActivity::Activity.class_eval do
belongs_to :owner,
-> { unscope(where: :active) },
polymorphic: true,
optional: true
def parameter_for(*args)
parameters
.with_indifferent_access
.fetch(*args)
.then { |value| value.is_a?(Array) ? value[1] : value }
end
def updated_attributes elements = %w[updated_at]
parameters.without(*elements)
end
end Do you use the spring gem by any chance? |
Currently, this gem does not make it explicitly clear whether the
trackable
association should be required or not. When upgrading a Rails application and start enabling new defaults one by one, theRails.application.config.active_record.belongs_to_required_by_default = true
completely breaks without this change. It's difficult to know when a codebase suddenly decides to start enabling new configurations. For example, you can be on Rails 7 and have this set to false or have it not enabled if yourconfig/application.rb
still has framework defaults set to say Rails5.0
.Overall, by the gem not having this specified, the intent is questionable and I'd say it's a
BREAKING CHANGE
when upgrading your application/enabling different configurations. This PR makes the intent clear and the changes more backwards compatible. I understand it can also break people that want it required. However, the gem should express explicit intent.