diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a452444eb..24760e14dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Allow monetizing methods with kwargs +- Fix money_only_cents for negative money ## 1.15.0 diff --git a/lib/money-rails/helpers/action_view_extension.rb b/lib/money-rails/helpers/action_view_extension.rb index 4a5b88aa82..f6205946a7 100644 --- a/lib/money-rails/helpers/action_view_extension.rb +++ b/lib/money-rails/helpers/action_view_extension.rb @@ -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 diff --git a/spec/helpers/action_view_extension_spec.rb b/spec/helpers/action_view_extension_spec.rb index c62f53a081..26a9ad5179 100644 --- a/spec/helpers/action_view_extension_spec.rb +++ b/spec/helpers/action_view_extension_spec.rb @@ -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