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 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..34fa5bb5ec 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 && 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..edf03672f6 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.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');