/
active_record_spec.rb
412 lines (353 loc) · 13.7 KB
/
active_record_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
require 'spec_helper'
require 'ostruct'
describe AssignableValues::ActiveRecord do
def disposable_song_class(&block)
klass = Class.new(Song) #, &block)
def klass.name
'Song'
end
klass.class_eval(&block) if block
klass
end
describe '.assignable_values' do
it 'should raise an error when not called with a block or :through option' do
expect do
disposable_song_class do
assignable_values_for :genre
end
end.to raise_error(AssignableValues::NoValuesGiven)
end
context 'when validating scalar attributes' do
context 'without options' do
before :each do
@klass = disposable_song_class do
assignable_values_for :genre do
%w[pop rock]
end
end
end
it 'should validate that the attribute is allowed' do
@klass.new(:genre => 'pop').should be_valid
@klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should use the same error message as validates_inclusion_of' do
record = @klass.new(:genre => 'disallowed value')
record.valid?
errors = record.errors[:genre]
error = errors.is_a?(Array) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == I18n.t('errors.messages.inclusion')
error.should == 'is not included in the list'
end
it 'should not allow nil for the attribute value' do
@klass.new(:genre => nil).should_not be_valid
end
it 'should allow a previously saved value even if that value is no longer allowed' do
record = @klass.create!(:genre => 'pop')
@klass.update_all(:genre => 'disallowed value') # update without validations for the sake of this test
record.reload.should be_valid
end
it 'should generate a method returning the humanized value' do
song = @klass.new(:genre => 'pop')
song.humanized_genre.should == 'Pop music'
end
it 'should generate a method returning the humanized value, which is nil when the value is blank' do
song = @klass.new
song.genre = nil
song.humanized_genre.should be_nil
song.genre = ''
song.humanized_genre.should be_nil
end
end
context 'if the :allow_blank option is set' do
before :each do
@klass = disposable_song_class do
assignable_values_for :genre, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should allow nil for the attribute value' do
@klass.new(:genre => nil).should be_valid
end
it 'should allow an empty string as value if the :allow_blank option is set' do
@klass.new(:genre => '').should be_valid
end
end
end
context 'when validating belongs_to associations' do
it 'should validate that the association is allowed' do
allowed_association = Artist.create!
disallowed_association = Artist.create!
klass = disposable_song_class do
assignable_values_for :artist do
[allowed_association]
end
end
klass.new(:artist => allowed_association).should be_valid
klass.new(:artist => disallowed_association).should_not be_valid
end
it 'should allow a nil association if the :allow_blank option is set' do
klass = disposable_song_class do
assignable_values_for :artist, :allow_blank => true do
[]
end
end
record = klass.new
record.artist.should be_nil
record.should be_valid
end
it 'should allow a previously saved association even if that association is no longer allowed' do
allowed_association = Artist.create!
disallowed_association = Artist.create!
klass = disposable_song_class
record = klass.create!(:artist => disallowed_association)
klass.class_eval do
assignable_values_for :artist do
[allowed_association]
end
end
record.should be_valid
end
it "should not load a previously saved association if the association's foreign key hasn't changed" do
association = Artist.create!
klass = disposable_song_class do
assignable_values_for :artist do
[association] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
record = klass.create!(:artist => association)
Artist.should_not_receive(:find_by_id)
record.valid?
end
it 'should not fail or allow nil if a previously saved association no longer exists in the database' do
allowed_association = Artist.create!
klass = disposable_song_class do
assignable_values_for :artist do
[allowed_association]
end
end
record = klass.new
record.stub :artist_id_was => -1
record.should_not be_valid
end
it 'should uncache a stale association before validating' do
klass = disposable_song_class do
assignable_values_for :artist do
[] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
association = Artist.create!
record = klass.new
record.stub(:artist => association, :artist_id => -1) # This is a stale association: The associated object's id doesn't match the foreign key. This can happen in Rails 2, not Rails 3.
record.should_receive(:artist).ordered.and_return(association)
record.should_receive(:artist).ordered.with(true).and_return(association)
record.valid?
end
it 'should not uncache a fresh association before validating' do
klass = disposable_song_class do
assignable_values_for :artist do
[] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
association = Artist.create!
record = klass.new
record.stub(:artist => association, :artist_id => association.id) # This is a fresh association: The associated object's id matches the foreign key.
record.should_receive(:artist).with(no_args).and_return(association)
record.valid?
end
end
context 'when delegating using the :through option' do
it 'should obtain allowed values from a method with the given name' do
klass = disposable_song_class do
assignable_values_for :genre, :through => :delegate
def delegate
OpenStruct.new(:assignable_song_genres => %w[pop rock])
end
end
klass.new(:genre => 'pop').should be_valid
klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should be able to delegate to a lambda, which is evaluated in the context of the record instance' do
klass = disposable_song_class do
assignable_values_for :genre, :through => lambda { delegate }
def delegate
OpenStruct.new(:assignable_song_genres => %w[pop rock])
end
end
klass.new(:genre => 'pop').should be_valid
klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should skip the validation if that method returns nil' do
klass = disposable_song_class do
assignable_values_for :genre, :through => :delegate
def delegate
nil
end
end
klass.new(:genre => 'pop').should be_valid
end
end
context 'with :default option' do
it 'should allow to set a default' do
klass = disposable_song_class do
assignable_values_for :genre, :default => 'pop' do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it 'should allow to set a default through a lambda' do
klass = disposable_song_class do
assignable_values_for :genre, :default => lambda { 'pop' } do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it 'should evaluate a lambda default in the context of the record instance' do
klass = disposable_song_class do
assignable_values_for :genre, :default => lambda { default_genre } do
%w[pop rock]
end
def default_genre
'pop'
end
end
klass.new.genre.should == 'pop'
end
end
context 'when generating methods to list assignable values' do
it 'should generate an instance method returning a list of assignable values' do
klass = disposable_song_class do
assignable_values_for :genre do
%w[pop rock]
end
end
klass.new.assignable_genres.should == %w[pop rock]
end
it 'should call #to_a on the list of assignable values, allowing ranges and scopes to be passed as allowed value descriptors' do
klass = disposable_song_class do
assignable_values_for :year do
1999..2001
end
end
klass.new.assignable_years.should == [1999, 2000, 2001]
end
it 'should evaluate the value block in the context of the record instance' do
klass = disposable_song_class do
assignable_values_for :genre do
genres
end
def genres
%w[pop rock]
end
end
klass.new.assignable_genres.should == %w[pop rock]
end
it 'should include a previously saved value, even if is no longer allowed' do
klass = disposable_song_class do
assignable_values_for :genre do
%w[pop rock]
end
end
record = klass.create!(:genre => 'pop')
klass.update_all(:genre => 'ballad') # update without validation for the sake of this test
record.reload.assignable_genres.should =~ %w[pop rock ballad]
end
context 'humanization' do
it "should define a method #humanized on strings in that list, which return up the value's' translation" do
klass = disposable_song_class do
assignable_values_for :genre do
%w[pop rock]
end
end
klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
end
it 'should use String#humanize as a default translation' do
klass = disposable_song_class do
assignable_values_for :genre do
%w[electronic]
end
end
klass.new.assignable_genres.collect(&:humanized).should == ['Electronic']
end
it 'should not define a method #humanized on values that are not strings' do
klass = disposable_song_class do
assignable_values_for :year do
[1999, 2000, 2001]
end
end
years = klass.new.assignable_years
years.should == [1999, 2000, 2001]
years.first.should_not respond_to(:humanized)
end
it 'should allow to directly declare humanized values by passing a hash to assignable_values_for' do
klass = disposable_song_class do
assignable_values_for :genre do
{ 'pop' => 'Pop music', 'rock' => 'Rock music' }
end
end
klass.new.assignable_genres.collect(&:humanized).should =~ ['Pop music', 'Rock music']
end
end
context 'with :through option' do
it 'should retrieve assignable values from the given method' do
klass = disposable_song_class do
assignable_values_for :genre, :through => :delegate
def delegate
@delegate ||= 'delegate'
end
end
record = klass.new
record.delegate.should_receive(:assignable_song_genres).and_return %w[pop rock]
record.assignable_genres.should == %w[pop rock]
end
it "should pass the record to the given method if the delegate's query method takes an argument" do
delegate = Object.new
def delegate.assignable_song_genres(record)
record_received(record)
%w[pop rock]
end
klass = disposable_song_class do
assignable_values_for :genre, :through => :delegate
define_method :delegate do
delegate
end
end
record = klass.new
delegate.should_receive(:record_received).with(record)
record.assignable_genres.should == %w[pop rock]
end
it 'should raise an error if the given method returns nil' do
klass = disposable_song_class do
assignable_values_for :genre, :through => :delegate
def delegate
nil
end
end
expect { klass.new.assignable_genres }.to raise_error(AssignableValues::DelegateUnavailable)
end
end
end
end
#describe '.authorize_values_for' do
#
# it 'should be a shortcut for .assignable_values_for :attribute, :through => :power' do
# @klass = disposable_song_class
# @klass.should_receive(:assignable_values_for).with(:attribute, :option => 'option', :through => :power)
# @klass.class_eval do
# authorize_values_for :attribute, :option => 'option'
# end
# end
#
# it 'should generate a getter and setter for a @power field' do
# @klass = disposable_song_class do
# authorize_values_for :attribute
# end
# song = @klass.new
# song.should respond_to(:power)
# song.should respond_to(:power=)
# end
#
#end
end