forked from mbleigh/acts-as-taggable-on
/
acts_as_taggable_on_spec.rb
514 lines (402 loc) · 18.4 KB
/
acts_as_taggable_on_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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
require File.expand_path('../../spec_helper', __FILE__)
describe "Acts As Taggable On" do
before(:each) do
clean_database!
end
it "should provide a class method 'taggable?' that is false for untaggable models" do
UntaggableModel.should_not be_taggable
end
describe "Taggable Method Generation To Preserve Order" do
before(:each) do
clean_database!
TaggableModel.tag_types = []
TaggableModel.preserve_tag_order = false
TaggableModel.acts_as_ordered_taggable_on(:ordered_tags)
@taggable = TaggableModel.new(:name => "Bob Jones")
end
it "should respond 'true' to preserve_tag_order?" do
@taggable.class.preserve_tag_order?.should be_true
end
end
describe "Taggable Method Generation" do
before(:each) do
clean_database!
TaggableModel.tag_types = []
TaggableModel.acts_as_taggable_on(:tags, :languages, :skills, :needs, :offerings)
@taggable = TaggableModel.new(:name => "Bob Jones")
end
it "should respond 'true' to taggable?" do
@taggable.class.should be_taggable
end
it "should create a class attribute for tag types" do
@taggable.class.should respond_to(:tag_types)
end
it "should create an instance attribute for tag types" do
@taggable.should respond_to(:tag_types)
end
it "should have all tag types" do
@taggable.tag_types.should == [:tags, :languages, :skills, :needs, :offerings]
end
it "should create a class attribute for preserve tag order" do
@taggable.class.should respond_to(:preserve_tag_order?)
end
it "should create an instance attribute for preserve tag order" do
@taggable.should respond_to(:preserve_tag_order?)
end
it "should respond 'false' to preserve_tag_order?" do
@taggable.class.preserve_tag_order?.should be_false
end
it "should generate an association for each tag type" do
@taggable.should respond_to(:tags, :skills, :languages)
end
it "should add tagged_with and tag_counts to singleton" do
TaggableModel.should respond_to(:tagged_with, :tag_counts)
end
it "should generate a tag_list accessor/setter for each tag type" do
@taggable.should respond_to(:tag_list, :skill_list, :language_list)
@taggable.should respond_to(:tag_list=, :skill_list=, :language_list=)
end
it "should generate a tag_list accessor, that includes owned tags, for each tag type" do
@taggable.should respond_to(:all_tags_list, :all_skills_list, :all_languages_list)
end
end
describe "Single Table Inheritance" do
before do
@taggable = TaggableModel.new(:name => "taggable")
@inherited_same = InheritingTaggableModel.new(:name => "inherited same")
@inherited_different = AlteredInheritingTaggableModel.new(:name => "inherited different")
end
it "should pass on tag contexts to STI-inherited models" do
@inherited_same.should respond_to(:tag_list, :skill_list, :language_list)
@inherited_different.should respond_to(:tag_list, :skill_list, :language_list)
end
it "should have tag contexts added in altered STI models" do
@inherited_different.should respond_to(:part_list)
end
end
describe "Reloading" do
it "should save a model instantiated by Model.find" do
taggable = TaggableModel.create!(:name => "Taggable")
found_taggable = TaggableModel.find(taggable.id)
found_taggable.save
end
end
describe "Related Objects" do
it "should find related objects based on tag names on context" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = TaggableModel.create!(:name => "Taggable 2")
taggable3 = TaggableModel.create!(:name => "Taggable 3")
taggable1.tag_list = "one, two"
taggable1.save
taggable2.tag_list = "three, four"
taggable2.save
taggable3.tag_list = "one, four"
taggable3.save
taggable1.find_related_tags.should include(taggable3)
taggable1.find_related_tags.should_not include(taggable2)
end
it "should find related objects based on tag names on context - non standard id" do
taggable1 = NonStandardIdTaggableModel.create!(:name => "Taggable 1")
taggable2 = NonStandardIdTaggableModel.create!(:name => "Taggable 2")
taggable3 = NonStandardIdTaggableModel.create!(:name => "Taggable 3")
taggable1.tag_list = "one, two"
taggable1.save
taggable2.tag_list = "three, four"
taggable2.save
taggable3.tag_list = "one, four"
taggable3.save
taggable1.find_related_tags.should include(taggable3)
taggable1.find_related_tags.should_not include(taggable2)
end
it "should find other related objects based on tag names on context" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = OtherTaggableModel.create!(:name => "Taggable 2")
taggable3 = OtherTaggableModel.create!(:name => "Taggable 3")
taggable1.tag_list = "one, two"
taggable1.save
taggable2.tag_list = "three, four"
taggable2.save
taggable3.tag_list = "one, four"
taggable3.save
taggable1.find_related_tags_for(OtherTaggableModel).should include(taggable3)
taggable1.find_related_tags_for(OtherTaggableModel).should_not include(taggable2)
end
it "should not include the object itself in the list of related objects" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = TaggableModel.create!(:name => "Taggable 2")
taggable1.tag_list = "one"
taggable1.save
taggable2.tag_list = "one, two"
taggable2.save
taggable1.find_related_tags.should include(taggable2)
taggable1.find_related_tags.should_not include(taggable1)
end
it "should not include the object itself in the list of related objects - non standard id" do
taggable1 = NonStandardIdTaggableModel.create!(:name => "Taggable 1")
taggable2 = NonStandardIdTaggableModel.create!(:name => "Taggable 2")
taggable1.tag_list = "one"
taggable1.save
taggable2.tag_list = "one, two"
taggable2.save
taggable1.find_related_tags.should include(taggable2)
taggable1.find_related_tags.should_not include(taggable1)
end
context "Inherited Models" do
before do
@taggable1 = InheritingTaggableModel.create!(:name => "InheritingTaggable 1")
@taggable2 = InheritingTaggableModel.create!(:name => "InheritingTaggable 2")
@taggable3 = InheritingTaggableModel.create!(:name => "InheritingTaggable 3")
@taggable4 = TaggableModel.create!(:name => "Taggable 4")
@taggable1.tag_list = "one, two"
@taggable1.save
@taggable2.tag_list = "three, four"
@taggable2.save
@taggable3.tag_list = "one, four"
@taggable3.save
@taggable4.tag_list = "one, two, three, four"
@taggable4.save
end
it "should find related objects based on tag names on context" do
@taggable1.find_related_tags.should include(@taggable3)
@taggable1.find_related_tags.should_not include(@taggable2)
@taggable1.find_related_tags.should_not include(@taggable4)
@taggable1.find_related_tags_for(TaggableModel).should include(@taggable3)
@taggable1.find_related_tags_for(TaggableModel).should_not include(@taggable2)
@taggable1.find_related_tags_for(TaggableModel).should include(@taggable4)
end
it "should not include the object itself in the list of related objects" do
@taggable1.find_related_tags.should_not include(@taggable1)
@taggable1.find_related_tags_for(InheritingTaggableModel).should_not include(@taggable1)
@taggable1.find_related_tags_for(TaggableModel).should_not include(@taggable1)
end
end
end
describe "Matching Contexts" do
it "should find objects with tags of matching contexts" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = TaggableModel.create!(:name => "Taggable 2")
taggable3 = TaggableModel.create!(:name => "Taggable 3")
taggable1.offering_list = "one, two"
taggable1.save!
taggable2.need_list = "one, two"
taggable2.save!
taggable3.offering_list = "one, two"
taggable3.save!
taggable1.find_matching_contexts(:offerings, :needs).should include(taggable2)
taggable1.find_matching_contexts(:offerings, :needs).should_not include(taggable3)
end
it "should find other related objects with tags of matching contexts" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = OtherTaggableModel.create!(:name => "Taggable 2")
taggable3 = OtherTaggableModel.create!(:name => "Taggable 3")
taggable1.offering_list = "one, two"
taggable1.save
taggable2.need_list = "one, two"
taggable2.save
taggable3.offering_list = "one, two"
taggable3.save
taggable1.find_matching_contexts_for(OtherTaggableModel, :offerings, :needs).should include(taggable2)
taggable1.find_matching_contexts_for(OtherTaggableModel, :offerings, :needs).should_not include(taggable3)
end
it "should not include the object itself in the list of related objects with tags of matching contexts" do
taggable1 = TaggableModel.create!(:name => "Taggable 1")
taggable2 = TaggableModel.create!(:name => "Taggable 2")
taggable1.offering_list = "one, two"
taggable1.need_list = "one, two"
taggable1.save
taggable2.need_list = "one, two"
taggable2.save
taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should include(taggable2)
taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should_not include(taggable1)
end
context "Inherited Models" do
before do
@taggable1 = InheritingTaggableModel.create!(:name => "InheritingTaggable 1")
@taggable2 = InheritingTaggableModel.create!(:name => "InheritingTaggable 2")
@taggable3 = InheritingTaggableModel.create!(:name => "InheritingTaggable 3")
@taggable4 = InheritingTaggableModel.create!(:name => "InheritingTaggable 4")
@taggable5 = TaggableModel.create!(:name => "Taggable 5")
@taggable1.offering_list = "one, two"
@taggable1.need_list = "one, two"
@taggable1.save!
@taggable2.need_list = "one, two"
@taggable2.save!
@taggable3.offering_list = "one, two"
@taggable3.save!
@taggable4.tag_list = "one, two, three, four"
@taggable4.save!
@taggable5.need_list = "one, two"
@taggable5.save!
end
it "should find objects with tags of matching contexts" do
@taggable1.find_matching_contexts(:offerings, :needs).should include(@taggable2)
@taggable1.find_matching_contexts(:offerings, :needs).should_not include(@taggable3)
@taggable1.find_matching_contexts(:offerings, :needs).should_not include(@taggable4)
@taggable1.find_matching_contexts(:offerings, :needs).should_not include(@taggable5)
@taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should include(@taggable2)
@taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should_not include(@taggable3)
@taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should_not include(@taggable4)
@taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should include(@taggable5)
end
it "should not include the object itself in the list of related objects with tags of matching contexts" do
@taggable1.find_matching_contexts(:offerings, :needs).should_not include(@taggable1)
@taggable1.find_matching_contexts_for(InheritingTaggableModel, :offerings, :needs).should_not include(@taggable1)
@taggable1.find_matching_contexts_for(TaggableModel, :offerings, :needs).should_not include(@taggable1)
end
end
end
describe 'Tagging Contexts' do
it 'should eliminate duplicate tagging contexts ' do
TaggableModel.acts_as_taggable_on(:skills, :skills)
TaggableModel.tag_types.freq[:skills].should_not == 3
end
it "should not contain embedded/nested arrays" do
TaggableModel.acts_as_taggable_on([:array], [:array])
TaggableModel.tag_types.freq[[:array]].should == 0
end
it "should _flatten_ the content of arrays" do
TaggableModel.acts_as_taggable_on([:array], [:array])
TaggableModel.tag_types.freq[:array].should == 1
end
it "should not raise an error when passed nil" do
lambda {
TaggableModel.acts_as_taggable_on()
}.should_not raise_error
end
it "should not raise an error when passed [nil]" do
lambda {
TaggableModel.acts_as_taggable_on([nil])
}.should_not raise_error
end
end
describe 'Caching' do
before(:each) do
@taggable = CachedModel.new(:name => "Bob Jones")
@another_taggable = OtherCachedModel.new(:name => "John Smith")
end
it "should add saving of tag lists and cached tag lists to the instance" do
@taggable.should respond_to(:save_cached_tag_list)
@another_taggable.should respond_to(:save_cached_tag_list)
@taggable.should respond_to(:save_tags)
end
it "should add cached tag lists to the instance if cached column is not present" do
TaggableModel.new(:name => "Art Kram").should_not respond_to(:save_cached_tag_list)
end
it "should generate a cached column checker for each tag type" do
CachedModel.should respond_to(:caching_tag_list?)
OtherCachedModel.should respond_to(:caching_language_list?)
end
it 'should not have cached tags' do
@taggable.cached_tag_list.should be_blank
@another_taggable.cached_language_list.should be_blank
end
it 'should cache tags' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable.cached_tag_list.should == 'awesome, epic'
@another_taggable.update_attributes(:language_list => 'ruby, .net')
@another_taggable.cached_language_list.should == 'ruby, .net'
end
it 'should keep the cache' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable = CachedModel.find(@taggable)
@taggable.save!
@taggable.cached_tag_list.should == 'awesome, epic'
end
it 'should update the cache' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable.update_attributes(:tag_list => 'awesome')
@taggable.cached_tag_list.should == 'awesome'
end
it 'should remove the cache' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable.update_attributes(:tag_list => '')
@taggable.cached_tag_list.should be_blank
end
it 'should have a tag list' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable = CachedModel.find(@taggable.id)
@taggable.tag_list.sort.should == %w(awesome epic).sort
end
it 'should keep the tag list' do
@taggable.update_attributes(:tag_list => 'awesome, epic')
@taggable = CachedModel.find(@taggable.id)
@taggable.save!
@taggable.tag_list.sort.should == %w(awesome epic).sort
end
end
context 'when tagging context ends in an "s" when singular (ex. "status", "glass", etc.)' do
describe 'caching' do
before { @taggable = OtherCachedModel.new(:name => "John Smith") }
subject { @taggable }
it { should respond_to(:save_cached_tag_list) }
its(:cached_language_list) { should be_blank }
its(:cached_status_list) { should be_blank }
its(:cached_glass_list) { should be_blank }
context 'language taggings cache after update' do
before { @taggable.update_attributes(:language_list => 'ruby, .net') }
subject { @taggable }
its(:language_list) { should == ['ruby', '.net']}
its(:cached_language_list) { should == 'ruby, .net' } # passes
its(:instance_variables) { should include((RUBY_VERSION < '1.9' ? '@language_list' : :@language_list)) }
end
context 'status taggings cache after update' do
before { @taggable.update_attributes(:status_list => 'happy, married') }
subject { @taggable }
its(:status_list) { should == ['happy', 'married'] }
its(:cached_status_list) { should == 'happy, married' } # fails
its(:cached_status_list) { should_not == '' } # fails, is blank
its(:instance_variables) { should include((RUBY_VERSION < '1.9' ? '@status_list' : :@status_list)) }
its(:instance_variables) { should_not include((RUBY_VERSION < '1.9' ? '@statu_list' : :@statu_list)) } # fails, note: one "s"
end
context 'glass taggings cache after update' do
before do
@taggable.update_attributes(:glass_list => 'rectangle, aviator')
end
subject { @taggable }
its(:glass_list) { should == ['rectangle', 'aviator'] }
its(:cached_glass_list) { should == 'rectangle, aviator' } # fails
its(:cached_glass_list) { should_not == '' } # fails, is blank
if RUBY_VERSION < '1.9'
its(:instance_variables) { should include('@glass_list') }
its(:instance_variables) { should_not include('@glas_list') } # fails, note: one "s"
else
its(:instance_variables) { should include(:@glass_list) }
its(:instance_variables) { should_not include(:@glas_list) } # fails, note: one "s"
end
end
end
end
describe "taggings" do
before(:each) do
@taggable = TaggableModel.new(:name => "Art Kram")
end
it 'should return [] taggings' do
@taggable.taggings.should == []
end
end
describe "@@remove_unused_tags" do
before do
@taggable = TaggableModel.create(:name => "Bob Jones")
@tag = ActsAsTaggableOn::Tag.create(:name => "awesome")
@tagging = ActsAsTaggableOn::Tagging.create(:taggable => @taggable, :tag => @tag, :context => 'tags')
end
context "if set to true" do
before do
ActsAsTaggableOn.remove_unused_tags = true
end
it "should remove unused tags after removing taggings" do
@tagging.destroy
ActsAsTaggableOn::Tag.find_by_name("awesome").should be_nil
end
end
context "if set to false" do
before do
ActsAsTaggableOn.remove_unused_tags = false
end
it "should not remove unused tags after removing taggings" do
@tagging.destroy
ActsAsTaggableOn::Tag.find_by_name("awesome").should == @tag
end
end
end
end