activerecord-typedstore 0.4.3 → 0.4.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: feff41b09759e6bba17c41f3b4ab635286d7852f
4
- data.tar.gz: 8ea136dccb104e0a43ad7267d425bdc2016cf5d3
3
+ metadata.gz: 805a07504ab5ed0b6a41983d945f5a0bdd649726
4
+ data.tar.gz: c09ffc9348aa4256c439645b7d36edb0b3951705
5
5
  SHA512:
6
- metadata.gz: 64a75b48fe0f9bd9e768b1fe5947f43ffee8eeef182057493e31958ede64d4f2f8c78292343c3cb8e74d48b605070cd953cbe0b3a96bcdd1b0d733a8294b0ae2
7
- data.tar.gz: 4e983b5546295199cc3cbc7dcf5ce5109693bd272b7185b9d4d1af05db5538ac5443f5b05834fe911f799ed9602e2c241bc39524bbc9e0d229cd808c458f0b35
6
+ metadata.gz: 2c033c463261169f42b5ab7e1a4c0dade01d101c1f084695124b5e018131bbce84a389b34bfc9aedeec224383def213cd6cf4c78b609d820cffbf8fd1cdaf695
7
+ data.tar.gz: 6ed412a72266d529d32401c6ef99a7bfc85fd5d38cf7716f8e44035cc1b1250099b67e3aa9e153a5eab1e97e4a2669fa1f7596fa2fb271aa1404274fabf94020
@@ -9,4 +9,3 @@ gem 'pg', '~> 0.11'
9
9
  gem 'mysql2'
10
10
  gem 'database_cleaner'
11
11
  gem 'coveralls', require: false
12
- gem 'debugger'
@@ -9,4 +9,3 @@ gem 'pg', '~> 0.11'
9
9
  gem 'mysql2'
10
10
  gem 'database_cleaner'
11
11
  gem 'coveralls', require: false
12
- gem 'debugger'
@@ -14,7 +14,7 @@ module ActiveRecord::TypedStore
14
14
  @columns.select(&:accessor?).map(&:name)
15
15
  end
16
16
 
17
- [:string, :integer, :float, :decimal, :datetime, :date, :boolean, :any].each do |type|
17
+ [:string, :text, :integer, :float, :decimal, :datetime, :date, :boolean, :any].each do |type|
18
18
  define_method(type) do |name, options={}|
19
19
  @columns << Column.new(name, type, options.reverse_merge(accessor: @accessors))
20
20
  end
@@ -61,10 +61,17 @@ module ActiveRecord::TypedStore
61
61
  return if @typed_store_attribute_methods_generated
62
62
  store_accessors.each do |attribute|
63
63
  define_virtual_attribute_method(attribute)
64
+ undefine_before_type_cast_method(attribute)
64
65
  end
65
66
  @typed_store_attribute_methods_generated = true
66
67
  end
67
68
 
69
+ def undefine_before_type_cast_method(attribute)
70
+ # because it mess with ActionView forms, see #14.
71
+ method = "#{attribute}_before_type_cast"
72
+ undef_method(method) if method_defined?(method)
73
+ end
74
+
68
75
  def store_accessors
69
76
  return [] unless typed_store_attributes
70
77
  typed_store_attributes.values.select(&:accessor?).map(&:name).map(&:to_s)
@@ -92,7 +99,8 @@ module ActiveRecord::TypedStore
92
99
  end
93
100
 
94
101
  previous_value = read_store_attribute(store_attribute, key)
95
- attribute_will_change!(key.to_s) if value != previous_value
102
+ new_value = column ? column.type_cast(value) : value
103
+ attribute_will_change!(key.to_s) if new_value != previous_value
96
104
  super
97
105
  end
98
106
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module TypedStore
3
- VERSION = '0.4.3'
3
+ VERSION = '0.4.4'
4
4
  end
5
5
  end
@@ -27,7 +27,7 @@ shared_examples 'any model' do
27
27
 
28
28
  it 'assign attributes received by #initialize' do
29
29
  model = described_class.new(public: true)
30
- expect(model.public).to be_true
30
+ expect(model.public).to be true
31
31
  end
32
32
 
33
33
  end
@@ -65,6 +65,38 @@ shared_examples 'any model' do
65
65
  }.to change { model.age }.from(24).to(12)
66
66
  end
67
67
 
