Skip to content

Commit

Permalink
Merge branch 'main' into fix-monetized-attributes-class-discrepancy
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 authored Mar 13, 2024
2 parents 42a5024 + 7192ce1 commit b32228e
Show file tree
Hide file tree
Showing 6 changed files with 34 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
9 changes: 8 additions & 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 Expand Up @@ -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
```

Expand All @@ -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

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 b32228e

Please sign in to comment.