From 42a50242d730dd08ac8dbd0ce73fe84d1a99b3cb Mon Sep 17 00:00:00 2001 From: Neilos Date: Mon, 27 Nov 2023 16:31:16 +0000 Subject: [PATCH] Fix monetized_attributes class discrepancy Suppose we have a model ```ruby class Investment < ActiveRecord::Base monetize :value monetize :discounted_value end ``` and a subclass ```ruby class BadInvestment < Investment end ``` When we check the monetized_attributes of both the `Product` and `SpecialProduct` we get seemingly the same result: ```ruby Investment.monetized_attributes # => { value: value_cents, discounted_value: discounted_value_cents, } BadInvestment.monetized_attributes # => { value: value_cents, discounted_value: discounted_value_cents, } ``` ...but when we check the class of the `monetized_attributes` we can see that one is a `ActiveSupport::HashWithIndifferentAccess` while the other is a `Hash`. This commit fixes the discrepancy, ensuring both are a `ActiveSupport::HashWithIndifferentAccess`. --- lib/money-rails/active_record/monetizable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/money-rails/active_record/monetizable.rb b/lib/money-rails/active_record/monetizable.rb index 8bebfc5b62..0e429e5847 100644 --- a/lib/money-rails/active_record/monetizable.rb +++ b/lib/money-rails/active_record/monetizable.rb @@ -10,7 +10,7 @@ class ReadOnlyCurrencyException < MoneyRails::Error; end module ClassMethods def monetized_attributes - monetized_attributes = @monetized_attributes || {} + monetized_attributes = @monetized_attributes || {}.with_indifferent_access if superclass.respond_to?(:monetized_attributes) monetized_attributes.merge(superclass.monetized_attributes)