Fix preferences columns to be always serialized#3997
Fix preferences columns to be always serialized#3997mamhoff wants to merge 0 commit intosolidusio:masterfrom
Conversation
Since rails/rails@7b39197 serialezable hash attributes are not allocated by default anymore if not used.
553c700 to
670c2a5
Compare
|
Thanks Martin, one question: does this require to add serialize :preferences, Hash
after_initialize :initialize_preference_defaultsexplicitly to any custom model defined in host applications and extensions that has preferences? |
|
@kennyadsl Yes, that is true. I could add a changelog entry and/or raise an exception when it's not defined. |
|
Can you please provide an example of the code broken by this change? It's still not super clear to me and would help to evaluate if this needed to be backported. |
|
All of our calculators that do not have preferences (they're custom code) return |
|
So, if you added a custom shipping calculator with no preferences, theses two lines would definitely break if |
|
I have a failing spec for the estimator when there's a shipping calculator without preferences # core/spec/models/spree/stock/estimator_spec.rb:96
context 'with a custom shipping calculator with no preference' do
class Spree::Calculator::Shipping::NoPreferences < Spree::ShippingCalculator
def compute_package(_package)
# no op
end
end
let!(:shipping_methods) do
[
create(:shipping_method, calculator: Spree::Calculator::Shipping::NoPreferences.new)
]
end
it 'does not raise an error' do
expect { subject.shipping_rates(package) }.not_to raise_error
end
endI think the best approach here is by finding a way to let |
|
I should have a fix that doesn't involve reverting that commit, or introduce a breaking change. If that works well, I think we can still keep this PR for Solidus 3, I like that it's more explicit and can remove the hacky code I'm going to propose. I'd love to have a period when this is optional though and introduces a deprecation warning when the serialization is not explicit. |
| end | ||
|
|
||
| def preference(name, type, options = {}) | ||
| if Object.const_defined?("Spree::Base") && self < Spree::Base && !self.new.preferences.is_a?(Hash) |
There was a problem hiding this comment.
Style/RedundantSelf: Redundant self detected.
670c2a5 to
4d1cef6
Compare
4d1cef6 to
39a9d7d
Compare
Description
This fixes an issue that arose because of commit b947015, which
initialized the serialization of the preference column only if the
respective subclass had any preferences defined. This is not always the
case, and previous versions of Solidus would always allow a subclass of
e.g.
Spree::Calculatorto access theirpreferencesattribute as aHash.
The intention of the commit in question was to speed up initialization
for models that do not have a preferences column. Instead of complex
metamagic, this moves the initialization of the preferences column and
preference defaults to those classes that actually needed, actually
yielding MORE performance benefits, as
serializeand theafter_initializehook are only called once, not once per preferencebeing defined.
Checklist: