easy_rails_money 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,3 +47,13 @@
47
47
 
48
48
  add_column and change_table do not work for now
49
49
  with this syntax
50
+
51
+ ## 0.0.8
52
+ - bugfix: single currency was being persisted as a yaml object
53
+ - #currency and #some_column_currency are both assigned
54
+ and persisted as a downcase string
55
+ eg. "inr" in the case of Indian Rupee
56
+ so that, Money.default_currency.id == "inr".to_sym
57
+ holds true
58
+ api is changed. previously #currency and #single_currency
59
+ gave a Money::Currency object. now is is a String
data/README.md CHANGED
@@ -372,3 +372,8 @@ loan_usd.currency # equals Money::Currency.new(:usd)
372
372
  is dependent on the database adapter
373
373
  being used, which sucks. can test on other database adapters
374
374
  or handle a generic error
375
+ 13. add typecast for currency column
376
+ right now, it is always a string
377
+ do we want a Money::Currency object back?
378
+ not decided
379
+ see currency_persistence_spec.rb
@@ -28,7 +28,7 @@ module EasyRailsMoney
28
28
  end
29
29
 
30
30
  def with_currency currency, &block
31
- self.single_currency = EasyRailsMoney::MoneyDslHelper.to_currency(currency)
31
+ self.single_currency = EasyRailsMoney::MoneyDslHelper.to_currency(currency).id.to_s
32
32
  instance_eval &block
33
33
  end
34
34
 
@@ -37,7 +37,7 @@ module EasyRailsMoney
37
37
  # single currency is defined
38
38
  if single_currency?
39
39
  if attributes && attributes[:currency]
40
- instance.currency = EasyRailsMoney::MoneyDslHelper.to_currency attributes[:currency]
40
+ instance.currency = EasyRailsMoney::MoneyDslHelper.to_currency(attributes[:currency]).id.to_s
41
41
  else
42
42
  instance.currency = instance.class.single_currency
43
43
  end
@@ -61,6 +61,14 @@ module EasyRailsMoney
61
61
  nil
62
62
  end
63
63
  end
64
+
65
+ define_method "#{column_name}=" do |value|
66
+ raise ::ArgumentError.new("only Integer or nil accepted") unless (value.kind_of?(Integer) || value.is_a?(NilClass))
67
+
68
+ send("#{money_column}=", value)
69
+ # currency is stored in a seperate common column
70
+ return Money.new(value, self.currency)
71
+ end # define_method setter
64
72
  else
65
73
  # TODO: test if Memoization will make any difference
66
74
  define_method column_name do |*args|
@@ -73,20 +81,10 @@ module EasyRailsMoney
73
81
  nil
74
82
  end
75
83
  end
76
- end
77
-
78
- if single_currency?
79
- define_method "#{column_name}=" do |value|
80
- raise ::ArgumentError.new("only Integer or nil accepted") unless (value.kind_of?(Integer) || value.is_a?(NilClass))
81
-
82
- send("#{money_column}=", value)
83
- # currency is stored in a seperate common column
84
- return Money.new(value, self.currency)
85
- end # define_method setter
86
- else
84
+
87
85
  define_method "#{column_name}=" do |value|
88
86
  raise ::ArgumentError.new("only Money or nil accepted") unless (value.kind_of?(Money) || value.is_a?(NilClass))
89
-
87
+
90
88
  if value
91
89
  send("#{money_column}=", value.fractional)
92
90
  # it is stored in the database as a string but the Money
@@ -100,7 +98,7 @@ module EasyRailsMoney
100
98
  return nil
101
99
  end
102
100
  end # define_method setter
103
- end # if single_currency?
101
+ end
104
102
  end # def money
105
103
  end # module ClassMethods
106
104
 
