Update trackable association to optional#369
Update trackable association to optional#369smridge wants to merge 1 commit intopublic-activity:mainfrom
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
endattempt two # app/models/activity.rb (model already in app)
class Activity < PublicActivity::Activity
belongs_to :trackable, polymorphic: true, optional: true
...
endattempt three # config/initializers/public_activity.rb
PublicActivity::Activity.class_eval do
belongs_to :trackable, polymorphic: true, optional: true
endI 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
endDo you use the spring gem by any chance? |
Currently, this gem does not make it explicitly clear whether the
trackableassociation 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 = truecompletely 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.rbstill 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 CHANGEwhen 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.