mongoid_money_field 3.2.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document CHANGED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec CHANGED
File without changes
data/.travis.yml CHANGED
@@ -1,16 +1,13 @@
1
1
  language: ruby
2
2
  services: mongodb
3
3
  gemfile:
4
- - gemfiles/mongoid2.gemfile
5
- - gemfiles/mongoid3.gemfile
4
+ #- gemfiles/money4-mongoid3.gemfile
5
+ #- gemfiles/money4-mongoid31.gemfile
6
+ - gemfiles/money50-mongoid3.gemfile
7
+ #- gemfiles/money50-mongoid31.gemfile
8
+ - gemfiles/money51-mongoid3.gemfile
9
+ - gemfiles/money51-mongoid31.gemfile
6
10
  rvm:
7
- - 1.9.2
8
11
  - 1.9.3
9
- - rbx-19mode
10
- - jruby-19mode
11
- matrix:
12
- exclude:
13
- - rvm: 1.9.2
14
- gemfile: gemfiles/mongoid3.gemfile
15
- - rvm: rbx-19mode
16
- gemfile: gemfiles/mongoid2.gemfile
12
+ #- rbx-19mode
13
+ #- jruby-19mode
data/Gemfile CHANGED
@@ -1,13 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- gem 'mongoid', '>= 2.4.0'
4
- gem 'money'
5
-
6
- group :development do
7
- gem "rspec", "~> 2.12.0"
8
- gem "rdoc", "~> 3.12"
9
- gem "bundler", ">= 1.1.0"
10
- gem "jeweler", "~> 1.8.3"
11
- gem "simplecov", '>= 0.4.0', :require => false
12
- gem "database_cleaner", "~> 0.9.0"
13
- end
3
+ # Specify your gem's dependencies in mongoid_money_field.gemspec
4
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,59 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid_money_field (4.0.0)
5
+ money
6
+ mongoid (>= 3.0.0)
7
+
1
8
  GEM
2
- remote: http://rubygems.org/
9
+ remote: https://rubygems.org/
3
10
  specs:
4
- activemodel (3.2.9)
5
- activesupport (= 3.2.9)
11
+ activemodel (3.2.13)
12
+ activesupport (= 3.2.13)
6
13
  builder (~> 3.0.0)
7
- activesupport (3.2.9)
8
- i18n (~> 0.6)
14
+ activesupport (3.2.13)
15
+ i18n (= 0.6.1)
9
16
  multi_json (~> 1.0)
10
17
  builder (3.0.4)
11
18
  database_cleaner (0.9.1)
12
- diff-lcs (1.1.3)
13
- git (1.2.5)
19
+ diff-lcs (1.2.1)
14
20
  i18n (0.6.1)
15
- jeweler (1.8.4)
16
- bundler (~> 1.0)
17
- git (>= 1.2.5)
18
- rake
19
- rdoc
20
- json (1.7.5)
21
- money (5.1.0)
21
+ json (1.7.7)
22
+ money (5.1.1)
22
23
  i18n (~> 0.6.0)
23
- mongoid (3.0.14)
24
- activemodel (~> 3.1)
25
- moped (~> 1.1)
24
+ mongoid (3.1.2)
25
+ activemodel (~> 3.2)
26
+ moped (~> 1.4.2)
26
27
  origin (~> 1.0)
27
28
  tzinfo (~> 0.3.22)
28
- moped (1.3.0)
29
- multi_json (1.4.0)
29
+ mongoid-rspec (1.7.0)
30
+ mongoid (>= 3.0.1)
31
+ rake
32
+ rspec (>= 2.9)
33
+ moped (1.4.5)
34
+ multi_json (1.7.2)
30
35
  origin (1.0.11)
31
- rake (10.0.2)
32
- rdoc (3.12)
36
+ rake (10.0.4)
37
+ rdoc (4.0.1)
33
38
  json (~> 1.4)
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)
40
- diff-lcs (~> 1.1.3)
41
- rspec-mocks (2.12.0)
39
+ rspec (2.13.0)
40
+ rspec-core (~> 2.13.0)
41
+ rspec-expectations (~> 2.13.0)
42
+ rspec-mocks (~> 2.13.0)
43
+ rspec-core (2.13.1)
44
+ rspec-expectations (2.13.0)
45
+ diff-lcs (>= 1.1.3, < 2.0)
46
+ rspec-mocks (2.13.0)
42
47
  simplecov (0.7.1)
43
48
  multi_json (~> 1.0)
44
49
  simplecov-html (~> 0.7.1)