68
+ it 'does not dirty track assigning the same boolean' do
69
+ expect(model.enabled).to be true
70
+ expect {
71
+ model.enabled = true
72
+ }.to_not change { model.enabled_changed? }
73
+ end
74
+
75
+ it 'dirty tracks when the boolean changes' do
76
+ expect(model.enabled).to be true
77
+ expect {
78
+ model.enabled = false
79
+ }.to change { model.enabled_changed? }.from(false).to(true)
80
+ end
81
+
82
+ it 'does not dirty track assigning the same boolean even if it is a string' do
83
+ expect(model.enabled).to be true
84
+ expect {
85
+ model.enabled = "true"
86
+ }.to_not change { model.enabled_changed? }
87
+ end
88
+
89
+ it 'dirty tracks when the string changes' do
90
+ expect {
91
+ model.name = "Smith"
92
+ }.to change { model.name_changed? }.from(false).to(true)
93
+ end
94
+
95
+ it 'does not dirty track assigning the same string' do
96
+ expect {
97
+ model.name = ""
98
+ }.to_not change { model.name_changed? }
99
+ end
68
100
  end
69
101
 
70
102
  describe 'unknown attribute' do
@@ -108,15 +140,15 @@ shared_examples 'any model' do
108
140
 
109
141
  it 'any string is considered present' do
110
142
  model.name = 'Peter Gibbons'
111
- expect(model.name?).to be_true
143
+ expect(model.name?).to be true
112
144
  end
113
145
 
114
146
  it 'empty string is not considered present' do
115
- expect(model.name?).to be_false
147
+ expect(model.name?).to be false
116
148
  end
117
149
 
118
150
  it 'nil is not considered present' do
119
- expect(model.cell_phone?).to be_false
151
+ expect(model.cell_phone?).to be false
120
152
  end
121
153
 
122
154
  it 'not define the attributes more than one time' do
@@ -129,14 +161,14 @@ shared_examples 'any model' do
129
161
  describe 'boolean attribute' do
130
162
 
131
163
  it 'has the defined :default as initial value' do
132
- expect(model.public).to be_false
164
+ expect(model.public).to be false
133
165
  end
134
166
 
135
167
  [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].each do |value|
136
168
 
137
169
  it "cast `#{value.inspect}` as `true`" do
138
170
  model.public = value
139
- expect(model.public).to be_true
171
+ expect(model.public).to be true
140
172
  end
141
173
 
142
174
  end
@@ -145,22 +177,22 @@ shared_examples 'any model' do
145
177
 
146
178
  it "cast `#{value.inspect}` as `false`" do
147
179
  model.public = value
148
- expect(model.public).to be_false
180
+ expect(model.public).to be false
149
181
  end
150
182
 
151
183
  end
152
184
 
153
185
  it 'properly persit the value' do
154
186
  model.update_attributes(public: false)
155
- expect(model.reload.public).to be_false
187
+ expect(model.reload.public).to be false
156
188
  model.update_attributes(public: true)
157
- expect(model.reload.public).to be_true
189
+ expect(model.reload.public).to be true
158
190
  end
159
191
 
160
192
  it 'initialize with default value if the column is not nullable' do
161
- expect(model.public).to be_false
193
+ expect(model.public).to be false
162
194
  model.save
163
- expect(model.reload.public).to be_false
195
+ expect(model.reload.public).to be false
164
196
  end
165
197
 
166
198
  it 'can store nil if the column is nullable' do
@@ -170,20 +202,20 @@ shared_examples 'any model' do
170
202
 
171
203
  it 'save the default value if the column is nullable but the value not explictly set' do
172
204
  model.save
173
- expect(model.reload.enabled).to be_true
205
+ expect(model.reload.enabled).to be true
174
206
  end
175
207
 
176
208
  it 'true is considered present' do
177
- expect(model.enabled?).to be_true
209
+ expect(model.enabled?).to be true
178
210
  end
179
211
 
180
212
  it 'false is not considered present' do
181
- expect(model.public?).to be_false
213
+ expect(model.public?).to be false
182
214
  end
183
215
 
184
216
  it 'nil is not considered present' do
185
217
  model.update_attributes(enabled: nil)
186
- expect(model.enabled?).to be_false
218
+ expect(model.enabled?).to be false
187
219
  end
188
220
 
189
221
  end
@@ -210,22 +242,22 @@ shared_examples 'any model' do
210
242
  end
211
243
 
212
244
  it 'positive values are considered present' do
213
- expect(model.age?).to be_true
245
+ expect(model.age?).to be true
214
246
  end
215
247
 
216
248
  it 'negative values are considered present' do
217
249
  model.age = -42
218
- expect(model.age?).to be_true
250
+ expect(model.age?).to be true
219
251
  end
220
252
 
221
253
  it '0 is not considered present' do
222
254
  model.age = 0
223
- expect(model.age?).to be_false
255
+ expect(model.age?).to be false
224
256
  end
