money-rails 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/Rakefile +5 -2
- data/lib/money-rails/test_helpers.rb +52 -19
- data/lib/money-rails/version.rb +1 -1
- data/spec/active_record/migration_extensions/schema_statements_spec.rb +11 -11
- data/spec/active_record/migration_extensions/table_spec.rb +12 -12
- data/spec/active_record/monetizable_spec.rb +65 -65
- data/spec/dummy/app/models/dummy_product.rb +0 -3
- data/spec/dummy/app/models/product.rb +0 -7
- data/spec/dummy/app/models/service.rb +0 -3
- data/spec/dummy/app/models/transaction.rb +2 -4
- data/spec/dummy/config/application.rb +0 -6
- data/spec/dummy/config/environments/development.rb +2 -9
- data/spec/dummy/config/environments/production.rb +6 -4
- data/spec/dummy/config/environments/test.rb +4 -3
- data/spec/dummy/db/schema.rb +15 -15
- data/spec/spec_helper.rb +1 -0
- data/spec/test_helpers_spec.rb +5 -1
- metadata +40 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c8b379f2b72435fdb82e4b5022a947ac6470ddb
|
4
|
+
data.tar.gz: 1a4286158053e24e3eac5da3475b014b6251518e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f82ac9627b0e16100aa8d09fae383f86bc2398f632ff32e30af6f326636da2d183f863286d62b2ece83322eecc89f311cec63649d2b061191a7a1336b0ff182b
|
7
|
+
data.tar.gz: 79a411fa93a7a6a9ccda5ad9132b32b5973a9b2f991e116b1d05a6d86f0ba3358765f1b70375aa117f795fae0d9d5fc1d95abd2398c9af9e35457810dec02af1
|
data/README.md
CHANGED
@@ -465,6 +465,11 @@ monetize(:price_cents).should be_true
|
|
465
465
|
```
|
466
466
|
This will ensure that a column called `price_cents` is being monetized.
|
467
467
|
|
468
|
+
```
|
469
|
+
monetize(:price_cents).allow_nil.should be_true
|
470
|
+
```
|
471
|
+
By using `allow_nil` you can specify money attributes that accept nil values.
|
472
|
+
|
468
473
|
```
|
469
474
|
monetize(:price_cents).as(:discount_value).should be_true
|
470
475
|
```
|
data/Rakefile
CHANGED
@@ -44,6 +44,9 @@ namespace :spec do
|
|
44
44
|
desc "Run Tests against mongoid (version 2)"
|
45
45
|
task(:mongoid2) { run_with_gemfile 'gemfiles/mongoid2.gemfile' }
|
46
46
|
|
47
|
+
desc "Run Tests against rails 4.1"
|
48
|
+
task(:rails41) { run_with_gemfile 'gemfiles/rails41.gemfile' }
|
49
|
+
|
47
50
|
desc "Run Tests against rails 4"
|
48
51
|
task(:rails4) { run_with_gemfile 'gemfiles/rails4.gemfile' }
|
49
52
|
|
@@ -53,8 +56,8 @@ namespace :spec do
|
|
53
56
|
desc "Run Tests against mongoid 2 & 3"
|
54
57
|
task :mongoid => [:mongoid2, :mongoid3]
|
55
58
|
|
56
|
-
desc "Run Tests against rails 3 & 4"
|
57
|
-
task :rails => [:rails3, :rails4]
|
59
|
+
desc "Run Tests against rails 3 & 4 & 4.1"
|
60
|
+
task :rails => [:rails3, :rails4, :rails41]
|
58
61
|
|
59
62
|
desc "Run Tests against all ORMs"
|
60
63
|
task :all => [:rails, :mongoid]
|
@@ -2,48 +2,81 @@ require 'rspec/expectations'
|
|
2
2
|
|
3
3
|
module MoneyRails
|
4
4
|
module TestHelpers
|
5
|
-
|
5
|
+
def monetize(attribute)
|
6
|
+
MonetizeMatcher.new(attribute)
|
7
|
+
end
|
6
8
|
|
7
|
-
|
9
|
+
class MonetizeMatcher
|
10
|
+
def initialize(attribute)
|
11
|
+
@attribute = attribute
|
12
|
+
end
|
8
13
|
|
9
|
-
|
14
|
+
def with_currency(currency)
|
10
15
|
@currency_iso = currency
|
16
|
+
self
|
11
17
|
end
|
12
18
|
|
13
|
-
|
19
|
+
def as(virt_attr)
|
14
20
|
@as = virt_attr
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def allow_nil
|
25
|
+
@allow_nil = true
|
26
|
+
self
|
15
27
|
end
|
16
28
|
|
17
|
-
|
29
|
+
def matches?(actual)
|
30
|
+
@actual = actual
|
31
|
+
|
32
|
+
money_attr = @as.presence || @attribute.to_s.sub(/_cents$/, "")
|
33
|
+
|
18
34
|
matched = true
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
35
|
+
|
36
|
+
if actual.respond_to?(money_attr)
|
37
|
+
if @allow_nil
|
38
|
+
matched &&= actual.send(money_attr).nil?
|
39
|
+
actual.send("#{money_attr}=", 0)
|
40
|
+
end
|
41
|
+
matched &&= actual.send(money_attr).instance_of?(Money)
|
42
|
+
|
43
|
+
if @currency_iso
|
44
|
+
matched &&= actual.send(money_attr.to_sym).currency.id == @currency_iso
|
45
|
+
end
|
46
|
+
else
|
47
|
+
matched = false
|
48
|
+
end
|
49
|
+
|
24
50
|
matched
|
25
51
|
end
|
26
52
|
|
27
|
-
description
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
53
|
+
def description
|
54
|
+
desc = "monetize #{@attribute}"
|
55
|
+
desc << " as #{@as}" if @as
|
56
|
+
desc << " with currency #{@currency_iso}" if @currency_iso
|
57
|
+
desc
|
32
58
|
end
|
33
59
|
|
34
|
-
|
35
|
-
msg = "expected that #{
|
60
|
+
def failure_message # RSpec 3.x
|
61
|
+
msg = "expected that #{@attribute} of #{@actual} would be monetized"
|
36
62
|
msg << " as #{@as}" if @as
|
37
63
|
msg << " with currency #{@currency_iso}" if @currency_iso
|
38
64
|
msg
|
39
65
|
end
|
66
|
+
alias_method :failure_message_for_should, :failure_message # RSpec 1.2, 2.x, and minitest-matchers
|
40
67
|
|
41
|
-
|
42
|
-
msg = "expected that #{
|
68
|
+
def failure_message_when_negated # RSpec 3.x
|
69
|
+
msg = "expected that #{@attribute} of #{@actual} would not be monetized"
|
43
70
|
msg << " as #{@as}" if @as
|
44
71
|
msg << " with currency #{@currency_iso}" if @currency_iso
|
45
72
|
msg
|
46
73
|
end
|
74
|
+
alias_method :failure_message_for_should_not, :failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
|
75
|
+
alias_method :negative_failure_message, :failure_message_when_negated # RSpec 1.1
|
47
76
|
end
|
48
77
|
end
|
49
78
|
end
|
79
|
+
|
80
|
+
RSpec.configure do |config|
|
81
|
+
config.include MoneyRails::TestHelpers
|
82
|
+
end
|
data/lib/money-rails/version.rb
CHANGED
@@ -36,19 +36,19 @@ if defined? ActiveRecord
|
|
36
36
|
describe 'amount' do
|
37
37
|
subject { Item.columns_hash['price_cents'] }
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
it { expect(subject.default).to eq(0) }
|
40
|
+
it { expect(subject.null).to be(false) }
|
41
|
+
it { expect(subject.type).to eq(:integer) }
|
42
42
|
end
|
43
43
|
|
44
44
|
describe 'currency' do
|
45
45
|
subject { Item.columns_hash['price_currency'] }
|
46
46
|
|
47
47
|
# set in spec/dummy/config/initializers/money.rb
|
48
|
-
|
48
|
+
it { expect(subject.default).to eq('EUR') }
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
it { expect(subject.null).to be(false) }
|
51
|
+
it { expect(subject.type).to eq(:string) }
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -61,11 +61,11 @@ if defined? ActiveRecord
|
|
61
61
|
describe 'amount' do
|
62
62
|
subject { Item.columns_hash['prefix_price_with_full_options_postfix'] }
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
it { expect(subject.default).to eq(1) }
|
65
|
+
it { expect(subject.null).to be(true) }
|
66
|
+
it { expect(subject.type).to eq(:decimal) }
|
67
|
+
it { expect(subject.precision).to eq(4) }
|
68
|
+
it { expect(subject.scale).to eq(2) }
|
69
69
|
end
|
70
70
|
|
71
71
|
describe 'currency' do
|
@@ -37,19 +37,19 @@ if defined? ActiveRecord
|
|
37
37
|
describe 'amount' do
|
38
38
|
subject { Item.columns_hash['price_cents'] }
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
it { expect(subject.default).to eq(0) }
|
41
|
+
it { expect(subject.null).to be(false) }
|
42
|
+
it { expect(subject.type).to eq(:integer) }
|
43
43
|
end
|
44
44
|
|
45
45
|
describe 'currency' do
|
46
46
|
subject { Item.columns_hash['price_currency'] }
|
47
47
|
|
48
48
|
# set in spec/dummy/config/initializers/money.rb
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
it { expect(subject.default).to eq('EUR') }
|
50
|
+
|
51
|
+
it { expect(subject.null).to be(false) }
|
52
|
+
it { expect(subject.type).to eq(:string) }
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -62,11 +62,11 @@ if defined? ActiveRecord
|
|
62
62
|
describe 'amount' do
|
63
63
|
subject { Item.columns_hash['prefix_price_with_full_options_postfix'] }
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
65
|
+
it { expect(subject.default).to eq(1) }
|
66
|
+
it { expect(subject.null).to be(true) }
|
67
|
+
it { expect(subject.type).to eq(:decimal) }
|
68
|
+
it { expect(subject.precision).to eq(4) }
|
69
|
+
it { expect(subject.scale).to eq(2) }
|
70
70
|
end
|
71
71
|
|
72
72
|
describe 'currency' do
|
@@ -31,19 +31,19 @@ if defined? ActiveRecord
|
|
31
31
|
|
32
32
|
it "assigns the correct value from a Money object" do
|
33
33
|
product.price = Money.new(3210, "USD")
|
34
|
-
product.save.should
|
34
|
+
product.save.should eq(true)
|
35
35
|
product.price_cents.should == 3210
|
36
36
|
end
|
37
37
|
|
38
38
|
it "assigns the correct value from a Money object using create" do
|
39
39
|
product = Product.create(:price => Money.new(3210, "USD"), :discount => 150,
|
40
40
|
:bonus_cents => 200, :optional_price => 100)
|
41
|
-
product.valid?.should
|
41
|
+
product.valid?.should eq(true)
|
42
42
|
product.price_cents.should == 3210
|
43
43
|
end
|
44
44
|
|
45
45
|
it "updates correctly from a Money object using update_attributes" do
|
46
|
-
product.update_attributes(:price => Money.new(215, "USD")).should
|
46
|
+
product.update_attributes(:price => Money.new(215, "USD")).should eq(true)
|
47
47
|
product.price_cents.should == 215
|
48
48
|
end
|
49
49
|
|
@@ -60,15 +60,15 @@ if defined? ActiveRecord
|
|
60
60
|
|
61
61
|
it "uses numericality validation" do
|
62
62
|
product.price_cents = "foo"
|
63
|
-
product.save.should
|
63
|
+
product.save.should eq(false)
|
64
64
|
|
65
65
|
product.price_cents = 2000
|
66
|
-
product.save.should
|
66
|
+
product.save.should eq(true)
|
67
67
|
end
|
68
68
|
|
69
69
|
it "skips numericality validation when disabled" do
|
70
70
|
product.invalid_price_cents = 'not_valid'
|
71
|
-
product.save.should
|
71
|
+
product.save.should eq(true)
|
72
72
|
end
|
73
73
|
|
74
74
|
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
@@ -87,144 +87,144 @@ if defined? ActiveRecord
|
|
87
87
|
end
|
88
88
|
|
89
89
|
it "respects numericality validation when using update_attributes" do
|
90
|
-
product.update_attributes(:price_cents => "some text").should
|
91
|
-
product.update_attributes(:price_cents => 2000).should
|
90
|
+
product.update_attributes(:price_cents => "some text").should eq(false)
|
91
|
+
product.update_attributes(:price_cents => 2000).should eq(true)
|
92
92
|
end
|
93
93
|
|
94
94
|
it "uses numericality validation on money attribute" do
|
95
95
|
product.price = "some text"
|
96
|
-
product.save.should
|
96
|
+
product.save.should eq(false)
|
97
97
|
|
98
98
|
product.price = Money.new(320, "USD")
|
99
|
-
product.save.should
|
99
|
+
product.save.should eq(true)
|
100
100
|
|
101
101
|
product.sale_price = "12.34"
|
102
102
|
product.sale_price_currency_code = 'EUR'
|
103
|
-
product.valid?.should
|
103
|
+
product.valid?.should eq(true)
|
104
104
|
end
|
105
105
|
|
106
106
|
it "fails validation with the proper error message if money value is invalid decimal" do
|
107
107
|
product.price = "12.23.24"
|
108
|
-
product.save.should
|
108
|
+
product.save.should eq(false)
|
109
109
|
product.errors[:price].first.should match(/Must be a valid/)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "fails validation with the proper error message if money value is nothing but periods" do
|
113
113
|
product.price = "..."
|
114
|
-
product.save.should
|
114
|
+
product.save.should eq(false)
|
115
115
|
product.errors[:price].first.should match(/Must be a valid/)
|
116
116
|
end
|
117
117
|
|
118
118
|
it "fails validation with the proper error message if money value has invalid thousands part" do
|
119
119
|
product.price = "12,23.24"
|
120
|
-
product.save.should
|
120
|
+
product.save.should eq(false)
|
121
121
|
product.errors[:price].first.should match(/Must be a valid/)
|
122
122
|
end
|
123
123
|
|
124
124
|
it "passes validation if money value is a Float and the currency decimal mark is not period" do
|
125
125
|
# The corresponding String would be "12,34" euros
|
126
126
|
service.discount = 12.34
|
127
|
-
service.save.should
|
127
|
+
service.save.should eq(true)
|
128
128
|
end
|
129
129
|
|
130
130
|
it "passes validation if money value is a Float" do
|
131
131
|
product.price = 12.34
|
132
|
-
product.save.should
|
132
|
+
product.save.should eq(true)
|
133
133
|
end
|
134
134
|
|
135
135
|
it "passes validation if money value is an Integer" do
|
136
136
|
product.price = 12
|
137
|
-
product.save.should
|
137
|
+
product.save.should eq(true)
|
138
138
|
end
|
139
139
|
|
140
140
|
it "fails validation with the proper error message using numericality validations" do
|
141
141
|
product.price_in_a_range = "-12"
|
142
|
-
product.valid?.should
|
142
|
+
product.valid?.should eq(false)
|
143
143
|
product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
|
144
144
|
|
145
145
|
product.price_in_a_range = Money.new(-1200, "USD")
|
146
|
-
product.valid?.should
|
146
|
+
product.valid?.should eq(false)
|
147
147
|
product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
|
148
148
|
|
149
149
|
product.price_in_a_range = "0"
|
150
|
-
product.valid?.should
|
150
|
+
product.valid?.should eq(false)
|
151
151
|
product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
|
152
152
|
|
153
153
|
product.price_in_a_range = "12"
|
154
|
-
product.valid?.should
|
154
|
+
product.valid?.should eq(true)
|
155
155
|
|
156
156
|
product.price_in_a_range = Money.new(1200, "USD")
|
157
|
-
product.valid?.should
|
157
|
+
product.valid?.should eq(true)
|
158
158
|
|
159
159
|
product.price_in_a_range = "101"
|
160
|
-
product.valid?.should
|
160
|
+
product.valid?.should eq(false)
|
161
161
|
product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
|
162
162
|
|
163
163
|
product.price_in_a_range = Money.new(10100, "USD")
|
164
|
-
product.valid?.should
|
164
|
+
product.valid?.should eq(false)
|
165
165
|
product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
|
166
166
|
end
|
167
167
|
|
168
168
|
it "fails validation with the proper error message using validates :money" do
|
169
169
|
product.validates_method_amount = "-12"
|
170
|
-
product.valid?.should
|
170
|
+
product.valid?.should eq(false)
|
171
171
|
product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
|
172
172
|
|
173
173
|
product.validates_method_amount = Money.new(-1200, "USD")
|
174
|
-
product.valid?.should
|
174
|
+
product.valid?.should eq(false)
|
175
175
|
product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
|
176
176
|
|
177
177
|
product.validates_method_amount = "0"
|
178
|
-
product.valid?.should
|
178
|
+
product.valid?.should eq(false)
|
179
179
|
product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
|
180
180
|
|
181
181
|
product.validates_method_amount = "12"
|
182
|
-
product.valid?.should
|
182
|
+
product.valid?.should eq(true)
|
183
183
|
|
184
184
|
product.validates_method_amount = Money.new(1200, "USD")
|
185
|
-
product.valid?.should
|
185
|
+
product.valid?.should eq(true)
|
186
186
|
|
187
187
|
product.validates_method_amount = "101"
|
188
|
-
product.valid?.should
|
188
|
+
product.valid?.should eq(false)
|
189
189
|
product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
|
190
190
|
|
191
191
|
product.validates_method_amount = Money.new(10100, "USD")
|
192
|
-
product.valid?.should
|
192
|
+
product.valid?.should eq(false)
|
193
193
|
product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
|
194
194
|
end
|
195
195
|
|
196
196
|
it "fails validation with the proper error message on the cents field " do
|
197
197
|
product.price_in_a_range = "-12"
|
198
|
-
product.valid?.should
|
198
|
+
product.valid?.should eq(false)
|
199
199
|
product.errors[:price_in_a_range_cents].first.should match(/greater than 0/)
|
200
200
|
|
201
201
|
product.price_in_a_range = "0"
|
202
|
-
product.valid?.should
|
202
|
+
product.valid?.should eq(false)
|
203
203
|
product.errors[:price_in_a_range_cents].first.should match(/greater than 0/)
|
204
204
|
|
205
205
|
product.price_in_a_range = "12"
|
206
|
-
product.valid?.should
|
206
|
+
product.valid?.should eq(true)
|
207
207
|
|
208
208
|
product.price_in_a_range = "101"
|
209
|
-
product.valid?.should
|
209
|
+
product.valid?.should eq(false)
|
210
210
|
product.errors[:price_in_a_range_cents].first.should match(/less than or equal to 10000/)
|
211
211
|
end
|
212
212
|
|
213
213
|
it "fails validation when a non number string is given" do
|
214
214
|
product = Product.create(:price_in_a_range => "asd")
|
215
|
-
product.valid?.should
|
215
|
+
product.valid?.should eq(false)
|
216
216
|
product.errors[:price_in_a_range].first.should match(/greater than zero/)
|
217
217
|
|
218
218
|
product = Product.create(:price_in_a_range => "asd23")
|
219
|
-
product.valid?.should
|
219
|
+
product.valid?.should eq(false)
|
220
220
|
product.errors[:price_in_a_range].first.should match(/greater than zero/)
|
221
221
|
|
222
222
|
product = Product.create(:price => "asd")
|
223
|
-
product.valid?.should
|
223
|
+
product.valid?.should eq(false)
|
224
224
|
product.errors[:price].first.should match(/is not a number/)
|
225
225
|
|
226
226
|
product = Product.create(:price => "asd23")
|
227
|
-
product.valid?.should
|
227
|
+
product.valid?.should eq(false)
|
228
228
|
product.errors[:price].first.should match(/is not a number/)
|
229
229
|
end
|
230
230
|
|
@@ -242,17 +242,17 @@ if defined? ActiveRecord
|
|
242
242
|
|
243
243
|
it "passes validation if money value has correct format" do
|
244
244
|
product.price = "12,230.24"
|
245
|
-
product.save.should
|
245
|
+
product.save.should eq(true)
|
246
246
|
end
|
247
247
|
|
248
248
|
it "passes validation if there is a whitespace between the currency symbol and amount" do
|
249
249
|
product.price = "$ 123,456.78"
|
250
|
-
product.save.should
|
250
|
+
product.save.should eq(true)
|
251
251
|
end
|
252
252
|
|
253
253
|
it "respects numericality validation when using update_attributes on money attribute" do
|
254
|
-
product.update_attributes(:price => "some text").should
|
255
|
-
product.update_attributes(:price => Money.new(320, 'USD')).should
|
254
|
+
product.update_attributes(:price => "some text").should eq(false)
|
255
|
+
product.update_attributes(:price => Money.new(320, 'USD')).should eq(true)
|
256
256
|
end
|
257
257
|
|
258
258
|
it "uses i18n currency format when validating" do
|
@@ -263,7 +263,7 @@ if defined? ActiveRecord
|
|
263
263
|
"12.00".to_money.should == Money.new(1200, :eur)
|
264
264
|
transaction = Transaction.new(amount: "12.00", tax: "13.00")
|
265
265
|
transaction.amount_cents.should == 1200
|
266
|
-
transaction.valid?.should
|
266
|
+
transaction.valid?.should eq(true)
|
267
267
|
|
268
268
|
# reset locale setting
|
269
269
|
I18n.locale = old_locale
|
@@ -277,7 +277,7 @@ if defined? ActiveRecord
|
|
277
277
|
"12,00".to_money.should == Money.new(1200, :eur)
|
278
278
|
transaction = Transaction.new(amount: "12,00", tax: "13,00")
|
279
279
|
transaction.amount_cents.should == 1200
|
280
|
-
transaction.valid?.should
|
280
|
+
transaction.valid?.should eq(true)
|
281
281
|
|
282
282
|
# reset locale setting
|
283
283
|
I18n.locale = old_locale
|
@@ -285,12 +285,12 @@ if defined? ActiveRecord
|
|
285
285
|
|
286
286
|
it "doesn't allow nil by default" do
|
287
287
|
product.price_cents = nil
|
288
|
-
product.save.should
|
288
|
+
product.save.should eq(false)
|
289
289
|
end
|
290
290
|
|
291
291
|
it "allows nil if optioned" do
|
292
292
|
product.optional_price = nil
|
293
|
-
product.save.should
|
293
|
+
product.save.should eq(true)
|
294
294
|
product.optional_price.should be_nil
|
295
295
|
end
|
296
296
|
|
@@ -319,7 +319,7 @@ if defined? ActiveRecord
|
|
319
319
|
it "does not reset money_before_type_cast attr if save operation fails" do
|
320
320
|
product.bonus = ""
|
321
321
|
product.bonus_money_before_type_cast.should == ""
|
322
|
-
product.save.should
|
322
|
+
product.save.should eq(false)
|
323
323
|
product.bonus_money_before_type_cast.should == ""
|
324
324
|
end
|
325
325
|
|
@@ -338,90 +338,90 @@ if defined? ActiveRecord
|
|
338
338
|
|
339
339
|
it "assigns correctly Money objects to the attribute" do
|
340
340
|
product.price = Money.new(2500, :USD)
|
341
|
-
product.save.should
|
341
|
+
product.save.should eq(true)
|
342
342
|
product.price.cents.should == 2500
|
343
343
|
product.price.currency_as_string.should == "USD"
|
344
344
|
end
|
345
345
|
|
346
346
|
it "assigns correctly Fixnum objects to the attribute" do
|
347
347
|
product.price = 25
|
348
|
-
product.save.should
|
348
|
+
product.save.should eq(true)
|
349
349
|
product.price.cents.should == 2500
|
350
350
|
product.price.currency_as_string.should == "USD"
|
351
351
|
|
352
352
|
service.discount = 2
|
353
|
-
service.save.should
|
353
|
+
service.save.should eq(true)
|
354
354
|
service.discount.cents.should == 200
|
355
355
|
service.discount.currency_as_string.should == "EUR"
|
356
356
|
end
|
357
357
|
|
358
358
|
it "assigns correctly String objects to the attribute" do
|
359
359
|
product.price = "25"
|
360
|
-
product.save.should
|
360
|
+
product.save.should eq(true)
|
361
361
|
product.price.cents.should == 2500
|
362
362
|
product.price.currency_as_string.should == "USD"
|
363
363
|
|
364
364
|
service.discount = "2"
|
365
|
-
service.save.should
|
365
|
+
service.save.should eq(true)
|
366
366
|
service.discount.cents.should == 200
|
367
367
|
service.discount.currency_as_string.should == "EUR"
|
368
368
|
end
|
369
369
|
|
370
370
|
it "overrides default, model currency with the value of :with_currency in fixnum assignments" do
|
371
371
|
product.bonus = 25
|
372
|
-
product.save.should
|
372
|
+
product.save.should eq(true)
|
373
373
|
product.bonus.cents.should == 2500
|
374
374
|
product.bonus.currency_as_string.should == "GBP"
|
375
375
|
|
376
376
|
service.charge = 2
|
377
|
-
service.save.should
|
377
|
+
service.save.should eq(true)
|
378
378
|
service.charge.cents.should == 200
|
379
379
|
service.charge.currency_as_string.should == "USD"
|
380
380
|
end
|
381
381
|
|
382
382
|
it "overrides default, model currency with the value of :with_currency in string assignments" do
|
383
383
|
product.bonus = "25"
|
384
|
-
product.save.should
|
384
|
+
product.save.should eq(true)
|
385
385
|
product.bonus.cents.should == 2500
|
386
386
|
product.bonus.currency_as_string.should == "GBP"
|
387
387
|
|
388
388
|
service.charge = "2"
|
389
|
-
service.save.should
|
389
|
+
service.save.should eq(true)
|
390
390
|
service.charge.cents.should == 200
|
391
391
|
service.charge.currency_as_string.should == "USD"
|
392
392
|
end
|
393
393
|
|
394
394
|
it "overrides default currency with model currency, in fixnum assignments" do
|
395
395
|
product.discount_value = 5
|
396
|
-
product.save.should
|
396
|
+
product.save.should eq(true)
|
397
397
|
product.discount_value.cents.should == 500
|
398
398
|
product.discount_value.currency_as_string.should == "USD"
|
399
399
|
end
|
400
400
|
|
401
401
|
it "overrides default currency with model currency, in string assignments" do
|
402
402
|
product.discount_value = "5"
|
403
|
-
product.save.should
|
403
|
+
product.save.should eq(true)
|
404
404
|
product.discount_value.cents.should == 500
|
405
405
|
product.discount_value.currency_as_string.should == "USD"
|
406
406
|
end
|
407
407
|
|
408
408
|
it "falls back to default currency, in fixnum assignments" do
|
409
409
|
service.discount = 5
|
410
|
-
service.save.should
|
410
|
+
service.save.should eq(true)
|
411
411
|
service.discount.cents.should == 500
|
412
412
|
service.discount.currency_as_string.should == "EUR"
|
413
413
|
end
|
414
414
|
|
415
415
|
it "falls back to default currency, in string assignments" do
|
416
416
|
service.discount = "5"
|
417
|
-
service.save.should
|
417
|
+
service.save.should eq(true)
|
418
418
|
service.discount.cents.should == 500
|
419
419
|
service.discount.currency_as_string.should == "EUR"
|
420
420
|
end
|
421
421
|
|
422
422
|
it "sets field to nil, in nil assignments if allow_nil is set" do
|
423
423
|
product.optional_price = nil
|
424
|
-
product.save.should
|
424
|
+
product.save.should eq(true)
|
425
425
|
product.optional_price.should be_nil
|
426
426
|
end
|
427
427
|
|
@@ -429,13 +429,13 @@ if defined? ActiveRecord
|
|
429
429
|
pr = Product.new(:optional_price => nil, :price_cents => 5320,
|
430
430
|
:discount => 350, :bonus_cents => 320)
|
431
431
|
pr.optional_price.should be_nil
|
432
|
-
pr.save.should
|
432
|
+
pr.save.should eq(true)
|
433
433
|
pr.optional_price.should be_nil
|
434
434
|
end
|
435
435
|
|
436
436
|
it "sets field to nil, in blank assignments if allow_nil is set" do
|
437
437
|
product.optional_price = ""
|
438
|
-
product.save.should
|
438
|
+
product.save.should eq(true)
|
439
439
|
product.optional_price.should be_nil
|
440
440
|
end
|
441
441
|
|
@@ -526,7 +526,7 @@ if defined? ActiveRecord
|
|
526
526
|
|
527
527
|
it "assigns correctly Money objects to the attribute" do
|
528
528
|
transaction.amount = Money.new(2500, :eur)
|
529
|
-
transaction.save.should
|
529
|
+
transaction.save.should eq(true)
|
530
530
|
transaction.amount.cents.should == Money.new(2500, :eur).cents
|
531
531
|
transaction.amount.currency_as_string.should == "EUR"
|
532
532
|
end
|
@@ -1,11 +1,4 @@
|
|
1
1
|
class Product < ActiveRecord::Base
|
2
|
-
|
3
|
-
attr_accessible :price_cents, :discount, :bonus_cents,
|
4
|
-
:price, :discount_value, :bonus, :optional_price_cents, :optional_price,
|
5
|
-
:sale_price, :sale_price_amount, :sale_price_currency_code,
|
6
|
-
:price_in_a_range_cents, :price_in_a_range, :invalid_price_cents,
|
7
|
-
:validates_method_amount, :validates_method_amount_cents
|
8
|
-
|
9
2
|
# Use USD as model level currency
|
10
3
|
register_currency :usd
|
11
4
|
|
@@ -1,13 +1,11 @@
|
|
1
1
|
class Transaction < ActiveRecord::Base
|
2
|
-
|
3
|
-
attr_accessible :amount_cents, :currency, :tax_cents, :amount, :tax
|
4
|
-
|
5
2
|
monetize :amount_cents, :with_model_currency => :currency
|
3
|
+
|
6
4
|
monetize :tax_cents, :with_model_currency => :currency
|
7
5
|
|
8
6
|
monetize :total_cents, :with_model_currency => :currency
|
7
|
+
|
9
8
|
def total_cents
|
10
9
|
return amount_cents + tax_cents
|
11
10
|
end
|
12
|
-
|
13
11
|
end
|
@@ -52,12 +52,6 @@ module Dummy
|
|
52
52
|
# like if you have constraints or database-specific column types
|
53
53
|
# config.active_record.schema_format = :sql
|
54
54
|
|
55
|
-
# Enforce whitelist mode for mass assignment.
|
56
|
-
# This will create an empty whitelist of attributes available for mass-assignment for all models
|
57
|
-
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
|
58
|
-
# parameters by using an attr_accessible or attr_protected declaration.
|
59
|
-
config.active_record.whitelist_attributes = true
|
60
|
-
|
61
55
|
# Enable the asset pipeline
|
62
56
|
config.assets.enabled = true
|
63
57
|
|
@@ -6,8 +6,8 @@ Dummy::Application.configure do
|
|
6
6
|
# since you don't have to restart the web server when you make code changes.
|
7
7
|
config.cache_classes = false
|
8
8
|
|
9
|
-
#
|
10
|
-
config.
|
9
|
+
# Do not eager load code on boot.
|
10
|
+
config.eager_load = false
|
11
11
|
|
12
12
|
# Show full error reports and disable caching
|
13
13
|
config.consider_all_requests_local = true
|
@@ -22,13 +22,6 @@ Dummy::Application.configure do
|
|
22
22
|
# Only use best-standards-support built into browsers
|
23
23
|
config.action_dispatch.best_standards_support = :builtin
|
24
24
|
|
25
|
-
# Raise exception on mass assignment protection for Active Record models
|
26
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
27
|
-
|
28
|
-
# Log the query plan for queries taking more than this (works
|
29
|
-
# with SQLite, MySQL, and PostgreSQL)
|
30
|
-
config.active_record.auto_explain_threshold_in_seconds = 0.5
|
31
|
-
|
32
25
|
# Do not compress assets
|
33
26
|
config.assets.compress = false
|
34
27
|
|
@@ -4,6 +4,12 @@ Dummy::Application.configure do
|
|
4
4
|
# Code is not reloaded between requests
|
5
5
|
config.cache_classes = true
|
6
6
|
|
7
|
+
# Eager load code on boot. This eager loads most of Rails and
|
8
|
+
# your application in memory, allowing both threaded web servers
|
9
|
+
# and those relying on copy on write to perform better.
|
10
|
+
# Rake tasks automatically ignore this option for performance.
|
11
|
+
config.eager_load = true
|
12
|
+
|
7
13
|
# Full error reports are disabled and caching is turned on
|
8
14
|
config.consider_all_requests_local = false
|
9
15
|
config.action_controller.perform_caching = true
|
@@ -60,8 +66,4 @@ Dummy::Application.configure do
|
|
60
66
|
|
61
67
|
# Send deprecation notices to registered listeners
|
62
68
|
config.active_support.deprecation = :notify
|
63
|
-
|
64
|
-
# Log the query plan for queries taking more than this (works
|
65
|
-
# with SQLite, MySQL, and PostgreSQL)
|
66
|
-
# config.active_record.auto_explain_threshold_in_seconds = 0.5
|
67
69
|
end
|
@@ -7,7 +7,11 @@ Dummy::Application.configure do
|
|
7
7
|
# and recreated between test runs. Don't rely on the data there!
|
8
8
|
config.cache_classes = true
|
9
9
|
|
10
|
+
# Do not eager load code on boot. This avoids loading your whole application
|
11
|
+
# just for the purpose of running a single test. If you are using a tool that
|
12
|
+
# preloads Rails for running tests, you may have to set it to true.
|
10
13
|
config.eager_load = false
|
14
|
+
|
11
15
|
# Configure static asset server for tests with Cache-Control for performance
|
12
16
|
config.serve_static_assets = true
|
13
17
|
config.static_cache_control = "public, max-age=3600"
|
@@ -27,9 +31,6 @@ Dummy::Application.configure do
|
|
27
31
|
# ActionMailer::Base.deliveries array.
|
28
32
|
config.action_mailer.delivery_method = :test
|
29
33
|
|
30
|
-
# Raise exception on mass assignment protection for Active Record models
|
31
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
32
|
-
|
33
34
|
# Print deprecation notices to the stderr
|
34
35
|
config.active_support.deprecation = :stderr
|
35
36
|
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -9,43 +9,43 @@
|
|
9
9
|
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
10
|
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
11
|
#
|
12
|
-
# It's strongly recommended
|
12
|
+
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version
|
14
|
+
ActiveRecord::Schema.define(:version => 20140110194016) do
|
15
15
|
|
16
|
-
create_table "dummy_products", force
|
16
|
+
create_table "dummy_products", :force => true do |t|
|
17
17
|
t.string "currency"
|
18
18
|
t.integer "price_cents"
|
19
|
-
t.datetime "created_at"
|
20
|
-
t.datetime "updated_at"
|
19
|
+
t.datetime "created_at", :null => false
|
20
|
+
t.datetime "updated_at", :null => false
|
21
21
|
end
|
22
22
|
|
23
|
-
create_table "products", force
|
23
|
+
create_table "products", :force => true do |t|
|
24
24
|
t.integer "price_cents"
|
25
25
|
t.integer "discount"
|
26
|
-
t.datetime "created_at"
|
27
|
-
t.datetime "updated_at"
|
26
|
+
t.datetime "created_at", :null => false
|
27
|
+
t.datetime "updated_at", :null => false
|
28
28
|
t.integer "bonus_cents"
|
29
29
|
t.integer "optional_price_cents"
|
30
|
-
t.integer "sale_price_amount", default
|
30
|
+
t.integer "sale_price_amount", :default => 0, :null => false
|
31
31
|
t.string "sale_price_currency_code"
|
32
32
|
t.integer "price_in_a_range_cents"
|
33
33
|
t.integer "validates_method_amount_cents"
|
34
34
|
end
|
35
35
|
|
36
|
-
create_table "services", force
|
36
|
+
create_table "services", :force => true do |t|
|
37
37
|
t.integer "charge_cents"
|
38
38
|
t.integer "discount_cents"
|
39
|
-
t.datetime "created_at"
|
40
|
-
t.datetime "updated_at"
|
39
|
+
t.datetime "created_at", :null => false
|
40
|
+
t.datetime "updated_at", :null => false
|
41
41
|
end
|
42
42
|
|
43
|
-
create_table "transactions", force
|
43
|
+
create_table "transactions", :force => true do |t|
|
44
44
|
t.integer "amount_cents"
|
45
45
|
t.integer "tax_cents"
|
46
46
|
t.string "currency"
|
47
|
-
t.datetime "created_at"
|
48
|
-
t.datetime "updated_at"
|
47
|
+
t.datetime "created_at", :null => false
|
48
|
+
t.datetime "updated_at", :null => false
|
49
49
|
end
|
50
50
|
|
51
51
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -20,5 +20,6 @@ RSpec.configure do |config|
|
|
20
20
|
# automatically. This will be the default behavior in future versions of
|
21
21
|
# rspec-rails.
|
22
22
|
config.infer_base_class_for_anonymous_controllers = false
|
23
|
+
config.infer_spec_type_from_file_location!
|
23
24
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
24
25
|
end
|
data/spec/test_helpers_spec.rb
CHANGED
@@ -9,7 +9,7 @@ if defined? ActiveRecord
|
|
9
9
|
|
10
10
|
let(:product) do
|
11
11
|
Product.create(:price_cents => 3000, :discount => 150,
|
12
|
-
:bonus_cents => 200,
|
12
|
+
:bonus_cents => 200,
|
13
13
|
:sale_price_amount => 1200)
|
14
14
|
end
|
15
15
|
|
@@ -22,6 +22,10 @@ if defined? ActiveRecord
|
|
22
22
|
product.should monetize(:discount).as(:discount_value)
|
23
23
|
end
|
24
24
|
|
25
|
+
it "matches model attribute with nil value specified by :allow_nil chain" do
|
26
|
+
product.should monetize(:optional_price).allow_nil
|
27
|
+
end
|
28
|
+
|
25
29
|
it "matches model attribute with currency specified by :with_currency chain" do
|
26
30
|
product.should monetize(:bonus_cents).with_currency(:gbp)
|
27
31
|
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.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Loupasakis
|
@@ -10,106 +10,106 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
+
prerelease: false
|
16
17
|
name: money
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.1.1
|
17
23
|
requirement: !ruby/object:Gem::Requirement
|
18
24
|
requirements:
|
19
|
-
- -
|
25
|
+
- - ~>
|
20
26
|
- !ruby/object:Gem::Version
|
21
27
|
version: 6.1.1
|
22
28
|
type: :runtime
|
29
|
+
- !ruby/object:Gem::Dependency
|
23
30
|
prerelease: false
|
31
|
+
name: monetize
|
24
32
|
version_requirements: !ruby/object:Gem::Requirement
|
25
33
|
requirements:
|
26
|
-
- -
|
34
|
+
- - ~>
|
27
35
|
- !ruby/object:Gem::Version
|
28
|
-
version:
|
29
|
-
- !ruby/object:Gem::Dependency
|
30
|
-
name: monetize
|
36
|
+
version: 0.3.0
|
31
37
|
requirement: !ruby/object:Gem::Requirement
|
32
38
|
requirements:
|
33
|
-
- -
|
39
|
+
- - ~>
|
34
40
|
- !ruby/object:Gem::Version
|
35
41
|
version: 0.3.0
|
36
42
|
type: :runtime
|
43
|
+
- !ruby/object:Gem::Dependency
|
37
44
|
prerelease: false
|
45
|
+
name: activesupport
|
38
46
|
version_requirements: !ruby/object:Gem::Requirement
|
39
47
|
requirements:
|
40
|
-
- -
|
48
|
+
- - '>='
|
41
49
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
43
|
-
- !ruby/object:Gem::Dependency
|
44
|
-
name: activesupport
|
50
|
+
version: '3.0'
|
45
51
|
requirement: !ruby/object:Gem::Requirement
|
46
52
|
requirements:
|
47
|
-
- -
|
53
|
+
- - '>='
|
48
54
|
- !ruby/object:Gem::Version
|
49
55
|
version: '3.0'
|
50
56
|
type: :runtime
|
57
|
+
- !ruby/object:Gem::Dependency
|
51
58
|
prerelease: false
|
59
|
+
name: railties
|
52
60
|
version_requirements: !ruby/object:Gem::Requirement
|
53
61
|
requirements:
|
54
|
-
- -
|
62
|
+
- - '>='
|
55
63
|
- !ruby/object:Gem::Version
|
56
64
|
version: '3.0'
|
57
|
-
- !ruby/object:Gem::Dependency
|
58
|
-
name: railties
|
59
65
|
requirement: !ruby/object:Gem::Requirement
|
60
66
|
requirements:
|
61
|
-
- -
|
67
|
+
- - '>='
|
62
68
|
- !ruby/object:Gem::Version
|
63
69
|
version: '3.0'
|
64
70
|
type: :runtime
|
71
|
+
- !ruby/object:Gem::Dependency
|
65
72
|
prerelease: false
|
73
|
+
name: rails
|
66
74
|
version_requirements: !ruby/object:Gem::Requirement
|
67
75
|
requirements:
|
68
|
-
- -
|
76
|
+
- - '>='
|
69
77
|
- !ruby/object:Gem::Version
|
70
78
|
version: '3.0'
|
71
|
-
- !ruby/object:Gem::Dependency
|
72
|
-
name: rails
|
73
79
|
requirement: !ruby/object:Gem::Requirement
|
74
80
|
requirements:
|
75
|
-
- -
|
81
|
+
- - '>='
|
76
82
|
- !ruby/object:Gem::Version
|
77
83
|
version: '3.0'
|
78
84
|
type: :development
|
85
|
+
- !ruby/object:Gem::Dependency
|
79
86
|
prerelease: false
|
87
|
+
name: rspec-rails
|
80
88
|
version_requirements: !ruby/object:Gem::Requirement
|
81
89
|
requirements:
|
82
|
-
- -
|
90
|
+
- - ~>
|
83
91
|
- !ruby/object:Gem::Version
|
84
|
-
version: '
|
85
|
-
- !ruby/object:Gem::Dependency
|
86
|
-
name: rspec-rails
|
92
|
+
version: '2.10'
|
87
93
|
requirement: !ruby/object:Gem::Requirement
|
88
94
|
requirements:
|
89
|
-
- -
|
95
|
+
- - ~>
|
90
96
|
- !ruby/object:Gem::Version
|
91
97
|
version: '2.10'
|
92
98
|
type: :development
|
99
|
+
- !ruby/object:Gem::Dependency
|
93
100
|
prerelease: false
|
101
|
+
name: database_cleaner
|
94
102
|
version_requirements: !ruby/object:Gem::Requirement
|
95
103
|
requirements:
|
96
|
-
- -
|
104
|
+
- - '>='
|
97
105
|
- !ruby/object:Gem::Version
|
98
|
-
version:
|
99
|
-
- !ruby/object:Gem::Dependency
|
100
|
-
name: database_cleaner
|
106
|
+
version: 0.8.0
|
101
107
|
requirement: !ruby/object:Gem::Requirement
|
102
108
|
requirements:
|
103
|
-
- -
|
109
|
+
- - '>='
|
104
110
|
- !ruby/object:Gem::Version
|
105
111
|
version: 0.8.0
|
106
112
|
type: :development
|
107
|
-
prerelease: false
|
108
|
-
version_requirements: !ruby/object:Gem::Requirement
|
109
|
-
requirements:
|
110
|
-
- - ">="
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
version: 0.8.0
|
113
113
|
description: This library provides integration of RubyMoney - Money gem with Rails
|
114
114
|
email:
|
115
115
|
- alup.rubymoney@gmail.com
|
@@ -209,12 +209,12 @@ require_paths:
|
|
209
209
|
- lib
|
210
210
|
required_ruby_version: !ruby/object:Gem::Requirement
|
211
211
|
requirements:
|
212
|
-
- -
|
212
|
+
- - '>='
|
213
213
|
- !ruby/object:Gem::Version
|
214
214
|
version: '0'
|
215
215
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
216
216
|
requirements:
|
217
|
-
- -
|
217
|
+
- - '>='
|
218
218
|
- !ruby/object:Gem::Version
|
219
219
|
version: '0'
|
220
220
|
requirements: []
|
@@ -282,3 +282,4 @@ test_files:
|
|
282
282
|
- spec/spec_helper.rb
|
283
283
|
- spec/support/database_cleaner.rb
|
284
284
|
- spec/test_helpers_spec.rb
|
285
|
+
has_rdoc:
|