Skip to content

Commit

Permalink
Merge branch 'main' into raise_error_on_money_parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 authored Mar 13, 2024
2 parents 604b7de + 2d925d8 commit 302b685
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Allow monetizing methods with kwargs

## 1.15.0

- Bump money version to ~> 6.16
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 15 additions & 5 deletions lib/money-rails/active_record/monetizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/active_record/monetizable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 302b685

Please sign in to comment.