Skip to content

Commit

Permalink
Allow nil to be set as default currency
Browse files Browse the repository at this point in the history
  • Loading branch information
sirwolfgang committed Apr 24, 2024
1 parent df7a030 commit f5d1fc4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/money-rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def configure
# Configuration parameters

def default_currency
Money::Currency.new(Money.default_currency)
Money::Currency.new(Money.default_currency) if Money.default_currency
end

# Set default currency of money library
Expand All @@ -35,7 +35,7 @@ def register_currency=(currency_options)
end

def set_currency_column_for_default_currency!
iso_code = default_currency.iso_code
iso_code = default_currency&.iso_code
currency_column.merge! default: iso_code
end

Expand Down
21 changes: 21 additions & 0 deletions spec/active_record/monetizable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,27 @@ class SubProduct < Product
expect(price.amount).not_to eq(value.amount)
end

context 'without a default currency' do
let(:product) { OtherProduct.new }

before do
around do |example|
default_currency = MoneyRails::Configuration.default_currency
MoneyRails::Configuration.default_currency = nil

example.run

MoneyRails::Configuration.default_currency = default_currency
end
end

it "errors when" do
expect do
product.write_monetized :price, :price_cents, 10.5, false, nil, {}
end.to raise_error(Money::Currency::NoCurrency)
end
end

describe "instance_currency_name" do
it "updates instance_currency_name attribute" do
product.write_monetized :sale_price, :sale_price_amount, value, false, :sale_price_currency_code, {}
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/app/models/other_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OtherProduct < ActiveRecord::Base
monetize :price_cents
end

0 comments on commit f5d1fc4

Please sign in to comment.