money-rails 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a8e694d77fe73ebc1b806d1aeb709f74c5e25c2
4
- data.tar.gz: 2ed6d897d12f3ff13a08379e97404a9bfe54568f
3
+ metadata.gz: cadb127fbc12e2e400269dc9b0996c02f55c8cd6
4
+ data.tar.gz: c5e3dcc6f9d53d512927a91888b78ba996a0553e
5
5
  SHA512:
6
- metadata.gz: de46c3822927fd875d5a7fdf8a778562fe501d7837d50ae746b035205f1463782672f6d32ede43271fb34fbf312770183354493d99e57cf2b8e027d7fccd7673
7
- data.tar.gz: e3dee272257eaf62e7b631d2486d9bd2ae9ba9c6dfd0c3c04a0ff5e750b747a15a5acdc8544693c264983a220a66c9b034640eb1819fbe2e51ecbe6867c8fc0d
6
+ metadata.gz: 9c57f0377d92586e1cb7935cf780fadb16c204230dbc0b2a449bed4d94839e8d0013681aeb03530412cf0da5fc433900711a19378bb0caedfb20b58364a07b90
7
+ data.tar.gz: 43cf45749ad6a096ad3d83feb3004cc8d85546f28f0a8846b7eb98ba53313e75d401d0726c0811fd6a3b78798def69f2afdd77ff89b765843e335894159f2ff0
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.6.2
4
+
5
+ - Fix attribute order possibly affecting the value of monetized attribute
6
+ - Add support for RailsAdmin (use :money type)
7
+ - Raise error when gem was unable to infer monetized attribute name
8
+ - Revert decimal mark and thousands separator addtion, since formatting should depend on country and locale, instead of currency
9
+
3
10
  ## 1.6.1
4
11
 
5
12
  - View helper now respects currency decimal mark and thousands separator
@@ -58,6 +58,21 @@ MoneyRails.configure do |config|
58
58
  # :decimal_mark => ","
59
59
  # }
60
60
 
61
+ # Specify a rounding mode
62
+ # Any one of:
63
+ #
64
+ # BigDecimal::ROUND_UP,
65
+ # BigDecimal::ROUND_DOWN,
66
+ # BigDecimal::ROUND_HALF_UP,
67
+ # BigDecimal::ROUND_HALF_DOWN,
68
+ # BigDecimal::ROUND_HALF_EVEN,
69
+ # BigDecimal::ROUND_CEILING,
70
+ # BigDecimal::ROUND_FLOOR
71
+ #
72
+ # set to BigDecimal::ROUND_HALF_EVEN by default
73
+ #
74
+ # config.rounding_mode = BigDecimal::ROUND_HALF_UP
75
+
61
76
  # Set default money format globally.
62
77
  # Default value is nil meaning "ignore this option".
63
78
  # Example:
@@ -14,3 +14,7 @@ if defined? Rails
14
14
  require "money-rails/railtie"
15
15
  require "money-rails/engine"
16
16
  end
17
+
18
+ if Object.const_defined?("RailsAdmin")
19
+ require "money-rails/rails_admin"
20
+ end
@@ -50,8 +50,9 @@ module MoneyRails
50
50
  elsif subunit_name =~ /#{MoneyRails::Configuration.amount_column[:postfix]}$/
