Skip to content

Commit 79df005

Browse files
authored
Merge pull request #706 from edithemmings/main
Monetizable: Skip subunit write for non-attributes in getter
2 parents 777c565 + ae6d096 commit 79df005

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Fix `money_only_cents` for negative money
99
- Allow `nil` to be set as the default currency
1010
- Portuguese translation for errors
11+
- Skip subunit write for non-attributes in `read_monetized`
1112

1213
## 1.15.0
1314

lib/money-rails/active_record/monetizable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def read_monetized(name, subunit_name, options = nil, *args, **kwargs)
205205
result = memoized
206206
else
207207
memoized_amount = memoized.amount.to_money(attr_currency)
208-
write_attribute subunit_name, memoized_amount.cents
208+
write_attribute subunit_name, memoized_amount.cents if has_attribute? subunit_name
209209
# Cache the value (it may be nil)
210210
result = instance_variable_set("@#{name}", memoized_amount)
211211
end

spec/active_record/monetizable_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ def assert_monetized_attributes(monetized_attributes, expected_attributes)
103103
expect(transaction.amount_cents).to eq(20000)
104104
end
105105

106+
it "update to instance currency field gets applied to converted methods" do
107+
transaction = Transaction.create(amount: '200', tax: '10', currency: 'USD')
108+
expect(transaction.total).to eq(Money.new(21000, 'USD'))
109+
110+
transaction.currency = 'CLP'
111+
expect(transaction.total).to eq(Money.new(210, 'CLP'))
112+
end
113+
106114
it "raises an error if trying to create two attributes with the same name" do
107115
expect do
108116
class Product

0 commit comments

Comments
 (0)