Skip to content

Commit

Permalink
Skip subunit write for non-attributes on currency change in getter
Browse files Browse the repository at this point in the history
  • Loading branch information
edithemmings committed Oct 12, 2024
1 parent df7a030 commit b923185
Show file tree
Hide file tree
Showing 2 changed files with 9 additions 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 @@ -205,7 +205,7 @@ def read_monetized(name, subunit_name, options = nil, *args, **kwargs)
result = memoized
else
memoized_amount = memoized.amount.to_money(attr_currency)
write_attribute subunit_name, memoized_amount.cents
write_attribute subunit_name, memoized_amount.cents if has_attribute? subunit_name
# Cache the value (it may be nil)
result = instance_variable_set("@#{name}", memoized_amount)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/active_record/monetizable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def assert_monetized_attributes(monetized_attributes, expected_attributes)
expect(transaction.amount_cents).to eq(20000)
end

it "update to instance currency field gets applied to converted methods" do
transaction = Transaction.create(amount: '200', tax: '10', currency: 'USD')
expect(transaction.total).to eq(Money.new(21000, 'USD'))

transaction.currency = 'CLP'
expect(transaction.total).to eq(Money.new(210, 'CLP'))
end

it "raises an error if trying to create two attributes with the same name" do
expect do
class Product
Expand Down

0 comments on commit b923185

Please sign in to comment.