easy_rails_money 0.0.9.pre → 0.0.9.pre1

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/CHANGELOG.md CHANGED
@@ -57,3 +57,29 @@
57
57
  holds true
58
58
  api is changed. previously #currency and #single_currency
59
59
  gave a Money::Currency object. now is is a String
60
+
61
+ ## 0.0.9.pre
62
+ - add ActiveRecord::Base.validates_money
63
+ it validates that currency is in an allowed list
64
+ and money is stored as a number or can be nil
65
+ - api change: if we set currency (when it is a single currency)
66
+ as nil. the other money columns are set to nil
67
+ this is done because technically, Money has a default_currency
68
+ so we can persist a Money object without the currency
69
+ but that can change over time and we want to be explicit
70
+
71
+ ## 0.0.9.pre1
72
+ - https://github.com/deepak/easy_rails_money/pull/1
73
+ when currency is set to nil, the money columns are set to nil as
74
+ well (see changelog for 0.0.9.pre above)
75
+ that code was buggy. so is a column was named "amount_funded"
76
+ it would give the setter as "amountfunded=" rather than
77
+ "amount_funded=".
78
+ git sha1: c1d9f6a8160d5a075e78f625f177f6716715c637
79
+ - bugfix: validates_money was failing if allowed_currency was not passed
80
+ the syntax is
81
+ validates_money :principal, :repaid, :amount_funded, :allow_nil => false, :allowed_currency => %w[inr usd sgd]
82
+ if we do not pass the last argument allowed_currency then it should
83
+ validate that it is a legal Money::Currency
84
+ that was not happenning because the Rails includes validations
85
+ does not compare Symbols and strings
data/README.md CHANGED
@@ -383,3 +383,5 @@ loan_usd.currency # equals Money::Currency.new(:usd)
383
383
  15. document. methods defined inside activerecord's scope
384
384
  move to a helper, no that its namespace is not polluted
385
385
  16. two specs tagged with fixme in validates_money_spec failing
386
+ 17. a version of inclusion_in validator that can compare
387
+ Symbol and string
@@ -8,7 +8,7 @@ module EasyRailsMoney
8
8
 
9
9
  included do
10
10
  def money_attributes
11
- attributes.keys.select {|x| x =~ /^(.+)_money/ }.map {|x| x.split('_')[0..-2].join }
11
+ attributes.keys.select {|x| x =~ /^(.+)_money/ }.map {|x| x.split('_')[0..-2].join('_') }
12
12
  end
13
13
  end
14
14
 
@@ -78,6 +78,11 @@ module EasyRailsMoney
78
78
 
79
79
  define_method "currency=" do |value|
80
80
  if value.nil?
81
+ # if we set currency (when it is a single currency)
82
+ # as nil. the other money columns are set to nil
83
+ # this is done because technically, Money has a default_currency
84
+ # so we can persist a Money object without the currency
85
+ # but that can change over time and we want to be explicit
81
86
  money_attributes.map do |name|
82
87
  send "#{name}=", nil
83
88
  end
@@ -21,8 +21,10 @@ class ActiveRecord::Base
21
21
  validates "#{column_name}_money", :numericality => { only_integer: true, greater_than_or_equal_to: 0 }, :allow_nil => options[:allow_nil]
22
22
  end
23
23
 
24
- allowed_currency = options[:allowed_currency] || Money::Currency.table.keys
24
+ allowed_currency = options[:allowed_currency] || EasyRailsMoney::MoneyValidator.currency_list
25
25
  if single_currency?
26
+ # TODO: a version of inclusion_in validator that can compare
27
+ # Symbol and string
26
28
  validates :currency, :inclusion => { in: allowed_currency }, :allow_nil => options[:allow_nil]
27
29
  else
28
30
  args.each do |column_name|
@@ -7,6 +7,15 @@ module EasyRailsMoney
7
7
  # only allow_nil is passed around (as it was needed). test for other
8
8
  # like if and unless as well
9
9
  class MoneyValidator < ActiveModel::EachValidator
10
+
11
+ class << self
12
+ attr_writer :currency_list
13
+
14
+ def currency_list
15
+ @currency_list ||= Money::Currency.table.keys.map(&:to_s)
16
+ end
17
+ end
18
+
10
19
  def validate_each(record, attribute, value)
11
20
  if options[:allow_nil]
12
21
  return if value.nil?
@@ -1,3 +1,3 @@
1
1
  module EasyRailsMoney
2
- VERSION = "0.0.9.pre"
2
+ VERSION = "0.0.9.pre1"
3
3
  end
@@ -48,7 +48,7 @@ describe "Currency Persistence" do
48
48
  loan = loan_model.new
49
49
  loan.principal = 100.to_money(@currency)
50
50
  loan.repaid = 50.to_money(@currency)
51
- loan.npa = 10.to_money(@currency)
51
+ loan.amount_funded = 10.to_money(@currency)
52
52
  loan
53
53
  }
54
54
 