@@ -1,3 +1,3 @@
1
1
  module EasyRailsMoney
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require 'active_record_spec_helper'
3
+
4
+ # add test that the currency will be serialized in a fixed format
5
+ #
6
+ # currency (single or otherwise) is always a string
7
+ # and it is in smallcase
8
+ # because, Money.default_currency.id == "inr".to_sym
9
+ #
10
+ # previously, single_currency was a Money::Currency object
11
+ # whereas some_money_currency was a string object
12
+ #
13
+ # bugfix: also the currency object was being serialized as a yaml
14
+ # which was a bug. facepalm stupid
15
+ #
16
+ # this was added so that adding validations are easier
17
+ # ie. no need to make a case-insensitive check
18
+ # and to fix the above bug
19
+ #
20
+ # not very happy. might want to add typecasts
21
+
22
+ describe "Currency Persistence" do
23
+ before(:all) do
24
+ currencies = ["inr", # Indian rupee
25
+ "usd", # USA dollars
26
+ "aud", # Australian dollar
27
+ "eur", # Euro
28
+ "gbp" # British pound
29
+ ]
30
+ @currency = currencies.shuffle[0]
31
+
32
+ # TODO: how to couple this choice with rspec seed
33
+ # take a random currency so that we do not depend on any one
34
+ # default_currency while testing
35
+ EasyRailsMoney.configure do |c|
36
+ c.default_currency = Money::Currency.new(@currency)
37
+ end
38
+ end
39
+
40
+ context "individual currency", :wip do
41
+ let(:loan_model) {
42
+ migrate CreateTableDefinition::CreateLoanWithoutCurrency
43
+ require 'loan_model_spec_helper'
44
+ Loan
45
+ }
46
+
47
+ subject {
48
+ loan = loan_model.new
49
+ loan.principal = 100.to_money(@currency)
50
+ loan.repaid = 50.to_money(@currency)
51
+ loan.npa = 10.to_money(@currency)
52
+ loan
53
+ }
54
+
55
+ it "currency should be capitalized" do
56
+ subject.save!
57
+
58
+ expect(subject.principal_currency).to eq @currency
59
+ expect(subject.principal_currency_before_type_cast).to eq @currency
60
+ end
61
+
62
+ it "currency should be capitalized for a new object" do
63
+ subject.save!
64
+ loan = loan_model.find subject.id
65
+
66
+ expect(subject.principal_currency).to eq @currency
67
+ expect(subject.principal_currency_before_type_cast).to eq @currency
68
+ end
69
+ end
70
+
71
+ context "single currency" do
72
+ let(:loan_model) {
73
+ migrate CreateTableDefinition::CreateLoanWithCurrency
74
+ require 'loan_with_currency_model_spec_helper'
75
+ LoanWithCurrency
76
+ }
77
+
78
+ subject {
79
+ loan = loan_model.new
80
+ loan.currency = @currency
81
+ loan.principal = 100 * 100
82
+ loan.repaid = 50 * 100
83
+ loan.npa = 10 * 100
84
+ loan
85
+ }
86
+
87
+ it "currency should be capitalized" do
88
+ subject.save!
89
+
90
+ expect(subject.currency).to eq @currency
91
+ expect(subject.currency_before_type_cast).to eq @currency
92
+ end
93
+
94
+ it "currency should be capitalized for a new object" do
95
+ subject.save!
96
+ loan = loan_model.find subject.id
97
+
98
+ expect(loan.currency).to eq @currency
99
+ expect(loan.currency_before_type_cast).to eq @currency
100
+ end
101
+ end
102
+ end
@@ -1,193 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'active_record_spec_helper'
3
- require 'active_record/schema_dumper'
4
-
5
- # migrations used in tests. our factories/fixtures
6
- # their class names are the same if they are functionally
7
- # equivalent, but are organized in different modules depending on
8
- # whether it is implemented using
9
- # schema_statements, create_table or change_table
10
- module SchemaStatements
11
- class CreateLoanAndMoney < ActiveRecord::Migration
12
- def change
13
- suppress_messages do
14
- create_table :loans do |t|
15
- t.string :name
16
- end
17
- add_money :loans, :principal
18
- end
19
- end
20
- end
21
-
22
- class CreateLoanWithCurrency < ActiveRecord::Migration
23
- def change
24
- suppress_messages do
25
- create_table :loans do |t|
26
- t.string :name
27
- t.currency
28
- end
29
- add_money :loans, :principal, :repaid, :npa
30
- end
31
- end
32
- end
33
-
34
- class RemoveMoneyColumnsFromLoan < ActiveRecord::Migration
35
- def change
36
- suppress_messages do
37
- remove_money :loans, :principal, :repaid, :npa
38
- end
39
- end
40
- end
41
-
42
- class RemoveMoneyColumnsExceptPrincipalFromLoan < ActiveRecord::Migration
43
- def change
44
- suppress_messages do
45
- remove_money :loans, :repaid, :npa
46
- end
47
- end
48
- end
49
-
50
- class RemovePrincipalFromLoan < ActiveRecord::Migration
51
- def change
52
- suppress_messages do
53
- remove_money :loans, :principal
54
- end
55
- end
56
- end
57
-
58
- class RemoveCurrencyFromLoan < ActiveRecord::Migration
59
- def change
60
- suppress_messages do
61
- remove_currency :loans
62
- end
63
- end
64
- end
65
- end # module SchemaStatements
66
-
67
- module ChangeTable
68
- class AddPrincipalToLoan < ActiveRecord::Migration
69
- def change
70
- suppress_messages do
71
- change_table :loans do |t|
72
- t.money :principal
73
- end
74
- end
75
- end
76
- end
77
-
78
- class RemoveCurrencyFromLoan < ActiveRecord::Migration
79
- def change
80
- suppress_messages do
81
- change_table :loans, force: true do |t|
82
- t.remove_currency
83
- end
84
- end
85
- end
86
- end
87
-
88
- class RemovePrincipalFromLoan < ActiveRecord::Migration
89
- def change
90
- suppress_messages do
91
- change_table :loans do |t|
92
- t.remove_money :principal
93
- end
94
- end
95
- end
96
- end
97
-
98
- class AddSingleCurrencyToLoan < ActiveRecord::Migration
99
- def change
100
- suppress_messages do
101
- change_table :loans, force: true do |t|
102
- t.currency
103
- end
104
- end
105
- end
106
- end
107
-
108
- class RemoveMoneyColumnsFromLoan < ActiveRecord::Migration
109
- def change
110
- suppress_messages do
111
- change_table :loans, force: true do |t|
112
- t.remove_money :principal, :repaid, :npa
113
- end
114
- end
115
- end
116
- end
117
-
118
- class RemoveMoneyColumnsExceptPrincipalFromLoan < ActiveRecord::Migration
119
- def change
120
- suppress_messages do
121
- change_table :loans, force: true do |t|
122
- t.remove_money :repaid, :npa
123
- end
124
- end
125
- end
126
- end
127
- end # module ChangeTable
128
-
129
- module CreateTableDefinition
130
- class CreateLoan < ActiveRecord::Migration
131
- def change
132
- suppress_messages do
133
- create_table :loans do |t|
134
- t.string :name
135
- end
136
- end
137
- end
138
- end
139
-
140
- class CreateLoanAndMoney < ActiveRecord::Migration
141
- def change
142
- suppress_messages do
143
- create_table :loans do |t|
144
- t.string :name
145
- t.money :principal
146
- end
147
- end
148
- end
149
- end
150
-
151
- class CreateLoanWithConstraint < ActiveRecord::Migration
152
- def change
153
- suppress_messages do
154
- create_table :loans, :force => true do |t|
155
- t.string :name
156
- t.currency :null => false
157
- t.money :principal, :null => false
158
- end
159
- end
160
- end
161
- end
162
-
163
- class CreateLoanWithCurrencySpecifiedFirst < ActiveRecord::Migration
164
- def change
165
- suppress_messages do
166
- create_table :loans, force: true do |t|
167
- t.string :name
168
- t.currency
169
- t.money :principal
170
- t.money :repaid
171
- t.money :npa
172
- end
173
- end
174
- end
175
- end
176
-
177
- class CreateLoanWithCurrencySpecifiedInBetween < ActiveRecord::Migration
178
- def change
179
- suppress_messages do
180
- create_table :loans, force: true do |t|
181
- t.string :name
182
- t.money :principal
183
- t.money :repaid
184
- t.currency
185
- t.money :npa
186
- end
187
- end
188
- end
189
- end
190
- end # module CreateTableDefinition
3
+ require 'migration_factory_spec_helper'
191
4
 
