Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix `money_only_cents` for negative money
- Allow `nil` to be set as the default currency
- Portuguese translation for errors
- Skip subunit write for non-attributes in `read_monetized`

## 1.15.0

Expand Down
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 @@ -103,6 +103,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