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

Fix money_only_cents for negative money #688

Merged
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 @@ -3,6 +3,7 @@
## Unreleased

- Allow monetizing methods with kwargs
- Fix money_only_cents for negative money

## 1.15.0

Expand Down
2 changes: 1 addition & 1 deletion lib/money-rails/helpers/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def money_only_cents(value)

value = value.to_money

format "%0#{value.currency.exponent}d", (value % value.currency.subunit_to_unit).cents
format "%0#{value.currency.exponent}d", (value.abs % value.currency.subunit_to_unit).cents
end
end
end
5 changes: 5 additions & 0 deletions spec/helpers/action_view_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
let(:monetizable_object){ false }
it { is_expected.to eq "00" }
end

context 'with a negative monetizable object' do
let(:monetizable_object){ Money.new(-1_25) }
it { is_expected.to eq "25" }
end
end

context 'respects MoneyRails::Configuration settings' do
Expand Down