192
5
  describe "Migrating Money columns" do
193
6
 
@@ -144,13 +144,13 @@ describe "Money DSL" do
144
144
  end
145
145
 
146
146
  it "sets currency column" do
147
- expect(subject.currency).to eq ::Money::Currency.new(:inr)
147
+ expect(subject.currency).to eq "inr"
148
148
  end
149
149
 
150
150
  it "can change single currency on an instance" do
151
151
  loan = LoanWithCurrency.new(currency: :usd)
152
- expect(loan.currency).to eq ::Money::Currency.new(:usd)
153
- expect(loan.class.single_currency).to eq ::Money::Currency.new(:inr)
152
+ expect(loan.currency).to eq "usd"
153
+ expect(loan.class.single_currency).to eq "inr"
154
154
  end
155
155
 
156
156
  describe "#getter" do
@@ -1,3 +1,4 @@
1
+ require 'active_record/schema_dumper'
1
2
  require 'tempfile'
2
3
 
3
4
  module CreateTableDefinition
@@ -0,0 +1,186 @@
1
+ # migrations used in tests. our factories/fixtures
2
+ # their class names are the same if they are functionally
3
+ # equivalent, but are organized in different modules depending on
4
+ # whether it is implemented using
5
+ # schema_statements, create_table or change_table
6
+ module SchemaStatements
7
+ class CreateLoanAndMoney < ActiveRecord::Migration
8
+ def change
9
+ suppress_messages do
10
+ create_table :loans do |t|
11
+ t.string :name
12
+ end
13
+ add_money :loans, :principal
14
+ end
15
+ end
16
+ end
17
+
18
+ class CreateLoanWithCurrency < ActiveRecord::Migration
19
+ def change
20
+ suppress_messages do
21
+ create_table :loans do |t|
22
+ t.string :name
23
+ t.currency
24
+ end
25
+ add_money :loans, :principal, :repaid, :npa
26
+ end
27
+ end
28
+ end
29
+
30
+ class RemoveMoneyColumnsFromLoan < ActiveRecord::Migration
31
+ def change
32
+ suppress_messages do
33
+ remove_money :loans, :principal, :repaid, :npa
34
+ end
35
+ end
36
+ end
37
+
38
+ class RemoveMoneyColumnsExceptPrincipalFromLoan < ActiveRecord::Migration
39
+ def change
40
+ suppress_messages do
41
+ remove_money :loans, :repaid, :npa
42
+ end
43
+ end
44
+ end
45
+
46
+ class RemovePrincipalFromLoan < ActiveRecord::Migration
47
+ def change
48
+ suppress_messages do
49
+ remove_money :loans, :principal
50
+ end
51
+ end
52
+ end
53
+
54
+ class RemoveCurrencyFromLoan < ActiveRecord::Migration
55
+ def change
56
+ suppress_messages do
57
+ remove_currency :loans
58
+ end
59
+ end
60
+ end
61
+ end # module SchemaStatements
62
+
63
+ module ChangeTable
64
+ class AddPrincipalToLoan < ActiveRecord::Migration
65
+ def change
66
+ suppress_messages do
67
+ change_table :loans do |t|
68
+ t.money :principal
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ class RemoveCurrencyFromLoan < ActiveRecord::Migration
75
+ def change
76
+ suppress_messages do
77
+ change_table :loans, force: true do |t|
78
+ t.remove_currency
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ class RemovePrincipalFromLoan < ActiveRecord::Migration
85
+ def change
86
+ suppress_messages do
87
+ change_table :loans do |t|
88
+ t.remove_money :principal
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ class AddSingleCurrencyToLoan < ActiveRecord::Migration
95
+ def change
96
+ suppress_messages do
97
+ change_table :loans, force: true do |t|
98
+ t.currency
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ class RemoveMoneyColumnsFromLoan < ActiveRecord::Migration
105
+ def change
106
+ suppress_messages do
107
+ change_table :loans, force: true do |t|
108
+ t.remove_money :principal, :repaid, :npa
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ class RemoveMoneyColumnsExceptPrincipalFromLoan < ActiveRecord::Migration
115
+ def change
116
+ suppress_messages do
117
+ change_table :loans, force: true do |t|
118
+ t.remove_money :repaid, :npa
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end # module ChangeTable
124
+
125
+ module CreateTableDefinition
126
+ class CreateLoan < ActiveRecord::Migration
127
+ def change
128
+ suppress_messages do
129
+ create_table :loans do |t|
130
+ t.string :name
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ class CreateLoanAndMoney < ActiveRecord::Migration
137
+ def change
138
+ suppress_messages do
139
+ create_table :loans do |t|
140
+ t.string :name
141
+ t.money :principal
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ class CreateLoanWithConstraint < ActiveRecord::Migration
148
+ def change
149
+ suppress_messages do
150
+ create_table :loans, :force => true do |t|
151
+ t.string :name
152
+ t.currency :null => false
153
+ t.money :principal, :null => false
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ class CreateLoanWithCurrencySpecifiedFirst < ActiveRecord::Migration
160
+ def change
161
+ suppress_messages do
162
+ create_table :loans, force: true do |t|
163
+ t.string :name
164
+ t.currency
165
+ t.money :principal
166
+ t.money :repaid
167
+ t.money :npa
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ class CreateLoanWithCurrencySpecifiedInBetween < ActiveRecord::Migration
174
+ def change
175
+ suppress_messages do
176
+ create_table :loans, force: true do |t|
177
+ t.string :name
178
+ t.money :principal
179
+ t.money :repaid
180
+ t.currency
181
+ t.money :npa
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end # module CreateTableDefinition
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.7
4
+ version: 0.0.8
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: 2013-04-08 00:00:00.000000000 Z
12
+ date: 2013-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: money
@@ -196,12 +196,14 @@ files:
196
196
  - lib/easy_rails_money/configuration.rb