225
257
 
226
258
  it 'nil is not considered present' do
227
259
  model.max_length = nil
228
- expect(model.max_length?).to be_false
260
+ expect(model.max_length?).to be false
229
261
  end
230
262
 
231
263
  end
@@ -253,20 +285,20 @@ shared_examples 'any model' do
253
285
 
254
286
  it 'positive values are considered present' do
255
287
  model.rate = 4.2
256
- expect(model.rate?).to be_true
288
+ expect(model.rate?).to be true
257
289
  end
258
290
 
259
291
  it 'negative values are considered present' do
260
292
  model.rate = -4.2
261
- expect(model.rate?).to be_true
293
+ expect(model.rate?).to be true
262
294
  end
263
295
 
264
296
  it '0 is not considered present' do
265
- expect(model.rate?).to be_false
297
+ expect(model.rate?).to be false
266
298
  end
267
299
 
268
300
  it 'nil is not considered present' do
269
- expect(model.price?).to be_false
301
+ expect(model.price?).to be false
270
302
  end
271
303
 
272
304
  end
@@ -303,21 +335,21 @@ shared_examples 'any model' do
303
335
 
304
336
  it 'positive values are considered present' do
305
337
  model.shipping_cost = BigDecimal.new('4.2')
306
- expect(model.shipping_cost?).to be_true
338
+ expect(model.shipping_cost?).to be true
307
339
  end
308
340
 
309
341
  it 'negative values are considered present' do
310
342
  model.shipping_cost = BigDecimal.new('-4.2')
311
- expect(model.shipping_cost?).to be_true
343
+ expect(model.shipping_cost?).to be true
312
344
  end
313
345
 
314
346
  it '0 is not considered present' do
315
347
  model.shipping_cost = BigDecimal.new('0')
316
- expect(model.shipping_cost?).to be_false
348
+ expect(model.shipping_cost?).to be false
317
349
  end
318
350
 
319
351
  it 'nil is not considered present' do
320
- expect(model.shipping_cost?).to be_false
352
+ expect(model.shipping_cost?).to be false
321
353
  end
322
354
 
323
355
  end
@@ -352,11 +384,11 @@ shared_examples 'any model' do
352
384
 
353
385
  it 'any non-nil value is considered present' do
354
386
  model.remind_on = Date.new
355
- expect(model.remind_on?).to be_true
387
+ expect(model.remind_on?).to be true
356
388
  end
357
389
 
358
390
  it 'nil is not considered present' do
359
- expect(model.remind_on?).to be_false
391
+ expect(model.remind_on?).to be false
360
392
  end
361
393
 
362
394
  end
@@ -437,11 +469,11 @@ shared_examples 'any model' do
437
469
 
438
470
  it 'any non-nil value is considered present' do
439
471
  model.remind_at = DateTime.new
440
- expect(model.remind_at?).to be_true
472
+ expect(model.remind_at?).to be true
441
473
  end
442
474
 
443
475
  it 'nil is not considered present' do
444
- expect(model.remind_at?).to be_false
476
+ expect(model.remind_at?).to be false
445
477
  end
446
478
 
447
479
  end
@@ -489,6 +521,10 @@ shared_examples 'a store' do |retain_type=true|
489
521
 
490
522
  end
491
523
 
524
+ it 'do not respond to <attribute>_before_type_cast' do
525
+ expect(model).to_not respond_to :nickname_before_type_cast
526
+ end
527
+
492
528
  end
493
529
 
494
530
  describe 'attributes without accessors' do
@@ -538,7 +574,7 @@ shared_examples 'a store' do |retain_type=true|
538
574
  begin
539
575
  model.class::SettingsHash.columns['brand_new'] = new_column
540
576
  model.reload
541
- expect(model.settings[:brand_new]).to be_true
577
+ expect(model.settings[:brand_new]).to be true
542
578
  ensure
543
579
  model.class::SettingsHash.columns.delete('brand_new')
544
580
  end
@@ -16,8 +16,5 @@ require 'activerecord-typedstore'
16
16
  Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }
17
17
 
18
18
  RSpec.configure do |config|
19
- config.treat_symbols_as_metadata_keys_with_true_values = true
20
- config.run_all_when_everything_filtered = true
21
- config.filter_run :focus
22
19
  config.order = 'random'
23
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-typedstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2014-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  version: '0'
190
190
  requirements: []
191
191
  rubyforge_project:
192
- rubygems_version: 2.2.1
192
+ rubygems_version: 2.2.2
193
193
  signing_key:
194
194
  specification_version: 4
195
195
  summary: ActiveRecord::Store but with type definition