Skip to content

Commit 3015da3

Browse files
authored
Merge pull request #698 from quiltt/main
Allow nil to be set as default currency
2 parents c7645b0 + 7d1d493 commit 3015da3

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Allow monetizing methods with kwargs
66
- Fix money_only_cents for negative money
77
- BREAKING CHANGE: Drop support for Ruby < 3 and Rails < 6.1
8+
- Allow `nil` to be set as the default currency
89

910
## 1.15.0
1011

lib/money-rails/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def configure
2020
# Configuration parameters
2121

2222
def default_currency
23-
Money::Currency.new(Money.default_currency)
23+
Money::Currency.new(Money.default_currency) if Money.default_currency
2424
end
2525

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

3737
def set_currency_column_for_default_currency!
38-
iso_code = default_currency.iso_code
38+
iso_code = default_currency && default_currency.iso_code
3939
currency_column.merge! default: iso_code
4040
end
4141

spec/active_record/monetizable_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,25 @@ class SubProduct < Product
947947
expect(price.amount).not_to eq(value.amount)
948948
end
949949

950+
context 'without a default currency' do
951+
let(:product) { OtherProduct.new }
952+
953+
around do |example|
954+
default_currency = Money.default_currency
955+
Money.default_currency = nil
956+
957+
example.run
958+
959+
Money.default_currency = default_currency
960+
end
961+
962+
it "errors a NoCurrency Error" do
963+
expect do
964+
product.write_monetized :price, :price_cents, 10.5, false, nil, {}
965+
end.to raise_error(Money::Currency::NoCurrency)
966+
end
967+
end
968+
950969
describe "instance_currency_name" do
951970
it "updates instance_currency_name attribute" do
952971
product.write_monetized :sale_price, :sale_price_amount, value, false, :sale_price_currency_code, {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class OtherProduct < ActiveRecord::Base
2+
monetize :price_cents
3+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class AddOtherProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2+
def change
3+
create_table "other_products", force: :cascade do |t|
4+
t.string "currency"
5+
t.integer "price_cents"
6+
t.datetime "created_at"
7+
t.datetime "updated_at"
8+
end
9+
end
10+
end

spec/dummy/db/structure.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CREATE TABLE "dummy_products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "currency" varchar(255), "price_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
22
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "price_cents" integer, "discount" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "bonus_cents" integer, "optional_price_cents" integer, "sale_price_amount" integer DEFAULT 0 NOT NULL, "sale_price_currency_code" varchar(255), "price_in_a_range_cents" integer);
3+
CREATE TABLE "other_products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "price_cents" integer, "currency" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL;
34
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
45
CREATE TABLE "services" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "charge_cents" integer, "discount_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
56
CREATE TABLE "transactions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "amount_cents" integer, "tax_cents" integer, "currency" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
@@ -18,4 +19,4 @@ INSERT INTO schema_migrations (version) VALUES ('20120607210247');
1819

1920
INSERT INTO schema_migrations (version) VALUES ('20120712202655');
2021

21-
INSERT INTO schema_migrations (version) VALUES ('20130124023419');
22+
INSERT INTO schema_migrations (version) VALUES ('20130124023419');

0 commit comments

Comments
 (0)