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 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 diff --git a/README.md b/README.md index cb1b787982..a5ff20af51 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 diff --git a/lib/money-rails/active_record/monetizable.rb b/lib/money-rails/active_record/monetizable.rb index 8bebfc5b62..35aa2eaba3 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,19 @@ def track_monetized_attribute(name, value) end end - def read_monetized(name, subunit_name, options = {}, *args) - # Get the cents - amount = public_send(subunit_name, *args) + 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 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..4b1fe7983a 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) + amount_cents + tax_cents + foo + bar end end