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

raise UnknownCurrency error when currency is invalid #310

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
11 changes: 1 addition & 10 deletions lib/money/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,7 @@ def value_to_currency(currency)
when 'xxx', 'XXX'
Money::NULL_CURRENCY
when String
begin
Currency.find!(currency)
rescue Money::Currency::UnknownCurrency => error
if Money.config.legacy_deprecations
Money.deprecate(error.message)
Money::NULL_CURRENCY
else
raise error
end
end
Currency.find!(currency)
else
raise ArgumentError, "could not parse as currency #{currency.inspect}"
end
Expand Down
7 changes: 2 additions & 5 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@
expect(subject.value_to_currency('usd')).to eq(Money::Currency.new('USD'))
end

it 'returns the null currency when invalid iso is passed' do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).once
expect(subject.value_to_currency('invalid')).to eq(Money::NULL_CURRENCY)
end
it 'raises error when invalid iso is passed' do
expect { subject.value_to_currency('invalid') }.to raise_error(Money::Currency::UnknownCurrency)
end

it 'raises on invalid object' do
Expand Down
28 changes: 11 additions & 17 deletions spec/money_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@ class MoneyClassInheritance2 < MoneyWithCustomAccessors
expect(record.price_usd).to eq(Money.new(1.44, 'USD'))
end

it 'returns money with null currency when the currency in the DB is invalid' do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).once
record.update_columns(currency: 'invalid')
record.reload
expect(record.price.currency).to be_a(Money::NullCurrency)
expect(record.price.value).to eq(1.23)
end
it 'raises UnknownCurrency error when the currency in the DB is invalid' do
record.update_columns(currency: 'invalid')
record.reload
expect { record.price.currency }.to raise_error(Money::Currency::UnknownCurrency)
expect { record.price.value }.to raise_error(Money::Currency::UnknownCurrency)
end

it 'handles legacy support for saving floats' do
Expand Down Expand Up @@ -248,15 +245,12 @@ class MoneyWithWrongCurrencyArguments < ActiveRecord::Base
expect(record.price.currency.to_s).to eq('USD')
end

it 'reads an invalid currency from the db and generates a no currency object' do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).once
record = MoneyWithReadOnlyCurrency.create
record.update_columns(currency: 'invalid', price: 1)
record.reload
expect(record.price.value).to eq(1)
expect(record.price.currency.to_s).to eq('')
end
it 'raises UnknownCurrency error when reads an invalid currency from the DB' do
record = MoneyWithReadOnlyCurrency.create
record.update_columns(currency: 'invalid', price: 1)
record.reload
expect { record.price.value }.to raise_error(Money::Currency::UnknownCurrency)
expect { record.price.currency } .to raise_error(Money::Currency::UnknownCurrency)
end

it 'sets the currency correctly when the currency is changed' do
Expand Down
Loading