mongoid_money_field 4.0.8 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -1
- data/.ruby-version +1 -1
- data/.travis.yml +6 -5
- data/Gemfile +2 -0
- data/README.rdoc +5 -3
- data/gemfiles/money51-mongoid31.gemfile +6 -0
- data/gemfiles/money51-mongoid4.gemfile +6 -0
- data/gemfiles/money6-mongoid31.gemfile +6 -0
- data/gemfiles/money6-mongoid4.gemfile +6 -0
- data/lib/mongoid_money_field.rb +10 -162
- data/lib/mongoid_money_field/field.rb +1 -1
- data/lib/mongoid_money_field/type.rb +82 -0
- data/lib/mongoid_money_field/version.rb +1 -1
- data/mongoid_money_field.gemspec +8 -10
- data/spec/money_spec.rb +106 -107
- data/spec/ruby_money_spec.rb +15 -15
- data/spec/support/dummy_money.rb +1 -1
- data/spec/support/dummy_money_required.rb +9 -9
- data/spec/support/dummy_money_with_default.rb +9 -9
- data/spec/support/money3.rb +1 -1
- metadata +50 -72
- data/lib/mongoid_money_field/rails_admin.rb +0 -46
- data/spec/model_spec.rb +0 -8
- data/spec/money3_compat_spec.rb +0 -123
- data/spec/money3_spec.rb +0 -342
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9872f92a8e94961a24bfdc1555fec6dd83708eca
|
4
|
+
data.tar.gz: db2c1d912130b1f54bfbe4832c22a313f6515d4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b8205c61b1aa83a20809933df9cb4cf0dfe2976c922b7c85b47cf5d145d3abdc96e8ad5ea3eb4acd4cad15579a95f7f3c0e2bb7155f2bd5291aa1faf2458c94
|
7
|
+
data.tar.gz: bfdc740e907c52fe3aa3179124c06bdcd049eddd3ae801ede55c4bd64a41c38525b10c613c0777df85690df859e39dcb9a1dd64897403b7b65e7dadb15c6e953
|
data/.ruby-gemset
CHANGED
@@ -1 +1 @@
|
|
1
|
-
moneyfield
|
1
|
+
moneyfield
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.0.0-p247
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -19,9 +19,11 @@ Inspired by https://gist.github.com/840500
|
|
19
19
|
|
20
20
|
https://github.com/RubyMoney/money-rails
|
21
21
|
|
22
|
-
|
22
|
+
Since v4 storage format of mongoid money field is identical to that of money-rails
|
23
23
|
|
24
|
-
==
|
24
|
+
== Changelog
|
25
|
+
|
26
|
+
v5 drops compatibility with v3 storage format. Please use v4 until you upgraded your DB.
|
25
27
|
|
26
28
|
v4 is a massive refactor. Storage format is now a hash, like money-rails, instead of two separate cents and currency fields
|
27
29
|
Old values should be still accessible after you update. To migrate to new storage format do:
|
@@ -41,7 +43,7 @@ please avoid using v2, i realized that API was stupid after commiting it.
|
|
41
43
|
|
42
44
|
Include the gem in your Gemfile
|
43
45
|
|
44
|
-
gem 'mongoid_money_field'
|
46
|
+
gem 'mongoid_money_field', '~> 5.0.0'
|
45
47
|
|
46
48
|
== Usage
|
47
49
|
|
data/lib/mongoid_money_field.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# coding: utf-8
|
2
2
|
|
3
3
|
require "money"
|
4
|
-
|
5
|
-
require
|
4
|
+
|
5
|
+
require "mongoid_money_field/type"
|
6
6
|
require "mongoid_money_field/version"
|
7
7
|
require "mongoid_money_field/field"
|
8
8
|
|
@@ -12,172 +12,20 @@ module Mongoid
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
def money_field(*columns)
|
15
|
-
opts = columns.last.is_a?(
|
16
|
-
|
17
|
-
fixed_currency: nil,
|
18
|
-
default: nil,
|
19
|
-
required: false,
|
20
|
-
default_currency: nil
|
21
|
-
}.merge( opts )
|
22
|
-
|
23
|
-
ensure_default = Proc.new do |currency|
|
24
|
-
if opts[:fixed_currency].nil?
|
25
|
-
if currency.nil?
|
26
|
-
if opts[:default_currency].nil?
|
27
|
-
Money.default_currency
|
28
|
-
else
|
29
|
-
opts[:default_currency]
|
30
|
-
end
|
31
|
-
else
|
32
|
-
currency
|
33
|
-
end
|
34
|
-
else
|
35
|
-
opts[:fixed_currency]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
15
|
+
opts = columns.last.is_a?(Hash) ? columns.pop : {}
|
16
|
+
|
39
17
|
[columns].flatten.each do |name|
|
40
|
-
default
|
41
|
-
|
42
|
-
|
43
|
-
default = Monetize.parse(opts[:default])
|
44
|
-
end
|
45
|
-
|
46
|
-
field name, type: Money, default: default
|
47
|
-
validates_presence_of name if opts[:required]
|
48
|
-
|
49
|
-
define_method("#{name}=") do |value|
|
50
|
-
instance_variable_set( "@#{name}_before_type_cast".to_sym, value)
|
51
|
-
|
52
|
-
if value.blank?
|
53
|
-
write_attribute(name, nil)
|
54
|
-
else
|
55
|
-
if opts[:default_currency].nil?
|
56
|
-
if value.is_a?(Hash)
|
57
|
-
money = Money.new(value['cents'], value['currency_iso'])
|
58
|
-
else
|
59
|
-
money = value.to_money
|
60
|
-
end
|
61
|
-
else
|
62
|
-
old_default = Money.default_currency
|
63
|
-
Money.default_currency = Money::Currency.new(opts[:default_currency])
|
64
|
-
money = value.to_money
|
65
|
-
Money.default_currency = old_default
|
66
|
-
end
|
67
|
-
|
68
|
-
unless opts[:fixed_currency].nil?
|
69
|
-
money = Money.new(money.cents, opts[:fixed_currency])
|
70
|
-
end
|
71
|
-
|
72
|
-
write_attribute(name, money)
|
73
|
-
end
|
74
|
-
remove_attribute("#{name}_currency")
|
75
|
-
remove_attribute("#{name}_cents")
|
76
|
-
end
|
77
|
-
|
78
|
-
# mongoid money field 2 compat
|
79
|
-
define_method(name) do
|
80
|
-
if read_attribute("#{name}").nil? && !read_attribute("#{name}_cents").nil?
|
81
|
-
currency = read_attribute("#{name}_currency")
|
82
|
-
currency = ensure_default.call(currency)
|
83
|
-
Money.new(read_attribute("#{name}_cents"), currency)
|
84
|
-
else
|
85
|
-
value = read_attribute(name)
|
86
|
-
if value.nil?
|
87
|
-
nil
|
88
|
-
else
|
89
|
-
if value.is_a?(Hash)
|
90
|
-
value[:currency_iso] = ensure_default.call(value[:currency_iso])
|
91
|
-
end
|
92
|
-
Money.demongoize(value)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
define_method("#{name}_before_type_cast") do
|
98
|
-
instance_variable_get( "@#{name}_before_type_cast".to_sym) || send(name).to_s
|
99
|
-
end
|
100
|
-
|
101
|
-
# deprecated
|
102
|
-
define_method("migrate_#{name}_from_money_3!") do
|
103
|
-
if read_attribute(name).nil?
|
104
|
-
cents = read_attribute("#{name}_cents")
|
105
|
-
if cents.nil?
|
106
|
-
send("#{name}=", nil)
|
107
|
-
else
|
108
|
-
currency = read_attribute("#{name}_currency")
|
109
|
-
|
110
|
-
if currency.nil?
|
111
|
-
if opts[:default_currency].nil?
|
112
|
-
currency = Money.default_currency
|
113
|
-
else
|
114
|
-
currency = opts[:default_currency]
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
unless opts[:fixed_currency].nil?
|
119
|
-
currency = opts[:fixed_currency]
|
120
|
-
end
|
121
|
-
send("#{name}=", Money.new(cents, currency))
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
# deprecated
|
127
|
-
define_method("#{name}_cents") do
|
128
|
-
send(name).nil? ? 0 : send(name).cents
|
129
|
-
end
|
130
|
-
|
131
|
-
# deprecated
|
132
|
-
define_method("#{name}_currency") do
|
133
|
-
if opts[:fixed_currency].nil?
|
134
|
-
send(name).nil? ? Money.default_currency : send(name).currency.iso_code
|
135
|
-
else
|
136
|
-
opts[:fixed_currency]
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
# deprecated
|
141
|
-
define_method("#{name}_cents=") do |val|
|
142
|
-
send("#{name}=", Money.new(val, send("#{name}_currency")))
|
143
|
-
end
|
144
|
-
|
145
|
-
# deprecated
|
146
|
-
define_method("#{name}_currency=") do |val|
|
147
|
-
send("#{name}=", Money.new(send("#{name}_cents"), val))
|
148
|
-
end
|
149
|
-
|
150
|
-
# deprecated
|
151
|
-
define_method("#{name}_plain=") do |val|
|
152
|
-
send("#{name}=", val)
|
153
|
-
end
|
154
|
-
# deprecated
|
155
|
-
define_method("#{name}_plain") do |val|
|
156
|
-
send("#{name}")
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
def migrate_from_money_field_3!(*columns)
|
162
|
-
each do |val|
|
163
|
-
[columns].flatten.each do |name|
|
164
|
-
val.send("migrate_#{name.to_s}_from_money_3!")
|
165
|
-
if val.save
|
166
|
-
else
|
167
|
-
puts "Error: model failed validation and needs to be updated manually:"
|
168
|
-
p val
|
169
|
-
end
|
18
|
+
field name, type: MoneyType.new(opts), default: opts[:default]
|
19
|
+
if opts[:required]
|
20
|
+
validates_presence_of name
|
170
21
|
end
|
171
22
|
end
|
172
23
|
end
|
173
24
|
end
|
25
|
+
|
174
26
|
end
|
175
27
|
end
|
176
28
|
|
177
29
|
if Object.const_defined?("SimpleForm")
|
178
30
|
require "mongoid_money_field/simple_form/money_input"
|
179
|
-
end
|
180
|
-
|
181
|
-
if Object.const_defined?("RailsAdmin")
|
182
|
-
require "mongoid_money_field/rails_admin"
|
183
|
-
end
|
31
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
class MoneyType
|
2
|
+
attr_accessor :options
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = {
|
6
|
+
fixed_currency: nil,
|
7
|
+
default: nil,
|
8
|
+
required: false,
|
9
|
+
default_currency: nil
|
10
|
+
}.merge(options)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Get the object as it was stored in the database, and instantiate
|
14
|
+
# this custom class from it.
|
15
|
+
def demongoize(object)
|
16
|
+
if object.is_a?(Hash)
|
17
|
+
object = object.symbolize_keys
|
18
|
+
|
19
|
+
if object.has_key?(:cents)
|
20
|
+
if @options[:fixed_currency]
|
21
|
+
::Money.new(object[:cents], @options[:fixed_currency])
|
22
|
+
else
|
23
|
+
if object.has_key?(:currency_iso)
|
24
|
+
::Money.new(object[:cents], object[:currency_iso])
|
25
|
+
else
|
26
|
+
::Money.new(object[:cents], @options[:default_currency])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
elsif object.is_a?(Fixnum) || object.is_a?(Float)
|
33
|
+
if @options[:fixed_currency]
|
34
|
+
::Money.new(object, @options[:fixed_currency])
|
35
|
+
else
|
36
|
+
::Money.new(object, @options[:default_currency])
|
37
|
+
end
|
38
|
+
else
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Takes any possible object and converts it to how it would be
|
44
|
+
# stored in the database.
|
45
|
+
def mongoize(object)
|
46
|
+
unless @options[:default_currency].nil?
|
47
|
+
old_default = Money.default_currency
|
48
|
+
Money.default_currency = Money::Currency.new(@options[:default_currency])
|
49
|
+
end
|
50
|
+
|
51
|
+
ret = case
|
52
|
+
when object.is_a?(Money) then object.mongoize
|
53
|
+
when object.is_a?(Hash) then
|
54
|
+
object.symbolize_keys! if object.respond_to?(:symbolize_keys!)
|
55
|
+
::Money.new(object[:cents], object[:currency_iso]).mongoize
|
56
|
+
when object.blank? then
|
57
|
+
if !@options[:default].nil?
|
58
|
+
@options[:default].to_money.mongoize
|
59
|
+
else
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
when object.respond_to?(:to_money) then
|
63
|
+
object.to_money.mongoize
|
64
|
+
else object
|
65
|
+
end
|
66
|
+
|
67
|
+
unless @options[:default_currency].nil?
|
68
|
+
Money.default_currency = old_default
|
69
|
+
end
|
70
|
+
|
71
|
+
ret
|
72
|
+
end
|
73
|
+
|
74
|
+
# Converts the object that was supplied to a criteria and converts it
|
75
|
+
# into a database friendly form.
|
76
|
+
def evolve(object)
|
77
|
+
case object
|
78
|
+
when Money then object.mongoize
|
79
|
+
else object
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/mongoid_money_field.gemspec
CHANGED
@@ -18,16 +18,14 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
22
|
-
spec.
|
23
|
-
spec.add_dependency "money", "~> 6.1.1"
|
21
|
+
spec.add_runtime_dependency "mongoid", ">= 3.0.0"
|
22
|
+
spec.add_runtime_dependency "money", ">= 5.0.0"
|
24
23
|
|
25
24
|
spec.add_development_dependency "rake"
|
26
|
-
spec.add_development_dependency "bundler"
|
27
|
-
spec.add_development_dependency "rspec"
|
28
|
-
spec.add_development_dependency "rdoc"
|
29
|
-
spec.add_development_dependency "simplecov"
|
30
|
-
spec.add_development_dependency "database_cleaner"
|
31
|
-
spec.add_development_dependency "mongoid-rspec"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3.4"
|
26
|
+
spec.add_development_dependency "rspec", "~> 2.13.0"
|
27
|
+
spec.add_development_dependency "rdoc", "~> 4.0.1"
|
28
|
+
spec.add_development_dependency "simplecov", "~> 0.7.1"
|
29
|
+
spec.add_development_dependency "database_cleaner", "~> 0.9.1"
|
30
|
+
spec.add_development_dependency "mongoid-rspec", "~> 1.7.0"
|
32
31
|
end
|
33
|
-
|
data/spec/money_spec.rb
CHANGED
@@ -8,16 +8,16 @@ describe Mongoid::MoneyField do
|
|
8
8
|
it 'should be valid to save when field is filled in' do
|
9
9
|
dummy = DummyMoneyRequired.new
|
10
10
|
dummy.price = '$10'
|
11
|
-
|
12
|
-
|
11
|
+
dummy.should be_valid
|
12
|
+
dummy.save.should eq true
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'should be not valid to save when field is not filled in' do
|
16
16
|
dummy = DummyMoneyRequired.new
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
dummy.should_not be_valid
|
18
|
+
dummy.errors.count.should eq 1
|
19
|
+
dummy.errors.messages[:price][0].should eq "can't be blank"
|
20
|
+
dummy.save.should eq false
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -25,35 +25,35 @@ describe Mongoid::MoneyField do
|
|
25
25
|
it 'should raise the error when value consists non digits' do
|
26
26
|
dummy = DummyNotANumber.new
|
27
27
|
dummy.price = 'incorrect1'
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
dummy.should_not be_valid
|
29
|
+
dummy.errors.count.should eq 1
|
30
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
31
|
+
dummy.save.should eq false
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should raise the error when value consists more then one decimal separator' do
|
35
35
|
dummy = DummyNotANumber.new
|
36
36
|
dummy.price = '121,212,22'
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
dummy.should_not be_valid
|
38
|
+
dummy.errors.count.should eq 1
|
39
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
40
|
+
dummy.save.should eq false
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'should raise the error when value is not present' do
|
44
44
|
dummy = DummyNotANumber.new
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
dummy.should_not be_valid
|
46
|
+
dummy.errors.count.should eq 1
|
47
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
48
|
+
dummy.save.should eq false
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'should raise the error when value is not present' do
|
52
52
|
dummy = DummyNotANumber.new(price: '')
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
dummy.should_not be_valid
|
54
|
+
dummy.errors.count.should eq 1
|
55
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
56
|
+
dummy.save.should eq false
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -61,79 +61,78 @@ describe Mongoid::MoneyField do
|
|
61
61
|
it 'should raise the error when value consists non digits' do
|
62
62
|
dummy = DummyNotANumber.new
|
63
63
|
dummy.price = 'incorrect1'
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
dummy.should_not be_valid
|
65
|
+
dummy.errors.count.should eq 1
|
66
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
67
|
+
dummy.save.should eq false
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'should raise the error when value consists more then one decimal separator' do
|
71
71
|
dummy = DummyNotANumber.new
|
72
72
|
dummy.price = '121,212,22'
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
73
|
+
dummy.should_not be_valid
|
74
|
+
dummy.errors.count.should eq 1
|
75
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
76
|
+
dummy.save.should eq false
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
describe 'when value should be a positive number' do
|
81
81
|
it 'should raise the error when value lesser than 1' do
|
82
82
|
dummy = DummyPositiveNumber.new(price: '-10')
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
expect(dummy.price_cents).to eq -1000
|
83
|
+
dummy.should_not be_valid
|
84
|
+
dummy.errors.count.should eq 1
|
85
|
+
dummy.errors.messages[:price][0].should eq "must be greater than 1"
|
86
|
+
dummy.save.should eq false
|
88
87
|
end
|
89
88
|
|
90
89
|
it 'should raise the error when value lesser than 1' do
|
91
90
|
dummy = DummyPositiveNumber.new(price: '-1000')
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
91
|
+
dummy.should_not be_valid
|
92
|
+
dummy.errors.count.should eq 1
|
93
|
+
dummy.errors.messages[:price][0].should eq "must be greater than 1"
|
94
|
+
dummy.save.should eq false
|
96
95
|
end
|
97
96
|
|
98
97
|
it 'should raise the error when value lesser than 1' do
|
99
98
|
dummy = DummyPositiveNumber.new(price: '0')
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
99
|
+
dummy.should_not be_valid
|
100
|
+
dummy.errors.count.should eq 1
|
101
|
+
dummy.errors.messages[:price][0].should eq "must be greater than 1"
|
102
|
+
dummy.save.should eq false
|
104
103
|
end
|
105
104
|
|
106
105
|
it 'should be ok when value is greater than 1' do
|
107
106
|
dummy = DummyPositiveNumber.new(price: '10')
|
108
|
-
|
109
|
-
|
107
|
+
dummy.should be_valid
|
108
|
+
dummy.save.should eq true
|
110
109
|
end
|
111
110
|
|
112
111
|
it 'should be ok when value is greater than 1' do
|
113
112
|
dummy = DummyPositiveNumber.new(price: '1000')
|
114
|
-
|
115
|
-
|
113
|
+
dummy.should be_valid
|
114
|
+
dummy.save.should eq true
|
116
115
|
end
|
117
116
|
|
118
117
|
it 'should be ok when value is not present' do
|
119
118
|
dummy = DummyPositiveNumber.new(price: '')
|
120
|
-
|
121
|
-
|
119
|
+
dummy.should be_valid
|
120
|
+
dummy.save.should eq true
|
122
121
|
end
|
123
122
|
end
|
124
123
|
|
125
124
|
describe 'when both default currency and fixed currency is specified' do
|
126
125
|
it 'should use fixed currency instead of default' do
|
127
126
|
DummyOverrideDefaultCurrency.create!(price: '1.23')
|
128
|
-
|
127
|
+
DummyOverrideDefaultCurrency.first.price.currency.iso_code.should eq 'GBP'
|
129
128
|
end
|
130
129
|
end
|
131
130
|
|
132
131
|
describe 'when default currency is specified' do
|
133
132
|
it 'should use it instead of Money.default_currency' do
|
134
133
|
DummyWithDefaultCurrency.create!(price: '1.23')
|
135
|
-
|
136
|
-
|
134
|
+
DummyWithDefaultCurrency.first.price.currency.iso_code.should eq 'EUR'
|
135
|
+
Money.default_currency.iso_code.should eq 'RUB'
|
137
136
|
end
|
138
137
|
end
|
139
138
|
|
@@ -141,32 +140,32 @@ describe Mongoid::MoneyField do
|
|
141
140
|
it 'should be persisted normally when set as dollars' do
|
142
141
|
dummy = DummyMoney.new
|
143
142
|
dummy.price = '$10'
|
144
|
-
|
143
|
+
dummy.save.should eq true
|
145
144
|
end
|
146
145
|
|
147
146
|
it 'should be persisted normally when set as cents' do
|
148
147
|
dummy = DummyMoney.new
|
149
148
|
dummy.price = '$9.99'
|
150
|
-
|
149
|
+
dummy.save.should eq true
|
151
150
|
end
|
152
151
|
|
153
152
|
it 'should be persisted normally when set as Money' do
|
154
153
|
dummy = DummyMoney.new
|
155
|
-
dummy.price =
|
156
|
-
|
154
|
+
dummy.price = Money.parse(1.23)
|
155
|
+
dummy.save.should eq true
|
157
156
|
end
|
158
157
|
|
159
158
|
it 'should be possible to set value to nil' do
|
160
159
|
dummy = DummyMoney.new
|
161
|
-
dummy.price =
|
162
|
-
|
160
|
+
dummy.price = Money.parse(1.23)
|
161
|
+
dummy.save.should eq true
|
163
162
|
|
164
163
|
dummy = DummyMoney.first
|
165
|
-
|
164
|
+
dummy.price.should eq Money.parse(1.23)
|
166
165
|
dummy.price = nil
|
167
|
-
|
166
|
+
dummy.save.should eq true
|
168
167
|
dummy = DummyMoney.first
|
169
|
-
|
168
|
+
dummy.price.should be_nil
|
170
169
|
end
|
171
170
|
end
|
172
171
|
|
@@ -177,74 +176,74 @@ describe Mongoid::MoneyField do
|
|
177
176
|
|
178
177
|
it 'should have a Money value that matches the money value that was initially persisted' do
|
179
178
|
dummy = DummyMoney.first
|
180
|
-
|
179
|
+
dummy.price.should eq Money.parse('9.99')
|
181
180
|
end
|
182
181
|
end
|
183
182
|
|
184
183
|
describe 'when accessing a document from the datastore with a Money datatype set as money' do
|
185
184
|
before(:each) do
|
186
185
|
dm = DummyMoney.create(:description => "Test")
|
187
|
-
dm.price =
|
186
|
+
dm.price = Money.parse('1.23')
|
188
187
|
dm.save!
|
189
188
|
end
|
190
189
|
|
191
190
|
it 'should have a Money value that matches the money value that was initially persisted' do
|
192
191
|
dummy = DummyMoney.first
|
193
|
-
|
192
|
+
dummy.price.cents.should eq 123
|
194
193
|
end
|
195
194
|
end
|
196
195
|
|
197
196
|
describe 'when accessing a document from the datastore with a Money datatype set as money with mass asignment' do
|
198
197
|
before(:each) do
|
199
|
-
DummyMoney.create(:description => "Test", :price =>
|
198
|
+
DummyMoney.create(:description => "Test", :price => Money.parse('1.23'))
|
200
199
|
end
|
201
200
|
|
202
201
|
it 'should have a Money value that matches the money value that was initially persisted' do
|
203
202
|
dummy = DummyMoney.first
|
204
|
-
|
203
|
+
dummy.price.cents.should eq 123
|
205
204
|
end
|
206
205
|
end
|
207
206
|
|
208
207
|
describe 'when accessing a document from the datastore with a Money datatype and empty value' do
|
209
208
|
it 'should be nil' do
|
210
209
|
dummy = DummyMoneyWithoutDefault.new
|
211
|
-
|
212
|
-
|
210
|
+
dummy.save.should eq true
|
211
|
+
DummyMoneyWithoutDefault.first.price.should be_nil
|
213
212
|
end
|
214
213
|
|
215
214
|
it 'should be 0 when used with default' do
|
216
|
-
dummy =
|
217
|
-
|
218
|
-
|
215
|
+
dummy = DummyMoneyWithDefault.new
|
216
|
+
dummy.save.should eq true
|
217
|
+
DummyMoneyWithDefault.first.price.cents.should eq 100
|
219
218
|
end
|
220
219
|
|
221
220
|
it 'should set money to default currency if money is given without it' do
|
222
221
|
dummy = DummyMoneyWithDefault.new
|
223
|
-
|
222
|
+
dummy.save.should eq true
|
224
223
|
dummy = DummyMoneyWithDefault.first
|
225
|
-
|
226
|
-
|
224
|
+
dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
|
225
|
+
dummy.price.cents.should eq 100
|
227
226
|
end
|
228
227
|
|
229
228
|
it 'should set money to default currency if money is given without it on a document with multiple money fields' do
|
230
229
|
dummy = DummyPrices.new
|
231
|
-
|
230
|
+
dummy.save.should eq true
|
232
231
|
dummy = DummyPrices.first
|
233
|
-
|
234
|
-
|
232
|
+
dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
|
233
|
+
dummy.price.cents.should eq 100
|
235
234
|
|
236
|
-
|
235
|
+
dummy.price2.should be_nil
|
237
236
|
|
238
|
-
|
237
|
+
dummy.price1.cents.should eq 0
|
239
238
|
end
|
240
239
|
|
241
240
|
|
242
241
|
it 'should set money to correct currency if money is given with it' do
|
243
242
|
dummy = DummyMoneyWithDefaultWithCurrency.new
|
244
|
-
|
243
|
+
dummy.save.should eq true
|
245
244
|
dummy = DummyMoneyWithDefaultWithCurrency.first
|
246
|
-
|
247
|
-
|
245
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
246
|
+
dummy.price.cents.should eq 100
|
248
247
|
end
|
249
248
|
end
|
250
249
|
|
@@ -252,17 +251,17 @@ describe Mongoid::MoneyField do
|
|
252
251
|
it 'should have correct currency when value is set to 5$' do
|
253
252
|
DummyMoneyWithFixedCurrency.create!(price: '5$')
|
254
253
|
dummy = DummyMoneyWithFixedCurrency.first
|
255
|
-
|
256
|
-
|
257
|
-
|
254
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
255
|
+
dummy.price.cents.should eq 500
|
256
|
+
dummy.price.should eq Money.parse('5 GBP')
|
258
257
|
end
|
259
258
|
|
260
259
|
it 'should have correct currency when value is set to 100 RUB' do
|
261
260
|
DummyMoneyWithFixedCurrency.create!(price: '100 RUB')
|
262
261
|
dummy = DummyMoneyWithFixedCurrency.first
|
263
|
-
|
264
|
-
|
265
|
-
|
262
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
263
|
+
dummy.price.cents.should eq 100_00
|
264
|
+
dummy.price.should eq Money.parse('100 GBP')
|
266
265
|
end
|
267
266
|
end
|
268
267
|
|
@@ -270,17 +269,17 @@ describe Mongoid::MoneyField do
|
|
270
269
|
it 'should handle RUB' do
|
271
270
|
DummyMoney.create(description: "Test", price: '1.23 RUB')
|
272
271
|
dummy = DummyMoney.first
|
273
|
-
|
274
|
-
|
275
|
-
|
272
|
+
dummy.price.currency.iso_code.should eq 'RUB'
|
273
|
+
dummy.price.cents.should eq 123
|
274
|
+
dummy.price.should eq Money.parse('1.23 RUB')
|
276
275
|
end
|
277
276
|
|
278
277
|
it 'should handle $' do
|
279
278
|
DummyMoney.create(description: "Test", price: '1.23 USD')
|
280
279
|
dummy = DummyMoney.first
|
281
|
-
|
282
|
-
|
283
|
-
|
280
|
+
dummy.price.currency.iso_code.should eq 'USD'
|
281
|
+
dummy.price.cents.should eq 123
|
282
|
+
dummy.price.should eq Money.parse('1.23 USD')
|
284
283
|
end
|
285
284
|
end
|
286
285
|
|
@@ -291,25 +290,25 @@ describe Mongoid::MoneyField do
|
|
291
290
|
|
292
291
|
it 'should be nil' do
|
293
292
|
dummy = DummyMoney.first
|
294
|
-
|
293
|
+
dummy.price.should be_nil
|
295
294
|
end
|
296
295
|
|
297
296
|
it 'stays nil' do
|
298
297
|
dummy = DummyMoney.first
|
299
298
|
dummy.price = ''
|
300
|
-
|
301
|
-
|
302
|
-
|
299
|
+
dummy.price.should be_nil
|
300
|
+
dummy.save.should be_true
|
301
|
+
DummyMoney.first.price.should be_nil
|
303
302
|
end
|
304
303
|
|
305
304
|
it 'should be updated correctly' do
|
306
305
|
dummy = DummyMoney.first
|
307
|
-
|
306
|
+
dummy.price.should be_nil
|
308
307
|
dummy.price = '1.23 USD'
|
309
|
-
|
308
|
+
dummy.save.should eq true
|
310
309
|
dummy = DummyMoney.first
|
311
|
-
|
312
|
-
|
310
|
+
dummy.price.currency.iso_code.should eq 'USD'
|
311
|
+
dummy.price.cents.should eq 123
|
313
312
|
end
|
314
313
|
end
|
315
314
|
|
@@ -326,12 +325,12 @@ describe Mongoid::MoneyField do
|
|
326
325
|
|
327
326
|
it 'should have correct value for first item' do
|
328
327
|
o = DummyOrder.first
|
329
|
-
|
328
|
+
o.dummy_line_items.first.price.should eq Money.parse('12.99')
|
330
329
|
end
|
331
330
|
|
332
331
|
it 'should have correct value for first item' do
|
333
332
|
o = DummyOrder.first
|
334
|
-
|
333
|
+
o.dummy_line_items.last.price.should eq Money.parse('14.99')
|
335
334
|
end
|
336
335
|
end
|
337
336
|
|
@@ -342,15 +341,15 @@ describe Mongoid::MoneyField do
|
|
342
341
|
|
343
342
|
it 'should have correct Money value for field 1' do
|
344
343
|
dummy = DummyPrices.first
|
345
|
-
|
344
|
+
dummy.price1.should eq Money.parse('1.23')
|
346
345
|
end
|
347
346
|
it 'should have correct Money value for field 2' do
|
348
347
|
dummy = DummyPrices.first
|
349
|
-
|
348
|
+
dummy.price2.should eq Money.parse('2.33')
|
350
349
|
end
|
351
350
|
it 'should have correct Money value for field 3' do
|
352
351
|
dummy = DummyPrices.first
|
353
|
-
|
352
|
+
dummy.price3.should eq Money.parse('1')
|
354
353
|
end
|
355
354
|
end
|
356
355
|
end
|