197
197
  - lib/easy_rails_money/money_dsl_helper.rb
198
198
  - lib/easy_rails_money/version.rb
199
+ - spec/active_record/currency_persistence_spec.rb
199
200
  - spec/active_record/migration_spec.rb
200
201
  - spec/active_record/money_dsl_spec.rb
201
202
  - spec/active_record_spec_helper.rb
202
203
  - spec/configuration_spec.rb
203
204
  - spec/loan_model_spec_helper.rb
204
205
  - spec/loan_with_currency_model_spec_helper.rb
206
+ - spec/migration_factory_spec_helper.rb
205
207
  - spec/simplecov_helper.rb
206
208
  - spec/spec_helper.rb
207
209
  - vendor/cache/activemodel-3.2.12.gem
@@ -242,7 +244,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
242
244
  version: '0'
243
245
  segments:
244
246
  - 0
245
- hash: -1659709329796999212
247
+ hash: -3822270686044656207
246
248
  required_rubygems_version: !ruby/object:Gem::Requirement
247
249
  none: false
248
250
  requirements:
@@ -251,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
251
253
  version: '0'
252
254
  segments:
253
255
  - 0
254
- hash: -1659709329796999212
256
+ hash: -3822270686044656207
255
257
  requirements: []
256
258
  rubyforge_project:
257
259
  rubygems_version: 1.8.24
@@ -259,12 +261,14 @@ signing_key:
259
261
  specification_version: 3
260
262
  summary: Integrate Rail's ActiveRecord gem and the money gem
261
263
  test_files:
264
+ - spec/active_record/currency_persistence_spec.rb
262
265
  - spec/active_record/migration_spec.rb
263
266
  - spec/active_record/money_dsl_spec.rb
264
267
  - spec/active_record_spec_helper.rb
265
268
  - spec/configuration_spec.rb
266
269
  - spec/loan_model_spec_helper.rb
267
270
  - spec/loan_with_currency_model_spec_helper.rb
271
+ - spec/migration_factory_spec_helper.rb
268
272
  - spec/simplecov_helper.rb
269
273
  - spec/spec_helper.rb
270
274
  has_rdoc: