Skip to content

Commit 99a2216

Browse files
committed
Allow nil to be set as default currency
1 parent df7a030 commit 99a2216

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ platforms :jruby do
99
end
1010

1111
platforms :ruby do
12-
gem "sqlite3"
12+
gem "sqlite3", '~> 1.4'
1313
end
1414

1515
platform :mri do

lib/money-rails/configuration.rb

+2-2
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&.iso_code
3939
currency_column.merge! default: iso_code
4040
end
4141

spec/active_record/monetizable_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,25 @@ class SubProduct < Product
949949
expect(price.amount).not_to eq(value.amount)
950950
end
951951

952+
context 'without a default currency' do
953+
let(:product) { OtherProduct.new }
954+
955+
around do |example|
956+
default_currency = Money.default_currency
957+
Money.default_currency = nil
958+
959+
example.run
960+
961+
Money.default_currency = default_currency
962+
end
963+
964+
it "errors a NoCurrency Error" do
965+
expect do
966+
product.write_monetized :price, :price_cents, 10.5, false, nil, {}
967+
end.to raise_error(Money::Currency::NoCurrency)
968+
end
969+
end
970+
952971
describe "instance_currency_name" do
953972
it "updates instance_currency_name attribute" do
954973
product.write_monetized :sale_price, :sale_price_amount, value, false, :sale_price_currency_code, {}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class OtherProduct < ActiveRecord::Base
2+
monetize :price_cents
3+
end
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/schema.rb

+16-10
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2015_10_26_220420) do
14-
13+
ActiveRecord::Schema[7.0].define(version: 2024_04_25_081448) do
1514
create_table "dummy_products", force: :cascade do |t|
1615
t.string "currency"
1716
t.integer "price_cents"
18-
t.datetime "created_at"
19-
t.datetime "updated_at"
17+
t.datetime "created_at", precision: nil
18+
t.datetime "updated_at", precision: nil
19+
end
20+
21+
create_table "other_products", force: :cascade do |t|
22+
t.string "currency"
23+
t.integer "price_cents"
24+
t.datetime "created_at", precision: nil
25+
t.datetime "updated_at", precision: nil
2026
end
2127

2228
create_table "products", force: :cascade do |t|
2329
t.integer "price_cents"
2430
t.integer "discount"
25-
t.datetime "created_at"
26-
t.datetime "updated_at"
31+
t.datetime "created_at", precision: nil
32+
t.datetime "updated_at", precision: nil
2733
t.integer "bonus_cents"
2834
t.integer "optional_price_cents"
2935
t.integer "sale_price_amount", default: 0, null: false
@@ -43,16 +49,16 @@
4349
create_table "services", force: :cascade do |t|
4450
t.integer "charge_cents"
4551
t.integer "discount_cents"
46-
t.datetime "created_at"
47-
t.datetime "updated_at"
52+
t.datetime "created_at", precision: nil
53+
t.datetime "updated_at", precision: nil
4854
end
4955

5056
create_table "transactions", force: :cascade do |t|
5157
t.integer "amount_cents"
5258
t.integer "tax_cents"
5359
t.string "currency"
54-
t.datetime "created_at"
55-
t.datetime "updated_at"
60+
t.datetime "created_at", precision: nil
61+
t.datetime "updated_at", precision: nil
5662
t.integer "optional_amount_cents"
5763
end
5864

spec/dummy/db/structure.sql

+2-1
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)