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 25, 2024
1 parent df7a030 commit 99a2216
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ platforms :jruby do
end

platforms :ruby do
gem "sqlite3"
gem "sqlite3", '~> 1.4'
end

platform :mri do
Expand Down
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
19 changes: 19 additions & 0 deletions spec/active_record/monetizable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}
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
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20240425081448_add_other_products.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 16 additions & 10 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/db/structure.sql
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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');
INSERT INTO schema_migrations (version) VALUES ('20130124023419');

0 comments on commit 99a2216

Please sign in to comment.