mongoid_money_field 3.0.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -3
- data/Gemfile.lock +30 -30
- data/README.rdoc +10 -2
- data/VERSION +1 -1
- data/gemfiles/mongoid2.gemfile +4 -4
- data/gemfiles/mongoid3.gemfile +4 -4
- data/lib/mongoid_money_field.rb +84 -66
- data/lib/mongoid_money_field/simple_form/money_input.rb +20 -0
- data/mongoid_money_field.gemspec +14 -11
- data/spec/money_spec.rb +92 -9
- data/spec/support/dummy_not_a_number.rb +10 -0
- data/spec/support/dummy_positive_number.rb +10 -0
- metadata +14 -11
data/Gemfile
CHANGED
@@ -4,10 +4,10 @@ gem 'mongoid', '>= 2.4.0'
|
|
4
4
|
gem 'money'
|
5
5
|
|
6
6
|
group :development do
|
7
|
-
gem "rspec", "~> 2.
|
7
|
+
gem "rspec", "~> 2.12.0"
|
8
8
|
gem "rdoc", "~> 3.12"
|
9
|
-
gem "bundler", "
|
9
|
+
gem "bundler", ">= 1.1.0"
|
10
10
|
gem "jeweler", "~> 1.8.3"
|
11
11
|
gem "simplecov", '>= 0.4.0', :require => false
|
12
|
-
gem "database_cleaner", "~> 0.
|
12
|
+
gem "database_cleaner", "~> 0.9.0"
|
13
13
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,60 +1,60 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.2.
|
5
|
-
activesupport (= 3.2.
|
4
|
+
activemodel (3.2.9)
|
5
|
+
activesupport (= 3.2.9)
|
6
6
|
builder (~> 3.0.0)
|
7
|
-
activesupport (3.2.
|
7
|
+
activesupport (3.2.9)
|
8
8
|
i18n (~> 0.6)
|
9
9
|
multi_json (~> 1.0)
|
10
|
-
builder (3.0.
|
11
|
-
database_cleaner (0.
|
10
|
+
builder (3.0.4)
|
11
|
+
database_cleaner (0.9.1)
|
12
12
|
diff-lcs (1.1.3)
|
13
13
|
git (1.2.5)
|
14
|
-
i18n (0.6.
|
15
|
-
jeweler (1.8.
|
14
|
+
i18n (0.6.1)
|
15
|
+
jeweler (1.8.4)
|
16
16
|
bundler (~> 1.0)
|
17
17
|
git (>= 1.2.5)
|
18
18
|
rake
|
19
19
|
rdoc
|
20
|
-
json (1.
|
21
|
-
money (
|
22
|
-
i18n (~> 0.
|
23
|
-
|
24
|
-
mongoid (3.0.6)
|
20
|
+
json (1.7.5)
|
21
|
+
money (5.1.0)
|
22
|
+
i18n (~> 0.6.0)
|
23
|
+
mongoid (3.0.14)
|
25
24
|
activemodel (~> 3.1)
|
26
25
|
moped (~> 1.1)
|
27
26
|
origin (~> 1.0)
|
28
27
|
tzinfo (~> 0.3.22)
|
29
|
-
moped (1.
|
30
|
-
multi_json (1.
|
31
|
-
origin (1.0.
|
32
|
-
rake (0.
|
28
|
+
moped (1.3.0)
|
29
|
+
multi_json (1.3.7)
|
30
|
+
origin (1.0.11)
|
31
|
+
rake (10.0.2)
|
33
32
|
rdoc (3.12)
|
34
33
|
json (~> 1.4)
|
35
|
-
rspec (2.
|
36
|
-
rspec-core (~> 2.
|
37
|
-
rspec-expectations (~> 2.
|
38
|
-
rspec-mocks (~> 2.
|
39
|
-
rspec-core (2.
|
40
|
-
rspec-expectations (2.
|
34
|
+
rspec (2.12.0)
|
35
|
+
rspec-core (~> 2.12.0)
|
36
|
+
rspec-expectations (~> 2.12.0)
|
37
|
+
rspec-mocks (~> 2.12.0)
|
38
|
+
rspec-core (2.12.0)
|
39
|
+
rspec-expectations (2.12.0)
|
41
40
|
diff-lcs (~> 1.1.3)
|
42
|
-
rspec-mocks (2.
|
43
|
-
simplecov (0.
|
41
|
+
rspec-mocks (2.12.0)
|
42
|
+
simplecov (0.7.1)
|
44
43
|
multi_json (~> 1.0)
|
45
|
-
simplecov-html (~> 0.
|
46
|
-
simplecov-html (0.
|
47
|
-
tzinfo (0.3.
|
44
|
+
simplecov-html (~> 0.7.1)
|
45
|
+
simplecov-html (0.7.1)
|
46
|
+
tzinfo (0.3.35)
|
48
47
|
|
49
48
|
PLATFORMS
|
50
49
|
ruby
|
50
|
+
x86-mingw32
|
51
51
|
|
52
52
|
DEPENDENCIES
|
53
|
-
bundler (
|
54
|
-
database_cleaner (~> 0.
|
53
|
+
bundler (>= 1.1.0)
|
54
|
+
database_cleaner (~> 0.9.0)
|
55
55
|
jeweler (~> 1.8.3)
|
56
56
|
money
|
57
57
|
mongoid (>= 2.4.0)
|
58
58
|
rdoc (~> 3.12)
|
59
|
-
rspec (~> 2.
|
59
|
+
rspec (~> 2.12.0)
|
60
60
|
simplecov (>= 0.4.0)
|
data/README.rdoc
CHANGED
@@ -15,9 +15,10 @@ A simple gem that creates a Money datatype using RubyMoney for Mongoid.
|
|
15
15
|
|
16
16
|
Inspired by https://gist.github.com/840500
|
17
17
|
|
18
|
-
==
|
18
|
+
== Updating
|
19
19
|
v3 breaks backwards compatibility in favour of having syntax and defaults similar to field mongoid macro
|
20
|
-
|
20
|
+
|
21
|
+
To get the same behaviour as 1.0 please add "default: 0" to field declaration, like this
|
21
22
|
|
22
23
|
money_field :price, default: 0
|
23
24
|
|
@@ -54,6 +55,13 @@ Include the gem in your Gemfile
|
|
54
55
|
|
55
56
|
# default_currency is Money.default_currency.iso_code if not specified
|
56
57
|
# fixed_currency overrides default_currency if set
|
58
|
+
|
59
|
+
# field can be validated as numeric value (with localized separators)
|
60
|
+
validates_numericality_of :price, greater_than: 0
|
61
|
+
end
|
62
|
+
|
63
|
+
simple_form_for do |f|
|
64
|
+
f.input :price, as: :money
|
57
65
|
end
|
58
66
|
|
59
67
|
All Money values are converted and stored in mongo as cents and currency in two separate fields.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.2.0
|
data/gemfiles/mongoid2.gemfile
CHANGED
@@ -2,10 +2,10 @@ source "http://rubygems.org"
|
|
2
2
|
|
3
3
|
gem 'mongoid', '~> 2.4.0'
|
4
4
|
gem 'money'
|
5
|
-
|
6
|
-
gem "rspec", "~> 2.
|
5
|
+
|
6
|
+
gem "rspec", "~> 2.12.0"
|
7
7
|
gem "rdoc", "~> 3.12"
|
8
|
-
gem "bundler"
|
8
|
+
gem "bundler", ">= 1.1.0"
|
9
9
|
gem "jeweler", "~> 1.8.3"
|
10
10
|
gem "simplecov", '>= 0.4.0', :require => false
|
11
|
-
gem "database_cleaner", "~> 0.
|
11
|
+
gem "database_cleaner", "~> 0.9.0"
|
data/gemfiles/mongoid3.gemfile
CHANGED
@@ -2,10 +2,10 @@ source "http://rubygems.org"
|
|
2
2
|
|
3
3
|
gem 'mongoid', '~> 3.0.0'
|
4
4
|
gem 'money'
|
5
|
-
|
6
|
-
gem "rspec", "~> 2.
|
5
|
+
|
6
|
+
gem "rspec", "~> 2.12.0"
|
7
7
|
gem "rdoc", "~> 3.12"
|
8
|
-
gem "bundler"
|
8
|
+
gem "bundler", ">= 1.1.0"
|
9
9
|
gem "jeweler", "~> 1.8.3"
|
10
10
|
gem "simplecov", '>= 0.4.0', :require => false
|
11
|
-
gem "database_cleaner", "~> 0.
|
11
|
+
gem "database_cleaner", "~> 0.9.0"
|
data/lib/mongoid_money_field.rb
CHANGED
@@ -1,108 +1,126 @@
|
|
1
|
-
#
|
1
|
+
#encoding: utf-8
|
2
2
|
|
3
3
|
require 'money'
|
4
4
|
|
5
5
|
module Mongoid
|
6
6
|
module MoneyField
|
7
7
|
extend ActiveSupport::Concern
|
8
|
+
|
8
9
|
included do
|
9
|
-
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
13
|
|
14
|
-
def money_field(*columns)
|
15
|
-
opts = columns.last.is_a?(Hash) ? columns.pop : {}
|
14
|
+
def money_field( *columns )
|
15
|
+
opts = columns.last.is_a?( Hash ) ? columns.pop : {}
|
16
16
|
opts = {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
}.merge(opts)
|
22
|
-
|
23
|
-
[columns].flatten.each do |name|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
17
|
+
fixed_currency: nil,
|
18
|
+
default: nil,
|
19
|
+
required: false,
|
20
|
+
default_currency: nil
|
21
|
+
}.merge( opts )
|
22
|
+
|
23
|
+
[ columns ].flatten.each do |name|
|
24
|
+
default, default_cents = nil, nil
|
25
|
+
|
26
|
+
name = name.to_s
|
27
|
+
|
28
|
+
unless opts[:default].nil?
|
29
|
+
default = Money.parse( opts[:default] )
|
30
|
+
default_cents = default.cents
|
31
31
|
end
|
32
32
|
|
33
|
-
attr_cents
|
34
|
-
attr_currency
|
33
|
+
attr_cents = name + '_cents'
|
34
|
+
attr_currency = name + '_currency'
|
35
|
+
attr_plain = name + '_plain'
|
36
|
+
attr_before_type_cast = name + '_before_type_cast'
|
37
|
+
|
38
|
+
field attr_cents, type: Integer, default: default_cents
|
39
|
+
|
40
|
+
attr_accessible attr_plain, name
|
41
|
+
|
42
|
+
if opts[:required]
|
43
|
+
validates_presence_of name
|
44
|
+
end
|
35
45
|
|
36
|
-
field attr_cents, type: Integer, default: default
|
37
46
|
if opts[:fixed_currency].nil?
|
38
47
|
default_currency = nil
|
48
|
+
|
39
49
|
if opts[:default_currency].nil?
|
40
|
-
unless
|
41
|
-
default_currency = default_money.currency.iso_code
|
42
|
-
end
|
50
|
+
default_currency = default.currency.iso_code unless default.nil?
|
43
51
|
else
|
44
52
|
default_currency = opts[:default_currency]
|
45
53
|
end
|
46
54
|
|
47
55
|
field attr_currency, type: String, default: default_currency
|
48
|
-
end
|
49
56
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
if cents.nil?
|
55
|
-
errors.add(name, errors.generate_message(name, :error, default: "invalid value for #{name}"))
|
56
|
-
end
|
57
|
-
if opts[:fixed_currency].nil? && read_attribute(attr_currency).nil?
|
58
|
-
errors.add(name, errors.generate_message(name, :error, default: "invalid value for #{name} currency"))
|
57
|
+
if default_currency.nil?
|
58
|
+
before_save do
|
59
|
+
self[ attr_currency ] ||= Money.default_currency.iso_code
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
64
|
+
define_method( attr_before_type_cast ) do
|
65
|
+
code = ( opts[:fixed_currency].nil? ? read_attribute( attr_currency ) : opts[:fixed_currency] )
|
66
|
+
currency = Money::Currency.find( code ) || Money.default_currency
|
63
67
|
|
64
|
-
|
65
|
-
cents = read_attribute(attr_cents)
|
68
|
+
value = self.send( attr_plain )
|
66
69
|
|
67
|
-
if
|
68
|
-
currency = read_attribute(attr_currency)
|
69
|
-
else
|
70
|
-
currency = opts[:fixed_currency]
|
71
|
-
end
|
70
|
+
return if value.nil?
|
72
71
|
|
73
|
-
|
74
|
-
nil
|
75
|
-
else
|
76
|
-
Money.new(cents, currency || ::Money.default_currency)
|
77
|
-
end
|
72
|
+
value.gsub( currency.thousands_separator, '' ).gsub( currency.decimal_mark, '.' )
|
78
73
|
end
|
79
74
|
|
80
|
-
define_method(
|
75
|
+
define_method( name ) do
|
76
|
+
cents = read_attribute( attr_cents )
|
77
|
+
|
78
|
+
code = opts[:fixed_currency].nil? ? read_attribute( attr_currency ) : opts[:fixed_currency]
|
79
|
+
|
80
|
+
cents.nil? ? nil : Money.new( cents, code || ::Money.default_currency )
|
81
|
+
end
|
82
|
+
|
83
|
+
define_method( attr_plain ) do
|
84
|
+
value = instance_variable_get( "@#{attr_plain}".to_sym )
|
85
|
+
value = self.send( name ) if value.nil?
|
86
|
+
value = value.format( symbol: false, no_cents_if_whole: true ) if value.is_a?( Money )
|
87
|
+
|
88
|
+
value
|
89
|
+
end
|
90
|
+
|
91
|
+
define_method( "#{attr_plain}=" ) do |value|
|
92
|
+
instance_variable_set( "@#{attr_plain}".to_sym, value )
|
93
|
+
|
81
94
|
if value.blank?
|
82
|
-
write_attribute(attr_cents, nil)
|
83
|
-
if opts[:fixed_currency].nil?
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
else
|
88
|
-
if opts[:default_currency].nil?
|
89
|
-
money = value.to_money
|
90
|
-
else
|
91
|
-
old_default = Money.default_currency
|
92
|
-
Money.default_currency = Money::Currency.new(opts[:default_currency])
|
93
|
-
money = value.to_money
|
94
|
-
Money.default_currency = old_default
|
95
|
-
end
|
95
|
+
write_attribute( attr_cents, nil )
|
96
|
+
write_attribute( attr_currency, nil ) if opts[:fixed_currency].nil?
|
97
|
+
|
98
|
+
return
|
99
|
+
end
|
96
100
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
101
|
+
if opts[:default_currency].nil?
|
102
|
+
money = value.to_money
|
103
|
+
|
104
|
+
else
|
105
|
+
old_default = Money.default_currency
|
106
|
+
Money.default_currency = Money::Currency.new( opts[:default_currency] )
|
107
|
+
money = value.to_money
|
108
|
+
Money.default_currency = old_default
|
102
109
|
end
|
110
|
+
|
111
|
+
write_attribute( attr_cents, money.cents )
|
112
|
+
write_attribute( attr_currency, money.currency.iso_code ) if opts[:fixed_currency].nil?
|
113
|
+
end
|
114
|
+
|
115
|
+
define_method( "#{name}=" ) do |value|
|
116
|
+
self.send( "#{attr_plain}=", value )
|
103
117
|
end
|
104
118
|
end
|
105
119
|
end
|
106
120
|
end
|
107
121
|
end
|
108
122
|
end
|
123
|
+
|
124
|
+
if Object.const_defined?("SimpleForm")
|
125
|
+
require "mongoid_money_field/simple_form/money_input"
|
126
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class MoneyInput < SimpleForm::Inputs::Base
|
2
|
+
enable :placeholder, :min_max
|
3
|
+
|
4
|
+
def input
|
5
|
+
add_size!
|
6
|
+
input_html_classes.unshift("numeric")
|
7
|
+
if html5?
|
8
|
+
input_html_options[:type] ||= "number"
|
9
|
+
input_html_options[:step] ||= integer? ? 1 : "any"
|
10
|
+
end
|
11
|
+
@builder.text_field("#{attribute_name}_plain", input_html_options)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# Rails adds the size attr by default, if the :size key does not exist.
|
17
|
+
def add_size!
|
18
|
+
input_html_options[:size] ||= nil
|
19
|
+
end
|
20
|
+
end
|
data/mongoid_money_field.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "mongoid_money_field"
|
8
|
-
s.version = "3.
|
8
|
+
s.version = "3.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["glebtv"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-11-28"
|
13
13
|
s.description = "Use RubyMoney with mongoid"
|
14
14
|
s.email = "glebtv@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"gemfiles/mongoid2.gemfile",
|
30
30
|
"gemfiles/mongoid3.gemfile",
|
31
31
|
"lib/mongoid_money_field.rb",
|
32
|
+
"lib/mongoid_money_field/simple_form/money_input.rb",
|
32
33
|
"mongoid_money_field.gemspec",
|
33
34
|
"spec/money_spec.rb",
|
34
35
|
"spec/spec_helper.rb",
|
@@ -39,7 +40,9 @@ Gem::Specification.new do |s|
|
|
39
40
|
"spec/support/dummy_money_with_default_with_currency.rb",
|
40
41
|
"spec/support/dummy_money_with_fixed_currency.rb",
|
41
42
|
"spec/support/dummy_money_without_default.rb",
|
43
|
+
"spec/support/dummy_not_a_number.rb",
|
42
44
|
"spec/support/dummy_override_default_currency.rb",
|
45
|
+
"spec/support/dummy_positive_number.rb",
|
43
46
|
"spec/support/dummy_prices.rb",
|
44
47
|
"spec/support/dummy_with_default_currency.rb"
|
45
48
|
]
|
@@ -55,31 +58,31 @@ Gem::Specification.new do |s|
|
|
55
58
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
59
|
s.add_runtime_dependency(%q<mongoid>, [">= 2.4.0"])
|
57
60
|
s.add_runtime_dependency(%q<money>, [">= 0"])
|
58
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.
|
61
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
|
59
62
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
60
|
-
s.add_development_dependency(%q<bundler>, ["
|
63
|
+
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
61
64
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
62
65
|
s.add_development_dependency(%q<simplecov>, [">= 0.4.0"])
|
63
|
-
s.add_development_dependency(%q<database_cleaner>, ["~> 0.
|
66
|
+
s.add_development_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
64
67
|
else
|
65
68
|
s.add_dependency(%q<mongoid>, [">= 2.4.0"])
|
66
69
|
s.add_dependency(%q<money>, [">= 0"])
|
67
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
70
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
68
71
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
69
|
-
s.add_dependency(%q<bundler>, ["
|
72
|
+
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
70
73
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
71
74
|
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
72
|
-
s.add_dependency(%q<database_cleaner>, ["~> 0.
|
75
|
+
s.add_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
73
76
|
end
|
74
77
|
else
|
75
78
|
s.add_dependency(%q<mongoid>, [">= 2.4.0"])
|
76
79
|
s.add_dependency(%q<money>, [">= 0"])
|
77
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
80
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
78
81
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
79
|
-
s.add_dependency(%q<bundler>, ["
|
82
|
+
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
80
83
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
81
84
|
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
82
|
-
s.add_dependency(%q<database_cleaner>, ["~> 0.
|
85
|
+
s.add_dependency(%q<database_cleaner>, ["~> 0.9.0"])
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
data/spec/money_spec.rb
CHANGED
@@ -15,20 +15,105 @@ describe Mongoid::MoneyField do
|
|
15
15
|
it 'should be not valid to save when field is not filled in' do
|
16
16
|
dummy = DummyMoneyRequired.new
|
17
17
|
dummy.should_not be_valid
|
18
|
-
dummy.errors.count.should eq
|
19
|
-
dummy.errors.messages[:price][0].should eq "
|
20
|
-
dummy.errors.messages[:price][1].should eq "invalid value for price currency"
|
18
|
+
dummy.errors.count.should eq 1
|
19
|
+
dummy.errors.messages[:price][0].should eq "can't be blank"
|
21
20
|
dummy.save.should eq false
|
22
21
|
end
|
23
22
|
|
24
|
-
it 'should be
|
23
|
+
it 'should be valid to save when field is filled in but currency is not' do
|
25
24
|
dummy = DummyMoneyRequired.new
|
26
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' )
|
27
100
|
dummy.should_not be_valid
|
28
101
|
dummy.errors.count.should eq 1
|
29
|
-
dummy.errors.messages[:price][0].should eq "
|
102
|
+
dummy.errors.messages[:price][0].should eq "must be greater than 1"
|
30
103
|
dummy.save.should eq false
|
31
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
|
32
117
|
end
|
33
118
|
|
34
119
|
describe 'when both default currency and fixed currency is specified' do
|
@@ -157,7 +242,6 @@ describe Mongoid::MoneyField do
|
|
157
242
|
end
|
158
243
|
end
|
159
244
|
|
160
|
-
|
161
245
|
describe 'when accessing a document from the datastore with a Money datatype and fixed currency' do
|
162
246
|
it 'should have correct currency when value is set to 5$' do
|
163
247
|
DummyMoneyWithFixedCurrency.create!(price: '5$')
|
@@ -218,7 +302,7 @@ describe Mongoid::MoneyField do
|
|
218
302
|
describe 'when accessing a document from the datastore with embedded documents with money fields' do
|
219
303
|
before(:each) do
|
220
304
|
o = DummyOrder.new(first_name: 'test')
|
221
|
-
|
305
|
+
|
222
306
|
o.dummy_line_items << DummyLineItem.new({name: 'item 1', price: Money.new(1299)})
|
223
307
|
li = DummyLineItem.new({name: 'item 2', price: Money.new(1499)})
|
224
308
|
o.dummy_line_items.push li
|
@@ -234,8 +318,7 @@ describe Mongoid::MoneyField do
|
|
234
318
|
it 'should have correct value for first item' do
|
235
319
|
o = DummyOrder.first
|
236
320
|
o.dummy_line_items.last.price.should eq Money.parse('14.99')
|
237
|
-
end
|
238
|
-
|
321
|
+
end
|
239
322
|
end
|
240
323
|
|
241
324
|
describe 'when accessing a document from the datastore with multiple Money datatypes' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_money_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: 2.
|
53
|
+
version: 2.12.0
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2.
|
61
|
+
version: 2.12.0
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: rdoc
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,17 +80,17 @@ dependencies:
|
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
81
81
|
none: false
|
82
82
|
requirements:
|
83
|
-
- -
|
83
|
+
- - ! '>='
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version: 1.
|
85
|
+
version: 1.1.0
|
86
86
|
type: :development
|
87
87
|
prerelease: false
|
88
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
89
|
none: false
|
90
90
|
requirements:
|
91
|
-
- -
|
91
|
+
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version: 1.
|
93
|
+
version: 1.1.0
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: jeweler
|
96
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,7 +130,7 @@ dependencies:
|
|
130
130
|
requirements:
|
131
131
|
- - ~>
|
132
132
|
- !ruby/object:Gem::Version
|
133
|
-
version: 0.
|
133
|
+
version: 0.9.0
|
134
134
|
type: :development
|
135
135
|
prerelease: false
|
136
136
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -138,7 +138,7 @@ dependencies:
|
|
138
138
|
requirements:
|
139
139
|
- - ~>
|
140
140
|
- !ruby/object:Gem::Version
|
141
|
-
version: 0.
|
141
|
+
version: 0.9.0
|
142
142
|
description: Use RubyMoney with mongoid
|
143
143
|
email: glebtv@gmail.com
|
144
144
|
executables: []
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- gemfiles/mongoid2.gemfile
|
160
160
|
- gemfiles/mongoid3.gemfile
|
161
161
|
- lib/mongoid_money_field.rb
|
162
|
+
- lib/mongoid_money_field/simple_form/money_input.rb
|
162
163
|
- mongoid_money_field.gemspec
|
163
164
|
- spec/money_spec.rb
|
164
165
|
- spec/spec_helper.rb
|
@@ -169,7 +170,9 @@ files:
|
|
169
170
|
- spec/support/dummy_money_with_default_with_currency.rb
|
170
171
|
- spec/support/dummy_money_with_fixed_currency.rb
|
171
172
|
- spec/support/dummy_money_without_default.rb
|
173
|
+
- spec/support/dummy_not_a_number.rb
|
172
174
|
- spec/support/dummy_override_default_currency.rb
|
175
|
+
- spec/support/dummy_positive_number.rb
|
173
176
|
- spec/support/dummy_prices.rb
|
174
177
|
- spec/support/dummy_with_default_currency.rb
|
175
178
|
homepage: http://github.com/glebtv/mongoid_money_field
|
@@ -187,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
187
190
|
version: '0'
|
188
191
|
segments:
|
189
192
|
- 0
|
190
|
-
hash:
|
193
|
+
hash: -3180143814510618992
|
191
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
195
|
none: false
|
193
196
|
requirements:
|