@@ -80,7 +80,7 @@ describe "Currency Persistence" do
80
80
  loan.currency = @currency
81
81
  loan.principal = 100 * 100
82
82
  loan.repaid = 50 * 100
83
- loan.npa = 10 * 100
83
+ loan.amount_funded = 10 * 100
84
84
  loan
85
85
  }
86
86
 
@@ -36,7 +36,7 @@ EOF
36
36
  <<-EOF.strip_spaces
37
37
  create_table "loans", :force => true do |t|
38
38
  t.string "name"
39
- t.integer "npa_money"
39
+ t.integer "amount_funded_money"
40
40
  t.integer "principal_money"
41
41
  t.integer "repaid_money"
42
42
  t.string "currency"
@@ -48,8 +48,8 @@ EOF
48
48
  <<-EOF.strip_spaces
49
49
  create_table "loans", :force => true do |t|
50
50
  t.string "name"
51
- t.string "npa_currency"
52
- t.integer "npa_money"
51
+ t.string "amount_funded_currency"
52
+ t.integer "amount_funded_money"
53
53
  t.string "principal_currency"
54
54
  t.integer "principal_money"
55
55
  t.string "repaid_currency"
@@ -18,21 +18,21 @@ describe "Validation" do
18
18
 
19
19
  EasyRailsMoney::MoneyValidator.any_instance.should_receive(:validate_each).with(subject, :principal, subject.principal)
20
20
  EasyRailsMoney::MoneyValidator.any_instance.should_receive(:validate_each).with(subject, :repaid, subject.repaid)
21
- EasyRailsMoney::MoneyValidator.any_instance.should_receive(:validate_each).with(subject, :npa, subject.npa)
21
+ EasyRailsMoney::MoneyValidator.any_instance.should_receive(:validate_each).with(subject, :amount_funded, subject.amount_funded)
22
22
  end
23
23
 
24
24
  context "do not allow nil" do
25
25
  let(:subject) do
26
26
  model = loan_model
27
27
  model.instance_eval {
28
- validates_money :principal, :repaid, :npa, :allow_nil => false, :allowed_currency => %w[inr usd sgd]
28
+ validates_money :principal, :repaid, :amount_funded, :allow_nil => false, :allowed_currency => %w[inr usd sgd]
29
29
  }
30
30
 
31
31
  loan = model.new
32
32
  loan.name = "loan having some values"
33
33
  loan.principal = 100 * 100
34
34
  loan.repaid = 50 * 100
35
- loan.npa = 10 * 100
35
+ loan.amount_funded = 10 * 100
36
36
  loan
37
37
  end
38
38
 
@@ -54,8 +54,7 @@ describe "Validation" do
54
54
  expect(subject.attributes["currency"]).to be_nil
55
55
  add_expectations subject, false
56
56
  expect(subject).not_to be_valid
57
- puts subject.errors.messages
58
- expect(subject.errors.messages).to eq(:principal_money=>["is not a number"], :repaid_money=>["is not a number"], :npa_money=>["is not a number"], :currency=>["is not included in the list"])
57
+ expect(subject.errors.messages).to eq(:principal_money=>["is not a number"], :repaid_money=>["is not a number"], :amount_funded_money=>["is not a number"], :currency=>["is not included in the list"])
59
58
  end
60
59
  end # context "do not allow nil"
61
60
 
@@ -63,14 +62,14 @@ describe "Validation" do
63
62
  let(:subject) do
64
63
  model = loan_model
65
64
  model.instance_eval {
66
- validates_money :principal, :repaid, :npa, :allow_nil => true, :allowed_currency => %w[inr usd sgd]
65
+ validates_money :principal, :repaid, :amount_funded, :allow_nil => true, :allowed_currency => %w[inr usd sgd]
67
66
  }
68
67
 
69
68
  loan = model.new
70
69
  loan.name = "loan having nil values"
71
70
  loan.principal = nil
72
71
  loan.repaid = nil
73
- loan.npa = nil
72
+ loan.amount_funded = nil
74
73
  loan
75
74
  end
76
75
 
@@ -90,6 +89,27 @@ describe "Validation" do
90
89
  end
91
90
  end # context "allow nil"
92
91
 
92
+ context "no allowed currency" do
93
+ let(:subject) do
94
+ model = loan_model
95
+ model.instance_eval {
96
+ validates_money :principal, :repaid, :amount_funded, :allow_nil => false
97
+ }
98
+
99
+ loan = model.new
100
+ loan.name = "loan having some values and all currencies allowed"
101
+ loan.principal = 100 * 100
102
+ loan.repaid = 50 * 100
103
+ loan.amount_funded = 10 * 100
104
+ loan
105
+ end
106
+
107
+ it "is valid when it is a Money object" do
108
+ add_expectations subject, false
109
+ expect(subject).to be_valid
110
+ end
111
+ end
112
+
93
113
  pending "check lower-level validations"
94
114
  end
95
115
  end
@@ -9,7 +9,7 @@ module CreateTableDefinition
9
9
  t.string :name
