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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98301ef0d0c01eda3e2dfbb588e4b23649399c39
4
- data.tar.gz: 253619673f52ee7d97c6a6efcb5e4a8e1315eade
3
+ metadata.gz: 9872f92a8e94961a24bfdc1555fec6dd83708eca
4
+ data.tar.gz: db2c1d912130b1f54bfbe4832c22a313f6515d4a
5
5
  SHA512:
6
- metadata.gz: c809646376f3f0fb429df3dd700f8f378a77b3e82eb6e36cb3de61302727538470cbbdd6871848c4425a0dcf01165b16ef988fd49209130cf6f7c2d2682e51b3
7
- data.tar.gz: 98f7746d6548bb709c25c5ae48841c19a50271158f0bb889a3afdf838b8ed28f0cdc5e4ef5d15e8dfb7b1dae28f387769417ea532bc4bde5257c0501b2806836
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
1
+ 2.0.0-p247
data/.travis.yml CHANGED
@@ -1,11 +1,12 @@
1
1
  language: ruby
2
+
2
3
  services: mongodb
3
4
 
5
+ gemfile:
6
+ - gemfiles/money51-mongoid31.gemfile
7
+ - gemfiles/money51-mongoid4.gemfile
8
+
4
9
  rvm:
5
10
  - 1.9.3
6
11
  - 2.0.0
7
- - 2.1.2
8
-
9
- notifications:
10
- email: false
11
-
12
+ - jruby-19mode
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mongoid_money_field.gemspec
4
+ gem "mongoid", github: "mongoid/mongoid", branch: "master"
5
+
4
6
  gemspec
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
- With v4 storage format of mongoid money field is identical to that of money-rails
22
+ Since v4 storage format of mongoid money field is identical to that of money-rails
23
23
 
24
- == Updating
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
 
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.1.0'
4
+ gem "money", "~> 5.1.1"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid", github: "mongoid/mongoid", branch: "master"
4
+ gem "money", "~> 5.1.1"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid", '~> 3.1.0'
4
+ gem "money", "~> 6.0.0.pre5"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid", github: "mongoid/mongoid", branch: "master"
4
+ gem "money", "~> 6.0.0.pre5"
5
+
6
+ gemspec path: "../"
@@ -1,8 +1,8 @@
1
- #encoding: utf-8
1
+ # coding: utf-8
2
2
 
3
3
  require "money"
4
- require 'monetize'
5
- require 'monetize/core_extensions'
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?( Hash ) ? columns.pop : {}
16
- opts = {
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 = nil
41
- name = name.to_s
42
- unless opts[:default].nil?
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
@@ -1,5 +1,4 @@
1
1
  # coding: utf-8
2
-
3
2
  # from https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/mongoid/money.rb
4
3
 
5
4
  class Money
@@ -48,3 +47,4 @@ class Money
48
47
  end
49
48
  end
50
49
  end
50
+
@@ -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
@@ -1,3 +1,3 @@
1
1
  module MongoidMoneyField
2
- VERSION = "4.0.8"
2
+ VERSION = "5.0.0"
3
3
  end
@@ -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.add_dependency "mongoid", [">= 3.0", "< 4.1"]
22
- spec.add_dependency "monetize"
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
- expect(dummy).to be_valid
12
- expect(dummy.save).to eq true
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
- expect(dummy).not_to be_valid
18
- expect(dummy.errors.count).to eq 1
19
- expect(dummy.errors.messages[:price][0]).to eq "can't be blank"
20
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
29
- expect(dummy.errors.count).to eq 1
30
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
31
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
38
- expect(dummy.errors.count).to eq 1
39
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
40
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
46
- expect(dummy.errors.count).to eq 1
47
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
48
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
54
- expect(dummy.errors.count).to eq 1
55
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
56
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
65
- expect(dummy.errors.count).to eq 1
66
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
67
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
74
- expect(dummy.errors.count).to eq 1
75
- expect(dummy.errors.messages[:price][0]).to eq "is not a number"
76
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
84
- expect(dummy.errors.count).to eq 1
85
- expect(dummy.errors.messages[:price][0]).to eq "must be greater than 1"
86
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
93
- expect(dummy.errors.count).to eq 1
94
- expect(dummy.errors.messages[:price][0]).to eq "must be greater than 1"
95
- expect(dummy.save).to eq false
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
- expect(dummy).not_to be_valid
101
- expect(dummy.errors.count).to eq 1
102
- expect(dummy.errors.messages[:price][0]).to eq "must be greater than 1"
103
- expect(dummy.save).to eq false
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
- expect(dummy).to be_valid
109
- expect(dummy.save).to eq true
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
- expect(dummy).to be_valid
115
- expect(dummy.save).to eq true
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
- expect(dummy).to be_valid
121
- expect(dummy.save).to eq true
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
- expect(DummyOverrideDefaultCurrency.first.price.currency.iso_code).to eq 'GBP'
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
- expect(DummyWithDefaultCurrency.first.price.currency.iso_code).to eq 'EUR'
136
- expect(Money.default_currency.iso_code).to eq 'RUB'
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
- expect(dummy.save).to eq true
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
- expect(dummy.save).to eq true
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 = Monetize.parse(1.23)
156
- expect(dummy.save).to eq true
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 = Monetize.parse(1.23)
162
- expect(dummy.save).to eq true
160
+ dummy.price = Money.parse(1.23)
161
+ dummy.save.should eq true
163
162
 
164
163
  dummy = DummyMoney.first
165
- expect(dummy.price).to eq Monetize.parse(1.23)
164
+ dummy.price.should eq Money.parse(1.23)
166
165
  dummy.price = nil
167
- expect(dummy.save).to eq true
166
+ dummy.save.should eq true
168
167
  dummy = DummyMoney.first
169
- expect(dummy.price).to be_nil
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
- expect(dummy.price).to eq Monetize.parse('9.99')
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 = Monetize.parse('1.23')
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
- expect(dummy.price.cents).to eq 123
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 => Monetize.parse('1.23'))
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
- expect(dummy.price.cents).to eq 123
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
- expect(dummy.save).to eq true
212
- expect(DummyMoneyWithoutDefault.first.price).to be_nil
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 = DummyMoney.new
217
- expect(dummy.save).to eq true
218
- expect(DummyMoney.first.price.cents).to eq 0
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
- expect(dummy.save).to eq true
222
+ dummy.save.should eq true
224
223
  dummy = DummyMoneyWithDefault.first