51
51
  name = subunit_name.sub(/#{MoneyRails::Configuration.amount_column[:postfix]}$/, "")
52
52
  else
53
- # FIXME: provide a better default
54
- name = [subunit_name, "money"].join("_")
53
+ raise ArgumentError, "Unable to infer the name of the monetizable attribute for '#{subunit_name}'. " \
54
+ "Expected amount column postfix is '#{MoneyRails::Configuration.amount_column[:postfix]}'. " \
55
+ "Use :as option to explicitly specify the name or change the amount column postfix in the initializer."
55
56
  end
56
57
 
57
58
  # Optional accessor to be run on an instance to detect currency
@@ -127,12 +128,18 @@ module MoneyRails
127
128
  memoized = instance_variable_get("@#{name}")
128
129
 
129
130
  # Dont create a new Money instance if the values haven't been changed.
130
- if memoized && memoized.cents == amount &&
131
- memoized.currency == attr_currency
132
- result = memoized
133
- else
131
+ if memoized && memoized.cents == amount
132
+ if memoized.currency == attr_currency
133
+ result = memoized
134
+ else
135
+ memoized_amount = memoized.amount.to_money(attr_currency)
136
+ write_attribute subunit_name, memoized_amount.cents
137
+ # Cache the value (it may be nil)
138
+ result = instance_variable_set "@#{name}", memoized_amount
139
+ end
140
+ elsif amount.present?
134
141
  # If amount is NOT nil (or empty string) load the amount in a Money
135
- amount = Money.new(amount, attr_currency) unless amount.blank?
142
+ amount = Money.new(amount, attr_currency)
136
143
 
137
144
  # Cache the value (it may be nil)
138
145
  result = instance_variable_set "@#{name}", amount
@@ -11,23 +11,19 @@ module MoneyRails
11
11
  options = { :symbol => options }
12
12
  end
13
13
 
14
- unless value.is_a?(Money)
15
- if value.respond_to?(:to_money)
16
- value = value.to_money
17
- else
18
- return ''
19
- end
20
- end
21
-
22
14
  options = {
23
- :no_cents_if_whole => MoneyRails::Configuration.no_cents_if_whole.nil? ? true : MoneyRails::Configuration.no_cents_if_whole,
24
- :symbol => false,
25
- :decimal_mark => value.currency.decimal_mark,
26
- :thousands_separator => value.currency.thousands_separator
15
+ :no_cents_if_whole => MoneyRails::Configuration.no_cents_if_whole.nil? ? true : MoneyRails::Configuration.no_cents_if_whole,
16
+ :symbol => false
27
17
  }.merge(options)
28
18
  options.delete(:symbol) if options[:disambiguate]
29
19
 
30
- value.format(options)
20
+ if value.is_a?(Money)
21
+ value.format(options)
22
+ elsif value.respond_to?(:to_money)
23
+ value.to_money.format(options)
24
+ else
25
+ ""
26
+ end
31
27
  end
32
28
 
33
29
  def humanized_money_with_symbol(value, options={})
@@ -0,0 +1,21 @@
1
+ require 'rails_admin/config/fields'
2
+ require 'rails_admin/config/fields/types/integer'
3
+ require 'money-rails/helpers/action_view_extension'
4
+
5
+ include MoneyRails::ActionViewExtension
6
+
7
+ module RailsAdmin
8
+ module Config
9
+ module Fields
10
+ module Types
11
+ class Money < RailsAdmin::Config::Fields::Types::Integer
12
+ RailsAdmin::Config::Fields::Types::register(self)
13
+
14
+ register_instance_option :pretty_value do
15
+ humanized_money_with_symbol(value)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module MoneyRails
2
- VERSION = "1.6.1"
2
+ VERSION = '1.6.2'
3
3
  end
@@ -77,6 +77,14 @@ if defined? ActiveRecord
77
77
  expect(product.price_cents).to eq(215)
78
78
  end
79
79
 
80
+ it "assigns the correct value from params" do
81
+ params_clp = { amount: '20000', tax: '1000', currency: 'CLP' }
82
+ product = Transaction.create(params_clp)
83
+ expect(product.valid?).to be_truthy
84
+ expect(product.amount.currency.subunit_to_unit).to eq(1)
85
+ expect(product.amount_cents).to eq(20000)
86
+ end
87
+
80
88
  it "raises an error if trying to create two attributes with the same name" do
81
89
  expect do
82
90
  class Product
@@ -93,6 +101,19 @@ if defined? ActiveRecord
93
101
  end.to raise_error ArgumentError
94
102
  end
95
103
 
104
+ it "raises an error when unable to infer attribute name" do
105
+ old_postfix = MoneyRails::Configuration.amount_column[:postfix]
106
+ MoneyRails::Configuration.amount_column[:postfix] = '_pennies'
107
+
108
+ expect do
109
+ class AnotherProduct < Product
110
+ monetize :price_cents
111
+ end
112
+ end.to raise_error ArgumentError
113
+
114
+ MoneyRails::Configuration.amount_column[:postfix] = old_postfix
115
+ end
116
+
96
117
  it "allows subclass to redefine attribute with the same name" do
97
118
  class SubProduct < Product
98
119
  monetize :discount, as: :discount_price, with_currency: :gbp
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe 'MoneyRails::ActionViewExtension', :type => :helper do
@@ -46,18 +44,10 @@ describe 'MoneyRails::ActionViewExtension', :type => :helper do
46
44
  end
47
45
 
48
46
  describe '#humanized_money_with_symbol' do
49
- let(:amount) { 125_010 }
50
- let(:expected_values) { {
51
- :eur => '€1.250,10',
52
- :usd => '$1,250.10',
53
- :sek => '1 250,10 kr'
54
- } }
55
-
56
- it 'returns the correctly formatted values' do
57
- expected_values.each do |currency, result|
58
- expect(helper.humanized_money_with_symbol(Money.new(amount, currency))).to eq result
59
- end
60
- end
47
+ subject { helper.humanized_money_with_symbol Money.new(12500) }
48
+ it { is_expected.to be_a String }
49
+ it { is_expected.not_to include Money.default_currency.decimal_mark }
50
+ it { is_expected.to include Money.default_currency.symbol }
61
51
  end
62
52
 
63
53
  describe '#money_without_cents' do
@@ -96,7 +86,7 @@ describe 'MoneyRails::ActionViewExtension', :type => :helper do
96
86
  describe '#humanized_money' do
97
87
  subject { helper.humanized_money Money.new(12500) }
98
88
  it { is_expected.to be_a String }
99
- it { is_expected.to include Money.default_currency.decimal_mark }
89
+ it { is_expected.not_to include Money.default_currency.decimal_mark }
100
90
  it { is_expected.not_to include Money.default_currency.symbol }
101
91
  it { is_expected.to include "00" }
102
92
  end
@@ -104,7 +94,7 @@ describe 'MoneyRails::ActionViewExtension', :type => :helper do
104
94
  describe '#humanized_money_with_symbol' do
105
95
  subject { helper.humanized_money_with_symbol Money.new(12500) }
106
96
  it { is_expected.to be_a String }
107
- it { is_expected.to include Money.default_currency.decimal_mark }
97
+ it { is_expected.not_to include Money.default_currency.decimal_mark }
108
98
  it { is_expected.to include Money.default_currency.symbol }
109
99
  it { is_expected.to include "00" }
110
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Loupasakis
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-05-08 00:00:00.000000000 Z
13
+ date: 2016-06-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: money
@@ -159,6 +159,7 @@ files:
159
159
  - lib/money-rails/money.rb
160
160
  - lib/money-rails/mongoid/money.rb
161
161
  - lib/money-rails/mongoid/two.rb
162
+ - lib/money-rails/rails_admin.rb
162
163
  - lib/money-rails/railtie.rb
163
164
  - lib/money-rails/test_helpers.rb
164
165
  - lib/money-rails/version.rb