money-rails 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +19 -3
- data/README.md +28 -3
- data/Rakefile +19 -3
- data/lib/money-rails/active_model/validator.rb +23 -7
- data/lib/money-rails/active_record/monetizable.rb +77 -16
- data/lib/money-rails/configuration.rb +4 -1
- data/lib/money-rails/helpers/action_view_extension.rb +2 -2
- data/lib/money-rails/money.rb +2 -1
- data/lib/money-rails/mongoid/money.rb +2 -2
- data/lib/money-rails/mongoid/two.rb +5 -5
- data/lib/money-rails/test_helpers.rb +7 -7
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +1 -1
- data/spec/active_record/monetizable_spec.rb +250 -165
- data/spec/configuration_spec.rb +27 -0
- data/spec/dummy/app/models/dummy_product.rb +1 -1
- data/spec/dummy/app/models/priceable.rb +1 -0
- data/spec/dummy/app/models/product.rb +12 -4
- data/spec/dummy/app/models/transaction.rb +3 -3
- data/spec/dummy/config/application.rb +3 -1
- data/spec/dummy/config/environments/test.rb +0 -3
- data/spec/mongoid/three_spec.rb +29 -0
- data/spec/mongoid/two_spec.rb +16 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/test_helpers_spec.rb +21 -4
- metadata +4 -4
data/spec/configuration_spec.rb
CHANGED
@@ -55,6 +55,23 @@ describe "configuration" do
|
|
55
55
|
MoneyRails.symbol = nil
|
56
56
|
end
|
57
57
|
|
58
|
+
it "sets the location of the negative sign for formatted output globally" do
|
59
|
+
value = Money.new(-12345600, "EUR")
|
60
|
+
symbol = Money::Currency.find(:eur).symbol
|
61
|
+
value.format.should =~ /#{symbol}-/
|
62
|
+
|
63
|
+
MoneyRails.sign_before_symbol = false
|
64
|
+
value.format.should =~ /#{symbol}-/
|
65
|
+
value.format(sign_before_symbol: false).should =~ /#{symbol}-/
|
66
|
+
|
67
|
+
MoneyRails.sign_before_symbol = true
|
68
|
+
value.format.should =~ /-#{symbol}/
|
69
|
+
value.format(sign_before_symbol: true).should =~ /-#{symbol}/
|
70
|
+
|
71
|
+
# Reset global setting
|
72
|
+
MoneyRails.sign_before_symbol = nil
|
73
|
+
end
|
74
|
+
|
58
75
|
it "changes the amount and currency column settings based on the default currency" do
|
59
76
|
old_currency = MoneyRails.default_currency
|
60
77
|
MoneyRails.default_currency = :inr
|
@@ -79,5 +96,15 @@ describe "configuration" do
|
|
79
96
|
MoneyRails.default_currency = old_currency
|
80
97
|
end
|
81
98
|
|
99
|
+
it "assigns a default bank" do
|
100
|
+
old_bank = MoneyRails.default_bank
|
101
|
+
|
102
|
+
bank = Money::Bank::VariableExchange.new
|
103
|
+
MoneyRails.default_bank = bank
|
104
|
+
expect(Money.default_bank).to eq(bank)
|
105
|
+
|
106
|
+
MoneyRails.default_bank = old_bank
|
107
|
+
end
|
108
|
+
|
82
109
|
end
|
83
110
|
end
|
@@ -2,7 +2,8 @@ class Product < ActiveRecord::Base
|
|
2
2
|
|
3
3
|
attr_accessible :price_cents, :discount, :bonus_cents,
|
4
4
|
:price, :discount_value, :bonus, :optional_price_cents, :optional_price,
|
5
|
-
:sale_price, :sale_price_amount, :sale_price_currency_code
|
5
|
+
:sale_price, :sale_price_amount, :sale_price_currency_code,
|
6
|
+
:price_in_a_range_cents, :price_in_a_range, :invalid_price_cents
|
6
7
|
|
7
8
|
# Use USD as model level currency
|
8
9
|
register_currency :usd
|
@@ -24,10 +25,17 @@ class Product < ActiveRecord::Base
|
|
24
25
|
:with_model_currency => :sale_price_currency_code
|
25
26
|
|
26
27
|
monetize :price_in_a_range_cents, :allow_nil => true,
|
27
|
-
:
|
28
|
-
:
|
28
|
+
:subunit_numericality => {
|
29
|
+
:greater_than => 0,
|
29
30
|
:less_than_or_equal_to => 10000,
|
30
|
-
|
31
|
+
},
|
32
|
+
:numericality => {
|
33
|
+
:greater_than => 0,
|
34
|
+
:less_than_or_equal_to => 100,
|
35
|
+
:message => "Must be greater than zero and less than $100"
|
31
36
|
}
|
32
37
|
|
38
|
+
attr_accessor :invalid_price_cents
|
39
|
+
monetize :invalid_price_cents, disable_validation: true
|
40
|
+
|
33
41
|
end
|
@@ -2,10 +2,10 @@ class Transaction < ActiveRecord::Base
|
|
2
2
|
|
3
3
|
attr_accessible :amount_cents, :currency, :tax_cents, :amount, :tax
|
4
4
|
|
5
|
-
monetize :amount_cents
|
6
|
-
monetize :tax_cents
|
5
|
+
monetize :amount_cents, :with_model_currency => :currency
|
6
|
+
monetize :tax_cents, :with_model_currency => :currency
|
7
7
|
|
8
|
-
monetize :total_cents
|
8
|
+
monetize :total_cents, :with_model_currency => :currency
|
9
9
|
def total_cents
|
10
10
|
return amount_cents + tax_cents
|
11
11
|
end
|
@@ -4,7 +4,9 @@ require File.expand_path('../boot', __FILE__)
|
|
4
4
|
require "active_record/railtie"
|
5
5
|
require "action_controller/railtie"
|
6
6
|
require "action_mailer/railtie"
|
7
|
-
|
7
|
+
unless Rails::VERSION::MAJOR == 4
|
8
|
+
require "active_resource/railtie"
|
9
|
+
end
|
8
10
|
require "sprockets/railtie"
|
9
11
|
|
10
12
|
Bundler.require
|
@@ -11,9 +11,6 @@ Dummy::Application.configure do
|
|
11
11
|
config.serve_static_assets = true
|
12
12
|
config.static_cache_control = "public, max-age=3600"
|
13
13
|
|
14
|
-
# Log error messages when you accidentally call methods on nil
|
15
|
-
config.whiny_nils = true
|
16
|
-
|
17
14
|
# Show full error reports and disable caching
|
18
15
|
config.consider_all_requests_local = true
|
19
16
|
config.action_controller.perform_caching = false
|
data/spec/mongoid/three_spec.rb
CHANGED
@@ -10,6 +10,13 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
10
10
|
let!(:priceable_from_hash_with_indifferent_access) {
|
11
11
|
Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"}.with_indifferent_access)
|
12
12
|
}
|
13
|
+
let(:priceable_with_infinite_precision) { Priceable.create(:price => Money.new(BigDecimal.new('100.1'), 'EUR')) }
|
14
|
+
let(:priceable_with_hash_field) {
|
15
|
+
Priceable.create(:price_hash => {
|
16
|
+
:key1 => Money.new(100, "EUR"),
|
17
|
+
:key2 => Money.new(200, "USD")
|
18
|
+
})
|
19
|
+
}
|
13
20
|
|
14
21
|
context "mongoize" do
|
15
22
|
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
@@ -36,6 +43,28 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
36
43
|
priceable_from_hash_with_indifferent_access.price.cents.should == 100
|
37
44
|
priceable_from_hash_with_indifferent_access.price.currency.should == Money::Currency.find('EUR')
|
38
45
|
end
|
46
|
+
|
47
|
+
context "infinite_precision = true" do
|
48
|
+
before do
|
49
|
+
Money.infinite_precision = true
|
50
|
+
end
|
51
|
+
|
52
|
+
after do
|
53
|
+
Money.infinite_precision = false
|
54
|
+
end
|
55
|
+
|
56
|
+
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
57
|
+
priceable_with_infinite_precision.price.cents.should == BigDecimal.new('100.1')
|
58
|
+
priceable_with_infinite_precision.price.currency.should == Money::Currency.find('EUR')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "serializes correctly a Hash field containing Money objects" do
|
64
|
+
priceable_with_hash_field.price_hash[:key1][:cents].should == 100
|
65
|
+
priceable_with_hash_field.price_hash[:key2][:cents].should == 200
|
66
|
+
priceable_with_hash_field.price_hash[:key1][:currency_iso].should == 'EUR'
|
67
|
+
priceable_with_hash_field.price_hash[:key2][:currency_iso].should == 'USD'
|
39
68
|
end
|
40
69
|
|
41
70
|
context "demongoize" do
|
data/spec/mongoid/two_spec.rb
CHANGED
@@ -6,6 +6,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
6
6
|
let(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
|
7
7
|
let(:priceable_from_num) { Priceable.create(:price => 1) }
|
8
8
|
let(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
|
9
|
+
let(:priceable_with_infinite_precision) { Priceable.create(:price => Money.new(BigDecimal.new('100.1'), 'EUR')) }
|
9
10
|
|
10
11
|
context "serialize" do
|
11
12
|
it "serializes correctly a Money object to a hash of cents and currency" do
|
@@ -22,6 +23,21 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
22
23
|
priceable_from_string.price.cents.should == 100
|
23
24
|
priceable_from_string.price.currency.should == Money::Currency.find('EUR')
|
24
25
|
end
|
26
|
+
|
27
|
+
context "infinite_precision = true" do
|
28
|
+
before do
|
29
|
+
Money.infinite_precision = true
|
30
|
+
end
|
31
|
+
|
32
|
+
after do
|
33
|
+
Money.infinite_precision = false
|
34
|
+
end
|
35
|
+
|
36
|
+
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
37
|
+
priceable_with_infinite_precision.price.cents.should == BigDecimal.new('100.1')
|
38
|
+
priceable_with_infinite_precision.price.currency.should == Money::Currency.find('EUR')
|
39
|
+
end
|
40
|
+
end
|
25
41
|
end
|
26
42
|
|
27
43
|
context "deserialize" do
|
data/spec/spec_helper.rb
CHANGED
data/spec/test_helpers_spec.rb
CHANGED
@@ -14,17 +14,34 @@ if defined? ActiveRecord
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "monetize matcher" do
|
17
|
-
|
18
17
|
it "matches model attribute without a '_cents' suffix by default" do
|
19
|
-
monetize(:price_cents)
|
18
|
+
product.should monetize(:price_cents)
|
20
19
|
end
|
21
20
|
|
22
21
|
it "matches model attribute specified by :as chain" do
|
23
|
-
monetize(:
|
22
|
+
product.should monetize(:discount).as(:discount_value)
|
24
23
|
end
|
25
24
|
|
26
25
|
it "matches model attribute with currency specified by :with_currency chain" do
|
27
|
-
monetize(:
|
26
|
+
product.should monetize(:bonus_cents).with_currency(:gbp)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not match non existed attribute" do
|
30
|
+
product.should_not monetize(:price_fake)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "does not match wrong currency iso" do
|
34
|
+
product.should_not monetize(:bonus_cents).with_currency(:usd)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "does not match wrong money attribute name" do
|
38
|
+
product.should_not monetize(:bonus_cents).as(:bonussss)
|
39
|
+
end
|
40
|
+
|
41
|
+
context "using subject" do
|
42
|
+
subject { product }
|
43
|
+
|
44
|
+
it { should monetize(:price_cents) }
|
28
45
|
end
|
29
46
|
end
|
30
47
|
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: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-12-31 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: money
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 6.0.0
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ~>
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version:
|
31
|
+
version: 6.0.0
|
32
32
|
- !ruby/object:Gem::Dependency
|
33
33
|
name: activesupport
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|