Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -5,6 +5,7 @@
- Allow monetizing methods with kwargs
- Fix money_only_cents for negative money
- BREAKING CHANGE: Drop support for Ruby < 3 and Rails < 6.1
- Allow `nil` to be set as the default currency

## 1.15.0

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 && default_currency.iso_code
Copy link
Contributor

Choose a reason for hiding this comment

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

Since Money only supports Ruby > 3.1, it is possible to use the safe navigation operator.

This kind of issues can be spotted with RuboCop

Suggested change
iso_code = default_currency && 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
Comment on lines +952 to +962
Copy link
Contributor

Choose a reason for hiding this comment

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

it is better to use ensure here in case the test does not pass

Copy link
Member

Choose a reason for hiding this comment

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

Using around like this still calls whatever is called after example.run even when the test fails or raises exceptions, so this is fine 👍🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, and apologies for the misleading comment.

I have a branch with improvements to the spec, I'll remove the ensure from here

around(:each) do |example|
begin
Money.locale_backend = :currency
example.run
ensure
Money.locale_backend = :i18n
end
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
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');