45
50
  simplecov-html (0.7.1)
46
- tzinfo (0.3.35)
51
+ tzinfo (0.3.37)
47
52
 
48
53
  PLATFORMS
49
54
  ruby
50
55
 
51
56
  DEPENDENCIES
52
- bundler (>= 1.1.0)
53
- database_cleaner (~> 0.9.0)
54
- jeweler (~> 1.8.3)
55
- money
56
- mongoid (>= 2.4.0)
57
- rdoc (~> 3.12)
58
- rspec (~> 2.12.0)
59
- simplecov (>= 0.4.0)
57
+ bundler (~> 1.3.4)
58
+ database_cleaner (~> 0.9.1)
59
+ mongoid-rspec (~> 1.7.0)
60
+ mongoid_money_field!
61
+ rake
62
+ rdoc (~> 4.0.1)
63
+ rspec (~> 2.13.0)
64
+ simplecov (~> 0.7.1)
@@ -1,4 +1,6 @@
1
- Copyright (c) 2012 glebtv
1
+ Copyright (c) 2013 Gleb Tv
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -15,8 +15,21 @@ 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
+ == Alternatives
19
+
20
+ https://github.com/RubyMoney/money-rails
21
+
22
+ With v4 storage format of mongoid money field is identical to that of money-rails
23
+
18
24
  == Updating
19
- v3 breaks backwards compatibility in favour of having syntax and defaults similar to field mongoid macro
25
+
26
+ v4 is a massive refactor. Storage format is now a hash, like money-rails, instead of two separate cents and currency fields
27
+ Old values should be still accessible after you update. To migrate to new storage format do:
28
+
29
+ YourModel.migrate_from_money_field_3!(your_money_fields)
30
+
31
+
32
+ v3 has a minor break of backwards compatibility in favour of having syntax and defaults similar to field mongoid macro
20
33
 
21
34
  To get the same behaviour as 1.0 please add "default: 0" to field declaration, like this
22
35
 
@@ -72,5 +85,5 @@ DummyMoney.where(price_cents: 123).first
72
85
 
73
86
  == Copyright
74
87
 
