From a128610a0ddfc213d8e09480c3553e8692463b89 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 25 Apr 2023 14:04:23 +0200 Subject: [PATCH 1/7] Allow monetizing methods with kwargs --- lib/money-rails/active_record/monetizable.rb | 8 ++++---- spec/active_record/monetizable_spec.rb | 4 ++++ spec/dummy/app/models/transaction.rb | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/money-rails/active_record/monetizable.rb b/lib/money-rails/active_record/monetizable.rb index 8bebfc5b62..0ade75b94f 100644 --- a/lib/money-rails/active_record/monetizable.rb +++ b/lib/money-rails/active_record/monetizable.rb @@ -118,8 +118,8 @@ def monetize(*fields) # Getter for monetized attribute - define_method name do |*args| - read_monetized name, subunit_name, options, *args + define_method name do |*args, **kwargs| + read_monetized name, subunit_name, options, *args, **kwargs end # Setter for monetized attribute @@ -178,9 +178,9 @@ def track_monetized_attribute(name, value) end end - def read_monetized(name, subunit_name, options = {}, *args) + def read_monetized(name, subunit_name, options = {}, *args, **kwargs) # Get the cents - amount = public_send(subunit_name, *args) + amount = public_send(subunit_name, *args, **kwargs) return if amount.nil? && options[:allow_nil] # Get the currency object diff --git a/spec/active_record/monetizable_spec.rb b/spec/active_record/monetizable_spec.rb index 7ce3bd200d..85dd378fbe 100644 --- a/spec/active_record/monetizable_spec.rb +++ b/spec/active_record/monetizable_spec.rb @@ -725,6 +725,10 @@ class SubProduct < Product expect(transaction.total).to eq(Money.new(3000, :usd)) end + it "constructs the money object from the mapped method value with arguments" do + expect(transaction.total(1, bar: 2)).to eq(Money.new(3003, :usd)) + end + it "allows currency column postfix to be blank" do allow(MoneyRails::Configuration).to receive(:currency_column) { { postfix: nil, column_name: 'currency' } } expect(dummy_product_with_nil_currency.price.currency).to eq(Money::Currency.find(:gbp)) diff --git a/spec/dummy/app/models/transaction.rb b/spec/dummy/app/models/transaction.rb index e7cf4c1e51..6efb8df099 100644 --- a/spec/dummy/app/models/transaction.rb +++ b/spec/dummy/app/models/transaction.rb @@ -7,7 +7,7 @@ class Transaction < ActiveRecord::Base monetize :optional_amount_cents, with_model_currency: :currency, allow_nil: true - def total_cents - return amount_cents + tax_cents + def total_cents(foo = 0, bar: 0) + return amount_cents + tax_cents + foo + bar end end From fcb1f863461538b8eef273276ae2bbb7cbacb8be Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 21 Jul 2023 18:29:21 +0200 Subject: [PATCH 2/7] Fix CI badge The old badge was pointing to Travis. This commits uses the GitHub generated markdown code for ruby workflow [ci skip] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 75f549cfc5..fa1c3214bc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RubyMoney - Money-Rails [![Gem Version](https://badge.fury.io/rb/money-rails.svg)](http://badge.fury.io/rb/money-rails) -[![Build Status](https://secure.travis-ci.org/RubyMoney/money-rails.svg?branch=master)](http://travis-ci.org/RubyMoney/money-rails) +[![Ruby](https://github.com/RubyMoney/money-rails/actions/workflows/ruby.yml/badge.svg)](https://github.com/RubyMoney/money-rails/actions/workflows/ruby.yml) [![License](http://img.shields.io/:license-mit-green.svg?style=flat)](http://opensource.org/licenses/MIT) ## Introduction From 604b7de3c2a3973095ea472bb0c9dd26f92c12cb Mon Sep 17 00:00:00 2001 From: Graham Rogers Date: Wed, 23 Aug 2023 13:52:51 +0100 Subject: [PATCH 3/7] Document raise_error_on_money_parsing --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 75f549cfc5..cb1b787982 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,12 @@ MoneyRails.configure do |config| # symbol: nil, # sign_before_symbol: nil # } + + # Set whether an error should be raised when parsing money values + # This includes assigning to a monetized field with the wrong currency + # Default value is false + # + # config.raise_error_on_money_parsing = true end ``` @@ -493,6 +499,7 @@ end * `amount_column`: Provide values for the amount column (holding the fractional part of a money object). * `currency_column`: Provide default values or even disable (`present: false`) the currency column. * `rounding_mode`: Set `Money.rounding_mode` to one of the BigDecimal constants. +* `raise_error_on_money_parsing`: Set whether errors should be raised when parsing money values ### Helpers From 298c955cd1376eac3e9c0fd98a7031f86ba9f076 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Wed, 27 Dec 2023 21:08:20 +0100 Subject: [PATCH 4/7] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ecd207adf..2a452444eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Allow monetizing methods with kwargs + ## 1.15.0 - Bump money version to ~> 6.16 From 8d71d6a5ed654462d1cef1decfb8fd5cf63f2a4c Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 28 Dec 2023 13:50:52 +0100 Subject: [PATCH 5/7] Point of no return --- spec/dummy/app/models/transaction.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/dummy/app/models/transaction.rb b/spec/dummy/app/models/transaction.rb index 6efb8df099..4b1fe7983a 100644 --- a/spec/dummy/app/models/transaction.rb +++ b/spec/dummy/app/models/transaction.rb @@ -8,6 +8,6 @@ class Transaction < ActiveRecord::Base monetize :optional_amount_cents, with_model_currency: :currency, allow_nil: true def total_cents(foo = 0, bar: 0) - return amount_cents + tax_cents + foo + bar + amount_cents + tax_cents + foo + bar end end From ce036104010cb79498b955debfef77ff2544ee6a Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 28 Dec 2023 13:50:59 +0100 Subject: [PATCH 6/7] Ruby 2.x compat --- lib/money-rails/active_record/monetizable.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/money-rails/active_record/monetizable.rb b/lib/money-rails/active_record/monetizable.rb index 0ade75b94f..35aa2eaba3 100644 --- a/lib/money-rails/active_record/monetizable.rb +++ b/lib/money-rails/active_record/monetizable.rb @@ -178,9 +178,19 @@ def track_monetized_attribute(name, value) end end - def read_monetized(name, subunit_name, options = {}, *args, **kwargs) - # Get the cents - amount = public_send(subunit_name, *args, **kwargs) + def read_monetized(name, subunit_name, options = nil, *args, **kwargs) + # Ruby 2.x compatibility + if options.nil? + options = kwargs + kwargs = {} + end + + if kwargs.any? + amount = public_send(subunit_name, *args, **kwargs) + else + # Ruby 2.x does not allow empty kwargs + amount = public_send(subunit_name, *args) + end return if amount.nil? && options[:allow_nil] # Get the currency object From 6f701ea8d17a1ef112d4b980e4bc1138938dbfb7 Mon Sep 17 00:00:00 2001 From: Shane Emmons <27679+semmons99@users.noreply.github.com> Date: Mon, 1 Jan 2024 11:12:49 -0500 Subject: [PATCH 7/7] chore: Update copyright year in LICENSE file --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index c481edcbc0..66caedf217 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ MIT License Copyright (c) 2012 Andreas Loupasakis -Copyright (c) 2023 Shane Emmons +Copyright (c) 2024 Shane Emmons Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal