Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monetizable: Skip subunit write for non-attributes in getter #706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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