75
- Copyright (c) 2012 glebtv. MIT License.
88
+ Copyright (c) 2012-2013 glebtv (http://rocketscience.pro). MIT License.
76
89
 
data/Rakefile CHANGED
@@ -1,44 +1,9 @@
1
- # encoding: utf-8
1
+ require "bundler/gem_tasks"
2
2
 
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "mongoid_money_field"
18
- gem.homepage = "http://github.com/glebtv/mongoid_money_field"
19
- gem.license = "MIT"
20
- gem.summary = %Q{Use RubyMoney with mongoid}
21
- gem.description = %Q{Use RubyMoney with mongoid}
22
- gem.email = "glebtv@gmail.com"
23
- gem.authors = ["glebtv"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
- require 'rspec/core'
29
3
  require 'rspec/core/rake_task'
30
- RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
33
4
 
5
+ desc 'Default: run specs.'
34
6
  task :default => :spec
35
7
 
36
- require 'rdoc/task'
37
- Rake::RDocTask.new do |rdoc|
38
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
-
40
- rdoc.rdoc_dir = 'rdoc'
41
- rdoc.title = "mongoid_money_field #{version}"
42
- rdoc.rdoc_files.include('README*')
43
- rdoc.rdoc_files.include('lib/**/*.rb')
44
- end
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new
@@ -1,7 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'mongoid', '~> 3.0.0'
4
- gem 'money'
4
+ gem "money", "~> 4.0.2"
5
5
 
6
6
  gem "rspec", "~> 2.12.0"
7
7
  gem "rdoc", "~> 3.12"
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.1.0'
4
+ gem "money", "~> 4.0.2"
5
+
6
+ gem "rake"
7
+ gem "bundler", "~> 1.3.4"
8
+ gem "rspec", "~> 2.13.0"
9
+ gem "rdoc", "~> 4.0.1"
10
+ gem "simplecov", "~> 0.7.1", require: false
11
+ gem "database_cleaner", "~> 0.9.1"
12
+ gem "mongoid-rspec", "~> 1.7.0"
@@ -1,11 +1,11 @@
1
- source "http://rubygems.org"
2
-
3
- gem 'mongoid', '~> 2.4.0'
4
- gem 'money'
5
-
6
- gem "rspec", "~> 2.12.0"
7
- gem "rdoc", "~> 3.12"
8
- gem "bundler", ">= 1.1.0"
9
- gem "jeweler", "~> 1.8.3"
10
- gem "simplecov", '>= 0.4.0', :require => false
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.0.0'
4
+ gem "money", "~> 5.0.0"
5
+
6
+ gem "rspec", "~> 2.12.0"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", ">= 1.1.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ gem "simplecov", '>= 0.4.0', :require => false
11
11
  gem "database_cleaner", "~> 0.9.0"
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.1.0'
4
+ gem "money", "~> 5.0.0"
5
+
6
+ gem "rake"
7
+ gem "bundler", "~> 1.3.4"
8
+ gem "rspec", "~> 2.13.0"
9
+ gem "rdoc", "~> 4.0.1"
10
+ gem "simplecov", "~> 0.7.1", require: false
11
+ gem "database_cleaner", "~> 0.9.1"
12
+ gem "mongoid-rspec", "~> 1.7.0"
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.0.0'
4
+ gem "money", "~> 5.1.1"
5
+
6
+ gem "rspec", "~> 2.12.0"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", ">= 1.1.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ gem "simplecov", '>= 0.4.0', :require => false
11
+ gem "database_cleaner", "~> 0.9.0"
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '~> 3.1.0'
4
+ gem "money", "~> 5.1.1"
5
+
6
+ gem "rake"
7
+ gem "bundler", "~> 1.3.4"
8
+ gem "rspec", "~> 2.13.0"
9
+ gem "rdoc", "~> 4.0.1"
10
+ gem "simplecov", "~> 0.7.1", require: false
11
+ gem "database_cleaner", "~> 0.9.1"
12
+ gem "mongoid-rspec", "~> 1.7.0"
@@ -1,119 +1,161 @@
1
1
  #encoding: utf-8
2
2
 
3
- require 'money'
3
+ require "money"
4
+ require "mongoid_money_field/version"
5
+ require "mongoid_money_field/field"
4
6
 
5
7
  module Mongoid
6
8
  module MoneyField
7
9
  extend ActiveSupport::Concern
8
-
9
- included do
10
- end
11
10
 
12
11
  module ClassMethods
13
-
14
- def money_field( *columns )
12
+ def money_field(*columns)
15
13
  opts = columns.last.is_a?( Hash ) ? columns.pop : {}
16
14
  opts = {
17
- fixed_currency: nil,
18
- default: nil,
19
- required: false,
20
- default_currency: nil
15
+ fixed_currency: nil,
16
+ default: nil,
17
+ required: false,
18
+ default_currency: nil
21
19
  }.merge( opts )
22
20
 
23
- [ columns ].flatten.each do |name|
24
- default, default_cents = nil, nil
21
+ ensure_default = Proc.new do |currency|
22
+ if opts[:fixed_currency].nil?
23
+ if currency.nil?
24
+ if opts[:default_currency].nil?
25
+ Money.default_currency
26
+ else
27
+ opts[:default_currency]
28
+ end
29
+ else
30
+ currency
31
+ end
32
+ else
33
+ opts[:fixed_currency]
34
+ end
35
+ end
25
36
 
37
+ [columns].flatten.each do |name|
38
+ default = nil
26
39
  name = name.to_s
27
-
28
40
  unless opts[:default].nil?
29
- default = Money.parse( opts[:default] )
30
- default_cents = default.cents
41
+ default = Money.parse(opts[:default])
31
42
  end
32
43
 
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
44
+ field name, type: Money, default: default
45
+ validates_presence_of name if opts[:required]
39
46
 
40
- attr_accessible attr_plain, name
47
+ define_method("#{name}=") do |value|
48
+ instance_variable_set( "@#{name}_before_type_cast".to_sym, value)
41
49
 
42
- if opts[:required]
43
- validates_presence_of name
44
- end
45
-
46
- if opts[:fixed_currency].nil?
47
- default_currency = nil
48
-
49
- if opts[:default_currency].nil?
50
- default_currency = default.currency.iso_code unless default.nil?
50
+ if value.blank?
51
+ write_attribute(name, nil)
51
52
  else
52
- default_currency = opts[:default_currency]
53
- end
54
-
55
- field attr_currency, type: String, default: default_currency
53
+ if opts[:default_currency].nil?
54
+ money = value.to_money
55
+ else
56
+ old_default = Money.default_currency
57
+ Money.default_currency = Money::Currency.new(opts[:default_currency])
58
+ money = value.to_money
59
+ Money.default_currency = old_default
60
+ end
56
61
 
57
- if default_currency.nil?
58
- before_save do
59
- self[ attr_currency ] ||= Money.default_currency.iso_code
62
+ unless opts[:fixed_currency].nil?
63
+ money = Money.new(money.cents, opts[:fixed_currency])
60
64
  end
65
+
66
+ write_attribute(name, money)
67
+ remove_attribute("#{name}_currency")
68
+ remove_attribute("#{name}_cents")
61
69
  end
62
70
  end
63
71
 
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
67
-
68
- value = self.send( attr_plain )
69
-
70
- return if value.nil?
72
+ # mongoid money field 2 compat
73
+ define_method(name) do
74
+ if read_attribute("#{name}_cents").nil?
75
+ value = read_attribute(name)
76
+ if value.nil?
77
+ nil
78
+ else
79
+ if value.is_a?(Hash)
80
+ value[:currency_iso] = ensure_default.call(value[:currency_iso])
81
+ end
82
+ Money.demongoize(value)
83
+ end
71
84
 
72
- value.gsub( currency.thousands_separator, '' ).gsub( currency.decimal_mark, '.' )
85
+ else
86
+ currency = read_attribute("#{name}_currency")
87
+ currency = ensure_default.call(currency)
88
+ Money.new(read_attribute("#{name}_cents"), currency)
89
+ end
73
90
  end
74
91
 
75
- define_method( name ) do
76
- cents = read_attribute( attr_cents )
92
+ define_method("#{name}_before_type_cast") do
93
+ instance_variable_get( "@#{name}_before_type_cast".to_sym) || send(name).to_s
94
+ end
77
95
 
78
- code = opts[:fixed_currency].nil? ? read_attribute( attr_currency ) : opts[:fixed_currency]
96
+ # deprecated
97
+ define_method("migrate_#{name}_from_money_3!") do
98
+ cents = read_attribute("#{name}_cents")
99
+ if cents.nil?
100
+ send("#{name}=", nil)
101
+ else
102
+ currency = read_attribute("#{name}_currency")
103
+
104
+ if currency.nil?
105
+ if opts[:default_currency].nil?
106
+ currency = Money.default_currency
107
+ else
108
+ currency = opts[:default_currency]
109
+ end
110
+ end
79
111
 
80
- cents.nil? ? nil : Money.new( cents, code || ::Money.default_currency )
112
+ unless opts[:fixed_currency].nil?
113
+ currency = opts[:fixed_currency]
114
+ end
115
+ send("#{name}=", Money.new(cents, currency))
116
+ end
81
117
  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, thousands_separator: false ) if value.is_a?( Money )
87
118
 
88
- value
119
+ # deprecated
120
+ define_method("#{name}_cents") do
121
+ send(name).nil? ? 0 : send(name).cents
89
122
  end
90
-
91
- define_method( "#{attr_plain}=" ) do |value|
92
- instance_variable_set( "@#{attr_plain}".to_sym, value )
93
123
 
94
- if value.blank?
95
- write_attribute( attr_cents, nil )
96
- write_attribute( attr_currency, nil ) if opts[:fixed_currency].nil?
97
-
98
- return
124
+ # deprecated
125
+ define_method("#{name}_currency") do
126
+ if opts[:fixed_currency].nil?
127
+ send(name).nil? ? Money.default_currency : send(name).currency.iso_code
128
+ else
129
+ opts[:fixed_currency]
99
130
  end
131
+ end
100
132
 
101
- if opts[:default_currency].nil?
102
- money = value.to_money
133
+ # deprecated
134
+ define_method("#{name}_cents=") do |val|
135
+ send("#{name}=", Money.new(val, send("#{name}_currency")))
136
+ end
103
137
 
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
109
- end
138
+ # deprecated
139
+ define_method("#{name}_currency=") do |val|
140
+ send("#{name}=", Money.new(send("#{name}_cents"), val))
141
+ end
110
142
 
111
- write_attribute( attr_cents, money.cents )
112
- write_attribute( attr_currency, money.currency.iso_code ) if opts[:fixed_currency].nil?
143
+ # deprecated
144
+ define_method("#{name}_plain=") do |val|
145
+ send("#{name}=", val)
146
+ end
147
+ # deprecated
148
+ define_method("#{name}_plain") do |val|
149
+ send("#{name}")
113
150
  end
151
+ end
152
+ end
114
153
 
115
- define_method( "#{name}=" ) do |value|
116
- self.send( "#{attr_plain}=", value )
154
+ def migrate_from_money_field_3!(*columns)
155
+ each do |val|
156
+ [columns].flatten.each do |name|
157
+ val.send("migrate_#{name.to_s}_from_money_3!")
158
+ val.save!
117
159
  end
118
160
  end
119
161
  end