From 99a2216272750e8be783b82d6d3a8043b94a88d4 Mon Sep 17 00:00:00 2001 From: Zane Wolfgang Pickett Date: Wed, 24 Apr 2024 21:14:07 +0200 Subject: [PATCH 1/4] Allow nil to be set as default currency --- Gemfile | 2 +- lib/money-rails/configuration.rb | 4 +-- spec/active_record/monetizable_spec.rb | 19 ++++++++++++++ spec/dummy/app/models/other_product.rb | 3 +++ .../20240425081448_add_other_products.rb | 10 +++++++ spec/dummy/db/schema.rb | 26 ++++++++++++------- spec/dummy/db/structure.sql | 3 ++- 7 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 spec/dummy/app/models/other_product.rb create mode 100644 spec/dummy/db/migrate/20240425081448_add_other_products.rb diff --git a/Gemfile b/Gemfile index ebddec7259..e77017e3fb 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,7 @@ platforms :jruby do end platforms :ruby do - gem "sqlite3" + gem "sqlite3", '~> 1.4' end platform :mri do diff --git a/lib/money-rails/configuration.rb b/lib/money-rails/configuration.rb index 129dba97f9..347ae4fc87 100644 --- a/lib/money-rails/configuration.rb +++ b/lib/money-rails/configuration.rb @@ -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 @@ -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 diff --git a/spec/active_record/monetizable_spec.rb b/spec/active_record/monetizable_spec.rb index 85dd378fbe..aedc3deccd 100644 --- a/spec/active_record/monetizable_spec.rb +++ b/spec/active_record/monetizable_spec.rb @@ -949,6 +949,25 @@ class SubProduct < Product expect(price.amount).not_to eq(value.amount) end + context 'without a default currency' do + let(:product) { OtherProduct.new } + + around do |example| + default_currency = Money.default_currency + Money.default_currency = nil + + example.run + + Money.default_currency = default_currency + end + + it "errors a NoCurrency Error" 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, {} diff --git a/spec/dummy/app/models/other_product.rb b/spec/dummy/app/models/other_product.rb new file mode 100644 index 0000000000..574c71e8c5 --- /dev/null +++ b/spec/dummy/app/models/other_product.rb @@ -0,0 +1,3 @@ +class OtherProduct < ActiveRecord::Base + monetize :price_cents +end diff --git a/spec/dummy/db/migrate/20240425081448_add_other_products.rb b/spec/dummy/db/migrate/20240425081448_add_other_products.rb new file mode 100644 index 0000000000..c7d9adbd1a --- /dev/null +++ b/spec/dummy/db/migrate/20240425081448_add_other_products.rb @@ -0,0 +1,10 @@ +class AddOtherProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration) + def change + create_table "other_products", force: :cascade do |t| + t.string "currency" + t.integer "price_cents" + t.datetime "created_at" + t.datetime "updated_at" + end + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 540b103fa9..84067ea0a4 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,20 +10,26 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2015_10_26_220420) do - +ActiveRecord::Schema[7.0].define(version: 2024_04_25_081448) do create_table "dummy_products", force: :cascade do |t| t.string "currency" t.integer "price_cents" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", precision: nil + t.datetime "updated_at", precision: nil + end + + create_table "other_products", force: :cascade do |t| + t.string "currency" + t.integer "price_cents" + t.datetime "created_at", precision: nil + t.datetime "updated_at", precision: nil end create_table "products", force: :cascade do |t| t.integer "price_cents" t.integer "discount" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", precision: nil + t.datetime "updated_at", precision: nil t.integer "bonus_cents" t.integer "optional_price_cents" t.integer "sale_price_amount", default: 0, null: false @@ -43,16 +49,16 @@ create_table "services", force: :cascade do |t| t.integer "charge_cents" t.integer "discount_cents" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", precision: nil + t.datetime "updated_at", precision: nil end create_table "transactions", force: :cascade do |t| t.integer "amount_cents" t.integer "tax_cents" t.string "currency" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", precision: nil + t.datetime "updated_at", precision: nil t.integer "optional_amount_cents" end diff --git a/spec/dummy/db/structure.sql b/spec/dummy/db/structure.sql index 7f9860252e..78c1868b9e 100644 --- a/spec/dummy/db/structure.sql +++ b/spec/dummy/db/structure.sql @@ -1,5 +1,6 @@ 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); 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); +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; CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL); 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); 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'); INSERT INTO schema_migrations (version) VALUES ('20120712202655'); -INSERT INTO schema_migrations (version) VALUES ('20130124023419'); \ No newline at end of file +INSERT INTO schema_migrations (version) VALUES ('20130124023419'); From 1db68f988bb8258e84e5c8395c683ea944cfe2c9 Mon Sep 17 00:00:00 2001 From: Zane Wolfgang Pickett Date: Wed, 1 May 2024 21:20:05 +0200 Subject: [PATCH 2/4] Apply feedback from PR --- lib/money-rails/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/money-rails/configuration.rb b/lib/money-rails/configuration.rb index 347ae4fc87..34fa5bb5ec 100644 --- a/lib/money-rails/configuration.rb +++ b/lib/money-rails/configuration.rb @@ -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 && default_currency.iso_code currency_column.merge! default: iso_code end From e668c17b146465454e43e32be3b84e127924591b Mon Sep 17 00:00:00 2001 From: Zane Wolfgang Pickett Date: Tue, 14 May 2024 19:31:08 +0200 Subject: [PATCH 3/4] Fix issue trying to migrate CI --- spec/dummy/db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 84067ea0a4..edf03672f6 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_04_25_081448) do +ActiveRecord::Schema.define(version: 2024_04_25_081448) do create_table "dummy_products", force: :cascade do |t| t.string "currency" t.integer "price_cents" From cdefd40af00c49bf3948b99babd9922cf510e2b6 Mon Sep 17 00:00:00 2001 From: Zane Wolfgang Pickett Date: Tue, 14 May 2024 19:32:09 +0200 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24760e14dd..22a03ca9ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Allow monetizing methods with kwargs - Fix money_only_cents for negative money +- Allow `nil` to be set as the default currency ## 1.15.0