Skip to content

Commit

Permalink
Fix monetized_attributes class discrepancy
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Neilos committed Nov 27, 2023
1 parent 5fac874 commit 42a5024
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/money-rails/active_record/monetizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 42a5024

Please sign in to comment.