225
- expect(dummy.price.currency.iso_code).to eq Money.default_currency.iso_code
226
- expect(dummy.price.cents).to eq 100
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
- expect(dummy.save).to eq true
230
+ dummy.save.should eq true
232
231
  dummy = DummyPrices.first
233
- expect(dummy.price.currency.iso_code).to eq Money.default_currency.iso_code
234
- expect(dummy.price.cents).to eq 100
232
+ dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
233
+ dummy.price.cents.should eq 100
235
234
 
236
- expect(dummy.price2).to be_nil
235
+ dummy.price2.should be_nil
237
236
 
238
- expect(dummy.price1.cents).to eq 0
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
- expect(dummy.save).to eq true
243
+ dummy.save.should eq true
245
244
  dummy = DummyMoneyWithDefaultWithCurrency.first
246
- expect(dummy.price.currency.iso_code).to eq 'GBP'
247
- expect(dummy.price.cents).to eq 100
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
- expect(dummy.price.currency.iso_code).to eq 'GBP'
256
- expect(dummy.price.cents).to eq 500
257
- expect(dummy.price).to eq Monetize.parse('5 GBP')
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
- expect(dummy.price.currency.iso_code).to eq 'GBP'
264
- expect(dummy.price.cents).to eq 100_00
265
- expect(dummy.price).to eq Monetize.parse('100 GBP')
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
- expect(dummy.price.currency.iso_code).to eq 'RUB'
274
- expect(dummy.price.cents).to eq 123
275
- expect(dummy.price).to eq Monetize.parse('1.23 RUB')
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
- expect(dummy.price.currency.iso_code).to eq 'USD'
282
- expect(dummy.price.cents).to eq 123
283
- expect(dummy.price).to eq Monetize.parse('1.23 USD')
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
- expect(dummy.price).to be_nil
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
- expect(dummy.price).to be_nil
301
- expect(dummy.save).to be_truthy
302
- expect(DummyMoney.first.price).to be_nil
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
- expect(dummy.price).to be_nil
306
+ dummy.price.should be_nil
308
307
  dummy.price = '1.23 USD'
309
- expect(dummy.save).to eq true
308
+ dummy.save.should eq true
310
309
  dummy = DummyMoney.first
311
- expect(dummy.price.currency.iso_code).to eq 'USD'
312
- expect(dummy.price.cents).to eq 123
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
- expect(o.dummy_line_items.first.price).to eq Monetize.parse('12.99')
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
- expect(o.dummy_line_items.last.price).to eq Monetize.parse('14.99')
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
- expect(dummy.price1).to eq Monetize.parse('1.23')
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
- expect(dummy.price2).to eq Monetize.parse('2.33')
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
- expect(dummy.price3).to eq Monetize.parse('1')
352
+ dummy.price3.should eq Money.parse('1')
354
353
  end
355
354
  end
356
355
  end