Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nil to be set as default currency #698

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ platforms :jruby do
end

platforms :ruby do
gem "sqlite3"
gem "sqlite3", '~> 1.4'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

platform :mri do
4 changes: 2 additions & 2 deletions lib/money-rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions spec/active_record/monetizable_spec.rb
Original file line number Diff line number Diff line change
@@ -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, {}
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
@@ -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

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);
@@ -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');