10
10
  t.money :principal
11
11
  t.money :repaid
12
- t.money :npa
12
+ t.money :amount_funded
13
13
  t.currency
14
14
  end
15
15
  end
@@ -23,7 +23,7 @@ module CreateTableDefinition
23
23
  t.string :name
24
24
  t.money :principal
25
25
  t.money :repaid
26
- t.money :npa
26
+ t.money :amount_funded
27
27
  end
28
28
  end
29
29
  end
@@ -2,5 +2,5 @@ class Loan < ActiveRecord::Base
2
2
  attr_accessible :name
3
3
  money :principal
4
4
  money :repaid
5
- money :npa
5
+ money :amount_funded
6
6
  end
@@ -5,6 +5,6 @@ class LoanWithCurrency < ActiveRecord::Base
5
5
  with_currency(:inr) do
6
6
  money :principal
7
7
  money :repaid
8
- money :npa
8
+ money :amount_funded
9
9
  end
10
10
  end
@@ -22,7 +22,7 @@ module SchemaStatements
22
22
  t.string :name
23
23
  t.currency
24
24
  end
25
- add_money :loans, :principal, :repaid, :npa
25
+ add_money :loans, :principal, :repaid, :amount_funded
26
26
  end
27
27
  end
28
28
  end
@@ -30,7 +30,7 @@ module SchemaStatements
30
30
  class RemoveMoneyColumnsFromLoan < ActiveRecord::Migration
31
31
  def change
32
32
  suppress_messages do
33
- remove_money :loans, :principal, :repaid, :npa
33
+ remove_money :loans, :principal, :repaid, :amount_funded
34
34
  end
35
35
  end
36
36
  end
@@ -38,7 +38,7 @@ module SchemaStatements
38
38
  class RemoveMoneyColumnsExceptPrincipalFromLoan < ActiveRecord::Migration
39
39
  def change
40
40
  suppress_messages do
41
- remove_money :loans, :repaid, :npa
41
+ remove_money :loans, :repaid, :amount_funded
42
42
  end
43
43
  end
44
44
  end
@@ -105,7 +105,7 @@ module ChangeTable
105
105
  def change
106
106
  suppress_messages do
107
107
  change_table :loans, force: true do |t|
108
- t.remove_money :principal, :repaid, :npa
108
+ t.remove_money :principal, :repaid, :amount_funded
109
109
  end
110
110
  end
111
111
  end
@@ -115,7 +115,7 @@ module ChangeTable
115
115
  def change
116
116
  suppress_messages do
117
117
  change_table :loans, force: true do |t|
118
- t.remove_money :repaid, :npa
118
+ t.remove_money :repaid, :amount_funded
119
119
  end
120
120
  end
121
121
  end
@@ -164,7 +164,7 @@ module CreateTableDefinition
164
164
  t.currency
165
165
  t.money :principal
166
166
  t.money :repaid
167
- t.money :npa
167
+ t.money :amount_funded
168
168
  end
169
169
  end
170
170
  end
@@ -178,7 +178,7 @@ module CreateTableDefinition
178
178
  t.money :principal
179
179
  t.money :repaid
180
180
  t.currency
181
- t.money :npa
181
+ t.money :amount_funded
182
182
  end
183
183
  end
184
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_rails_money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9.pre
4
+ version: 0.0.9.pre1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -204,7 +204,6 @@ files:
204
204
  - spec/active_record_spec_helper.rb
205
205
  - spec/configuration_spec.rb
206
206
  - spec/loan_model_spec_helper.rb
207
- - spec/loan_with_currency_and_validation_model_spec_helper.rb
208
207
  - spec/loan_with_currency_model_spec_helper.rb
209
208
  - spec/migration_factory_spec_helper.rb
210
209
  - spec/simplecov_helper.rb
@@ -247,7 +246,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
247
246
  version: '0'
248
247
  segments:
249
248
  - 0
250
- hash: 4030965021580027687
249
+ hash: 176891542328036616
251
250
  required_rubygems_version: !ruby/object:Gem::Requirement
252
251
  none: false
253
252
  requirements:
@@ -268,7 +267,6 @@ test_files:
268
267
  - spec/active_record_spec_helper.rb
269
268
  - spec/configuration_spec.rb
270
269
  - spec/loan_model_spec_helper.rb
271
- - spec/loan_with_currency_and_validation_model_spec_helper.rb
272
270
  - spec/loan_with_currency_model_spec_helper.rb
273
271
  - spec/migration_factory_spec_helper.rb
274
272
  - spec/simplecov_helper.rb
@@ -1,12 +0,0 @@
1
- class LoanWithCurrencyAndValidation < ActiveRecord::Base
2
- self.table_name = "loans"
3
- attr_accessible :name
4
-
5
- with_currency(:inr) do
6
- money :principal
7
- money :repaid
8
- money :npa
9
- end
10
-
11
- validates_money :principal, :repaid, :npa, :allow_nil => true, :allowed_currency => %w[inr usd sgd]
12
- end