mongoid_money_field 3.2.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +0 -0
- data/.gitignore +18 -0
- data/.rspec +0 -0
- data/.travis.yml +8 -11
- data/Gemfile +3 -12
- data/Gemfile.lock +43 -38
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -1
- data/README.rdoc +15 -2
- data/Rakefile +4 -39
- data/gemfiles/{mongoid3.gemfile → money4-mongoid3.gemfile} +1 -1
- data/gemfiles/money4-mongoid31.gemfile +12 -0
- data/gemfiles/{mongoid2.gemfile → money50-mongoid3.gemfile} +10 -10
- data/gemfiles/money50-mongoid31.gemfile +12 -0
- data/gemfiles/money51-mongoid3.gemfile +11 -0
- data/gemfiles/money51-mongoid31.gemfile +12 -0
- data/lib/mongoid_money_field.rb +118 -76
- data/lib/mongoid_money_field/field.rb +48 -0
- data/lib/mongoid_money_field/simple_form/money_input.rb +1 -1
- data/lib/mongoid_money_field/version.rb +3 -0
- data/mongoid_money_field.gemspec +26 -83
- data/spec/model_spec.rb +8 -0
- data/spec/money3_compat_spec.rb +123 -0
- data/spec/money3_spec.rb +342 -0
- data/spec/money_spec.rb +60 -46
- data/spec/ruby_money_spec.rb +62 -0
- data/spec/spec_helper.rb +15 -16
- data/spec/support/dummy_not_a_number.rb +0 -0
- data/spec/support/dummy_positive_number.rb +0 -0
- data/spec/support/money3.rb +30 -0
- data/spec/support/mongoid.yml +8 -0
- data/spec/support/priceable.rb +5 -0
- metadata +90 -37
- data/VERSION +0 -1
@@ -0,0 +1,48 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class Money
|
4
|
+
# Converts an object of this instance into a database friendly value.
|
5
|
+
def mongoize
|
6
|
+
{
|
7
|
+
:cents => cents,
|
8
|
+
:currency_iso => currency.iso_code
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
# Get the object as it was stored in the database, and instantiate
|
15
|
+
# this custom class from it.
|
16
|
+
def demongoize(object)
|
17
|
+
if object.is_a?(Hash)
|
18
|
+
object = object.symbolize_keys
|
19
|
+
object.has_key?(:cents) ? ::Money.new(object[:cents], object[:currency_iso]) : nil
|
20
|
+
else
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Takes any possible object and converts it to how it would be
|
26
|
+
# stored in the database.
|
27
|
+
def mongoize(object)
|
28
|
+
case
|
29
|
+
when object.is_a?(Money) then object.mongoize
|
30
|
+
when object.is_a?(Hash) then
|
31
|
+
object.symbolize_keys! if object.respond_to?(:symbolize_keys!)
|
32
|
+
::Money.new(object[:cents], object[:currency_iso]).mongoize
|
33
|
+
when object.respond_to?(:to_money) then
|
34
|
+
object.to_money.mongoize
|
35
|
+
else object
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Converts the object that was supplied to a criteria and converts it
|
40
|
+
# into a database friendly form.
|
41
|
+
def evolve(object)
|
42
|
+
case object
|
43
|
+
when Money then object.mongoize
|
44
|
+
else object
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -8,7 +8,7 @@ class MoneyInput < SimpleForm::Inputs::Base
|
|
8
8
|
input_html_options[:type] ||= "number"
|
9
9
|
input_html_options[:step] ||= integer? ? 1 : "any"
|
10
10
|
end
|
11
|
-
@builder.text_field(
|
11
|
+
@builder.text_field(attribute_name, input_html_options)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
data/mongoid_money_field.gemspec
CHANGED
@@ -1,88 +1,31 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_money_field/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_money_field"
|
8
|
+
spec.version = MongoidMoneyField::VERSION
|
9
|
+
spec.authors = ["Gleb Tv"]
|
10
|
+
spec.email = ["glebtv@gmail.com"]
|
11
|
+
spec.description = %q{Use RubyMoney with mongoid}
|
12
|
+
spec.summary = %q{Use RubyMoney with mongoid}
|
13
|
+
spec.homepage = "http://github.com/glebtv/mongoid_money_field"
|
14
|
+
spec.license = "MIT"
|
9
15
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
s.email = "glebtv@gmail.com"
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".document",
|
20
|
-
".rspec",
|
21
|
-
".rvmrc",
|
22
|
-
".travis.yml",
|
23
|
-
"Gemfile",
|
24
|
-
"Gemfile.lock",
|
25
|
-
"MIT-LICENSE.txt",
|
26
|
-
"README.rdoc",
|
27
|
-
"Rakefile",
|
28
|
-
"VERSION",
|
29
|
-
"gemfiles/mongoid2.gemfile",
|
30
|
-
"gemfiles/mongoid3.gemfile",
|
31
|
-
"lib/mongoid_money_field.rb",
|
32
|
-
"lib/mongoid_money_field/simple_form/money_input.rb",
|
33
|
-
"mongoid_money_field.gemspec",
|
34
|
-
"spec/money_spec.rb",
|
35
|
-
"spec/spec_helper.rb",
|
36
|
-
"spec/support/dummy_embedded.rb",
|
37
|
-
"spec/support/dummy_money.rb",
|
38
|
-
"spec/support/dummy_money_required.rb",
|
39
|
-
"spec/support/dummy_money_with_default.rb",
|
40
|
-
"spec/support/dummy_money_with_default_with_currency.rb",
|
41
|
-
"spec/support/dummy_money_with_fixed_currency.rb",
|
42
|
-
"spec/support/dummy_money_without_default.rb",
|
43
|
-
"spec/support/dummy_not_a_number.rb",
|
44
|
-
"spec/support/dummy_override_default_currency.rb",
|
45
|
-
"spec/support/dummy_positive_number.rb",
|
46
|
-
"spec/support/dummy_prices.rb",
|
47
|
-
"spec/support/dummy_with_default_currency.rb"
|
48
|
-
]
|
49
|
-
s.homepage = "http://github.com/glebtv/mongoid_money_field"
|
50
|
-
s.licenses = ["MIT"]
|
51
|
-
s.require_paths = ["lib"]
|
52
|
-
s.rubygems_version = "1.8.24"
|
53
|
-
s.summary = "Use RubyMoney with mongoid"
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
54
20
|
|
55
|
-
|
56
|
-
|
21
|
+
spec.add_runtime_dependency "mongoid", ">= 3.0.0"
|
22
|
+
spec.add_runtime_dependency "money", ">= 0"
|
57
23
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
s.add_development_dependency(%q<simplecov>, [">= 0.4.0"])
|
66
|
-
s.add_development_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
67
|
-
else
|
68
|
-
s.add_dependency(%q<mongoid>, [">= 2.4.0"])
|
69
|
-
s.add_dependency(%q<money>, [">= 0"])
|
70
|
-
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
71
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
72
|
-
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
73
|
-
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
74
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
75
|
-
s.add_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
76
|
-
end
|
77
|
-
else
|
78
|
-
s.add_dependency(%q<mongoid>, [">= 2.4.0"])
|
79
|
-
s.add_dependency(%q<money>, [">= 0"])
|
80
|
-
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
81
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
82
|
-
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
83
|
-
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
84
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
85
|
-
s.add_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
86
|
-
end
|
24
|
+
spec.add_development_dependency "rake"
|
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"
|
87
31
|
end
|
88
|
-
|
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Money3Compat do
|
6
|
+
it { should allow_mass_assignment_of(:description) }
|
7
|
+
it { should allow_mass_assignment_of(:price) }
|
8
|
+
|
9
|
+
it 'correctly reads old fields data' do
|
10
|
+
Money3.create!(price_currency: 'GBP', price_cents: '12000')
|
11
|
+
Money3Compat.first.price.cents.should eq 12000
|
12
|
+
Money3Compat.first.price.currency.iso_code.should eq 'GBP'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'correctly works after save' do
|
16
|
+
Money3.create!(price_currency: 'GBP', price_cents: '12000')
|
17
|
+
Money3Compat.first.save.should be_true
|
18
|
+
|
19
|
+
Money3Compat.first.price.cents.should eq 12000
|
20
|
+
Money3Compat.first.price.currency.iso_code.should eq 'GBP'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'correctly migrates data' do
|
24
|
+
Money3.create!(price_currency: 'GBP', price_cents: '12000')
|
25
|
+
|
26
|
+
Money3.first.read_attribute(:price_currency).should_not be_nil
|
27
|
+
Money3.first.read_attribute(:price_cents).should_not be_nil
|
28
|
+
|
29
|
+
Money3.first.read_attribute(:price_currency).should eq 'GBP'
|
30
|
+
Money3.first.read_attribute(:price_cents).should eq 12000
|
31
|
+
|
32
|
+
Money3Compat.migrate_from_money_field_3!(:price)
|
33
|
+
|
34
|
+
Money3Compat.first.price.cents.should eq 12000
|
35
|
+
Money3Compat.first.price.currency.iso_code.should eq 'GBP'
|
36
|
+
|
37
|
+
Money3.first.read_attribute(:price_currency).should be_nil
|
38
|
+
Money3.first.read_attribute(:price_cents).should be_nil
|
39
|
+
|
40
|
+
Money3Compat.first.read_attribute(:price_currency).should be_nil
|
41
|
+
Money3Compat.first.read_attribute(:price_cents).should be_nil
|
42
|
+
|
43
|
+
f = Money3Compat.first
|
44
|
+
f.price = '32.00 GBP'
|
45
|
+
f.save.should be_true
|
46
|
+
|
47
|
+
Money3Compat.first.price.cents.should eq 3200
|
48
|
+
Money3Compat.first.price.currency.iso_code.should eq 'GBP'
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'with fixed currency' do
|
52
|
+
it 'correctly reads old' do
|
53
|
+
Money3.create!(price_with_fix_cents: '12000')
|
54
|
+
Money3Compat.first.price_with_fix.cents.should eq 12000
|
55
|
+
Money3Compat.first.price_with_fix.currency.iso_code.should eq 'GBP'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'correctly works after save' do
|
59
|
+
Money3.create!(price_with_fix_cents: '12000')
|
60
|
+
Money3Compat.first.save.should be_true
|
61
|
+
Money3Compat.first.price_with_fix.cents.should eq 12000
|
62
|
+
Money3Compat.first.price_with_fix.currency.iso_code.should eq 'GBP'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'correctly migrates data' do
|
66
|
+
Money3.create!(price_with_fix_cents: '12000')
|
67
|
+
Money3Compat.migrate_from_money_field_3!(:price_with_fix)
|
68
|
+
|
69
|
+
Money3Compat.first.read_attribute(:price_with_fix_currency).should be_nil
|
70
|
+
Money3Compat.first.read_attribute(:price_with_fix_cents).should be_nil
|
71
|
+
|
72
|
+
Money3Compat.first.price_with_fix.cents.should eq 12000
|
73
|
+
Money3Compat.first.price_with_fix.currency.iso_code.should eq 'GBP'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'with no default' do
|
78
|
+
it 'correctly reads old' do
|
79
|
+
Money3.create!(price_no_default_currency: 'GBP', price_no_default_cents: '12000')
|
80
|
+
Money3Compat.first.price_no_default.cents.should eq 12000
|
81
|
+
Money3Compat.first.price_no_default.currency.iso_code.should eq 'GBP'
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'correctly works after save' do
|
85
|
+
Money3.create!(price_no_default_currency: 'GBP', price_no_default_cents: '12000')
|
86
|
+
Money3Compat.first.save.should be_true
|
87
|
+
Money3Compat.first.price_no_default.cents.should eq 12000
|
88
|
+
Money3Compat.first.price_no_default.currency.iso_code.should eq 'GBP'
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'correctly migrates data' do
|
92
|
+
Money3.create!(price_no_default_currency: 'GBP', price_no_default_cents: '12000')
|
93
|
+
Money3Compat.migrate_from_money_field_3!(:price_no_default)
|
94
|
+
|
95
|
+
Money3Compat.first.read_attribute(:price_no_default_currency).should be_nil
|
96
|
+
Money3Compat.first.read_attribute(:price_no_default_cents).should be_nil
|
97
|
+
|
98
|
+
Money3Compat.first.price_no_default.cents.should eq 12000
|
99
|
+
Money3Compat.first.price_no_default.currency.iso_code.should eq 'GBP'
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'correctly migrates data with no currency' do
|
103
|
+
Money3.create!(price_no_default_cents: '12000')
|
104
|
+
Money3Compat.migrate_from_money_field_3!(:price_no_default)
|
105
|
+
|
106
|
+
Money3Compat.first.read_attribute(:price_no_default_currency).should be_nil
|
107
|
+
Money3Compat.first.read_attribute(:price_no_default_cents).should be_nil
|
108
|
+
|
109
|
+
Money3Compat.first.price_no_default.cents.should eq 12000
|
110
|
+
Money3Compat.first.price_no_default.currency.iso_code.should eq 'RUB'
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'correctly migrates data with no cents' do
|
114
|
+
Money3.create!()
|
115
|
+
Money3Compat.migrate_from_money_field_3!(:price_no_default)
|
116
|
+
|
117
|
+
Money3Compat.first.read_attribute(:price_no_default_currency).should be_nil
|
118
|
+
Money3Compat.first.read_attribute(:price_no_default_cents).should be_nil
|
119
|
+
|
120
|
+
Money3Compat.first.price_no_default.should be_nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/spec/money3_spec.rb
ADDED
@@ -0,0 +1,342 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Mongoid::MoneyField do
|
6
|
+
|
7
|
+
describe 'when money field is required' do
|
8
|
+
it 'should be valid to save when field is filled in' do
|
9
|
+
dummy = DummyMoneyRequired.new
|
10
|
+
dummy.price = '$10'
|
11
|
+
dummy.should be_valid
|
12
|
+
dummy.save.should eq true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be not valid to save when field is not filled in' do
|
16
|
+
dummy = DummyMoneyRequired.new
|
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
|
+
end
|
22
|
+
|
23
|
+
it 'should be valid to save when field is filled in but currency is not' do
|
24
|
+
dummy = DummyMoneyRequired.new
|
25
|
+
dummy.price_cents = 123
|
26
|
+
dummy.should be_valid
|
27
|
+
dummy.save.should eq true
|
28
|
+
dummy.price_currency.should eq Money.default_currency.iso_code
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'when value is filled from code' do
|
33
|
+
it 'should raise the error when value consists non digits' do
|
34
|
+
dummy = DummyNotANumber.new
|
35
|
+
dummy.price = 'incorrect1'
|
36
|
+
dummy.should_not be_valid
|
37
|
+
dummy.errors.count.should eq 1
|
38
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
39
|
+
dummy.save.should eq false
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should raise the error when value consists more then one decimal separator' do
|
43
|
+
dummy = DummyNotANumber.new
|
44
|
+
dummy.price = '121,212,22'
|
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
|
+
end
|
50
|
+
|
51
|
+
it 'should raise the error when value is not present' do
|
52
|
+
dummy = DummyNotANumber.new
|
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
|
+
end
|
58
|
+
|
59
|
+
it 'should raise the error when value is not present' do
|
60
|
+
dummy = DummyNotANumber.new( price: '' )
|
61
|
+
dummy.should_not be_valid
|
62
|
+
dummy.errors.count.should eq 1
|
63
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
64
|
+
dummy.save.should eq false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'when value is filled from SimpleForm' do
|
69
|
+
it 'should raise the error when value consists non digits' do
|
70
|
+
dummy = DummyNotANumber.new
|
71
|
+
dummy.price_plain = 'incorrect1'
|
72
|
+
dummy.should_not be_valid
|
73
|
+
dummy.errors.count.should eq 1
|
74
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
75
|
+
dummy.save.should eq false
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should raise the error when value consists more then one decimal separator' do
|
79
|
+
dummy = DummyNotANumber.new
|
80
|
+
dummy.price_plain = '121,212,22'
|
81
|
+
dummy.should_not be_valid
|
82
|
+
dummy.errors.count.should eq 1
|
83
|
+
dummy.errors.messages[:price][0].should eq "is not a number"
|
84
|
+
dummy.save.should eq false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'when value should be a positive number' do
|
89
|
+
it 'should raise the error when value lesser than 1' do
|
90
|
+
dummy = DummyPositiveNumber.new( price: '-10' )
|
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
|
95
|
+
dummy.price_cents.should eq -1000
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should raise the error when value lesser than 1' do
|
99
|
+
dummy = DummyPositiveNumber.new( price: '0' )
|
100
|
+
dummy.should_not be_valid
|
101
|
+
dummy.errors.count.should eq 1
|
102
|
+
dummy.errors.messages[:price][0].should eq "must be greater than 1"
|
103
|
+
dummy.save.should eq false
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should be ok when value is greater than 1' do
|
107
|
+
dummy = DummyPositiveNumber.new( price: '10' )
|
108
|
+
dummy.should be_valid
|
109
|
+
dummy.save.should eq true
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should be ok when value is not present' do
|
113
|
+
dummy = DummyPositiveNumber.new( price: '' )
|
114
|
+
dummy.should be_valid
|
115
|
+
dummy.save.should eq true
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe 'when both default currency and fixed currency is specified' do
|
120
|
+
it 'should use fixed currency instead of default' do
|
121
|
+
DummyOverrideDefaultCurrency.create!(price: '1.23')
|
122
|
+
DummyOverrideDefaultCurrency.first.price.currency.iso_code.should eq 'GBP'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe 'when default currency is specified' do
|
127
|
+
it 'should use it instead of Money.default_currency' do
|
128
|
+
DummyWithDefaultCurrency.create!(price: '1.23')
|
129
|
+
DummyWithDefaultCurrency.first.price.currency.iso_code.should eq 'EUR'
|
130
|
+
Money.default_currency.iso_code.should eq 'RUB'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe 'when persisting a document with a Money datatype' do
|
135
|
+
it 'should be persisted normally when set as dollars' do
|
136
|
+
dummy = DummyMoney.new
|
137
|
+
dummy.price = '$10'
|
138
|
+
dummy.save.should eq true
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should be persisted normally when set as cents' do
|
142
|
+
dummy = DummyMoney.new
|
143
|
+
dummy.price = '$9.99'
|
144
|
+
dummy.save.should eq true
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should be persisted normally when set as Money' do
|
148
|
+
dummy = DummyMoney.new
|
149
|
+
dummy.price = Money.parse(1.23)
|
150
|
+
dummy.save.should eq true
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should be possible to set value to nil' do
|
154
|
+
dummy = DummyMoney.new
|
155
|
+
dummy.price = Money.parse(1.23)
|
156
|
+
dummy.save.should eq true
|
157
|
+
|
158
|
+
dummy = DummyMoney.first
|
159
|
+
dummy.price.should eq Money.parse(1.23)
|
160
|
+
dummy.price = nil
|
161
|
+
dummy.save.should eq true
|
162
|
+
dummy = DummyMoney.first
|
163
|
+
dummy.price.should be_nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe 'when accessing a document from the datastore with a Money datatype' do
|
168
|
+
before(:each) do
|
169
|
+
DummyMoney.create(:description => "Test", :price => '9.99')
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should have a Money value that matches the money value that was initially persisted' do
|
173
|
+
dummy = DummyMoney.first
|
174
|
+
dummy.price.should eq Money.parse('9.99')
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe 'when accessing a document from the datastore with a Money datatype set as money' do
|
179
|
+
before(:each) do
|
180
|
+
dm = DummyMoney.create(:description => "Test")
|
181
|
+
dm.price = Money.parse('1.23')
|
182
|
+
dm.save!
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should have a Money value that matches the money value that was initially persisted' do
|
186
|
+
dummy = DummyMoney.first
|
187
|
+
dummy.price.cents.should eq 123
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe 'when accessing a document from the datastore with a Money datatype set as money with mass asignment' do
|
192
|
+
before(:each) do
|
193
|
+
DummyMoney.create(:description => "Test", :price => Money.parse('1.23'))
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'should have a Money value that matches the money value that was initially persisted' do
|
197
|
+
dummy = DummyMoney.first
|
198
|
+
dummy.price.cents.should eq 123
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe 'when accessing a document from the datastore with a Money datatype and empty value' do
|
203
|
+
it 'should be nil' do
|
204
|
+
dummy = DummyMoneyWithoutDefault.new
|
205
|
+
dummy.save.should eq true
|
206
|
+
DummyMoneyWithoutDefault.first.price.should be_nil
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'should be 0 when used with default' do
|
210
|
+
dummy = DummyMoney.new
|
211
|
+
dummy.save.should eq true
|
212
|
+
DummyMoney.first.price.cents.should eq 0
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should set money to default currency if money is given without it' do
|
216
|
+
dummy = DummyMoneyWithDefault.new
|
217
|
+
dummy.save.should eq true
|
218
|
+
dummy = DummyMoneyWithDefault.first
|
219
|
+
dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
|
220
|
+
dummy.price.cents.should eq 100
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'should set money to default currency if money is given without it on a document with multiple money fields' do
|
224
|
+
dummy = DummyPrices.new
|
225
|
+
dummy.save.should eq true
|
226
|
+
dummy = DummyPrices.first
|
227
|
+
dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
|
228
|
+
dummy.price.cents.should eq 100
|
229
|
+
|
230
|
+
dummy.price2.should be_nil
|
231
|
+
|
232
|
+
dummy.price1.cents.should eq 0
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
it 'should set money to correct currency if money is given with it' do
|
237
|
+
dummy = DummyMoneyWithDefaultWithCurrency.new
|
238
|
+
dummy.save.should eq true
|
239
|
+
dummy = DummyMoneyWithDefaultWithCurrency.first
|
240
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
241
|
+
dummy.price.cents.should eq 100
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe 'when accessing a document from the datastore with a Money datatype and fixed currency' do
|
246
|
+
it 'should have correct currency when value is set to 5$' do
|
247
|
+
DummyMoneyWithFixedCurrency.create!(price: '5$')
|
248
|
+
dummy = DummyMoneyWithFixedCurrency.first
|
249
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
250
|
+
dummy.price.cents.should eq 500
|
251
|
+
dummy.price.should eq Money.parse('5 GBP')
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'should have correct currency when value is set to 100 RUB' do
|
255
|
+
DummyMoneyWithFixedCurrency.create!(price: '100 RUB')
|
256
|
+
dummy = DummyMoneyWithFixedCurrency.first
|
257
|
+
dummy.price.currency.iso_code.should eq 'GBP'
|
258
|
+
dummy.price.cents.should eq 100_00
|
259
|
+
dummy.price.should eq Money.parse('100 GBP')
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe 'when setting to a string value with currency' do
|
264
|
+
it 'should handle RUB' do
|
265
|
+
DummyMoney.create(description: "Test", price: '1.23 RUB')
|
266
|
+
dummy = DummyMoney.first
|
267
|
+
dummy.price.currency.iso_code.should eq 'RUB'
|
268
|
+
dummy.price.cents.should eq 123
|
269
|
+
dummy.price.should eq Money.parse('1.23 RUB')
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'should handle $' do
|
273
|
+
DummyMoney.create(description: "Test", price: '1.23 USD')
|
274
|
+
dummy = DummyMoney.first
|
275
|
+
dummy.price.currency.iso_code.should eq 'USD'
|
276
|
+
dummy.price.cents.should eq 123
|
277
|
+
dummy.price.should eq Money.parse('1.23 USD')
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe 'when accessing a document from the datastore with a Money datatype and blank value' do
|
282
|
+
before(:each) do
|
283
|
+
DummyMoney.create(description: "Test", price: '')
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'should be nil' do
|
287
|
+
dummy = DummyMoney.first
|
288
|
+
dummy.price.should be_nil
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'should be updated correctly' do
|
292
|
+
dummy = DummyMoney.first
|
293
|
+
dummy.price.should be_nil
|
294
|
+
dummy.price = '1.23 USD'
|
295
|
+
dummy.save.should eq true
|
296
|
+
dummy = DummyMoney.first
|
297
|
+
dummy.price.currency.iso_code.should eq 'USD'
|
298
|
+
dummy.price.cents.should eq 123
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
describe 'when accessing a document from the datastore with embedded documents with money fields' do
|
303
|
+
before(:each) do
|
304
|
+
o = DummyOrder.new(first_name: 'test')
|
305
|
+
|
306
|
+
o.dummy_line_items << DummyLineItem.new({name: 'item 1', price: Money.new(1299)})
|
307
|
+
li = DummyLineItem.new({name: 'item 2', price: Money.new(1499)})
|
308
|
+
o.dummy_line_items.push li
|
309
|
+
|
310
|
+
o.save
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'should have correct value for first item' do
|
314
|
+
o = DummyOrder.first
|
315
|
+
o.dummy_line_items.first.price.should eq Money.parse('12.99')
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'should have correct value for first item' do
|
319
|
+
o = DummyOrder.first
|
320
|
+
o.dummy_line_items.last.price.should eq Money.parse('14.99')
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
describe 'when accessing a document from the datastore with multiple Money datatypes' do
|
325
|
+
before(:each) do
|
326
|
+
DummyPrices.create(description: "Test", price3: '1', price1: '1.23', price2: '2.33')
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'should have correct Money value for field 1' do
|
330
|
+
dummy = DummyPrices.first
|
331
|
+
dummy.price1.should eq Money.parse('1.23')
|
332
|
+
end
|
333
|
+
it 'should have correct Money value for field 2' do
|
334
|
+
dummy = DummyPrices.first
|
335
|
+
dummy.price2.should eq Money.parse('2.33')
|
336
|
+
end
|
337
|
+
it 'should have correct Money value for field 3' do
|
338
|
+
dummy = DummyPrices.first
|
339
|
+
dummy.price3.should eq Money.parse('1')
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|