acts_as_taggable 1.0.4 → 2.0.0

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.
Files changed (5) hide show
  1. data/CHANGELOG +10 -0
  2. data/README +89 -70
  3. data/lib/taggable.rb +610 -467
  4. data/test/acts_as_taggable_test.rb +412 -384
  5. metadata +26 -39
@@ -1,384 +1,412 @@
1
- AR_PATH = 'c:/dev/rails/activerecord' || ARGS[0]
2
- $:.unshift("#{AR_PATH}/test")
3
- $:.unshift("#{AR_PATH}/test/connections/native_mysql")
4
- $:.unshift(File.dirname(__FILE__) + '/../lib')
5
-
6
- require 'abstract_unit'
7
- require 'taggable'
8
-
9
- ActiveRecord::Base.connection.drop_table :tags rescue nil
10
- ActiveRecord::Base.connection.drop_table :tags_topics rescue nil
11
- ActiveRecord::Base.connection.drop_table :keywords rescue nil
12
- ActiveRecord::Base.connection.drop_table :keywords_companies rescue nil
13
- ActiveRecord::Base.connection.drop_table :tags_posts rescue nil
14
-
15
- ActiveRecord::Base.connection.create_table :tags do |t|
16
- t.column :name, :string
17
- end
18
-
19
- ActiveRecord::Base.connection.create_table :tags_topics, :id => false do |t|
20
- t.column :tag_id, :int
21
- t.column :topic_id, :int
22
- t.column :created_at, :time
23
- end
24
-
25
- ActiveRecord::Base.connection.create_table :keywords do |t|
26
- t.column :name, :string
27
- end
28
-
29
- ActiveRecord::Base.connection.create_table :keywords_companies, :id => false do |t|
30
- t.column :keyword_id, :int
31
- t.column :company_id, :int
32
- end
33
-
34
- ActiveRecord::Base.connection.create_table :tags_posts do |t|
35
- t.column :tag_id, :int
36
- t.column :post_id, :int
37
- t.column :created_at, :time
38
- t.column :created_by_id, :int
39
- t.column :position, :int
40
- end
41
-
42
- class Tag < ActiveRecord::Base; end
43
- class Topic < ActiveRecord::Base
44
- acts_as_taggable
45
- end
46
-
47
- class Keyword < ActiveRecord::Base; end
48
-
49
- class Company < ActiveRecord::Base
50
- acts_as_taggable :collection => :keywords, :tag_class_name => 'Keyword'
51
- end
52
-
53
- class Firm < Company; end
54
- class Client < Company; end
55
-
56
- class Post < ActiveRecord::Base
57
- acts_as_taggable :join_class_name => 'TagPost'
58
- end
59
-
60
- class TagPost
61
- acts_as_list :scope => :post
62
-
63
- def before_save
64
- self.created_by_id = rand(3) + 1
65
- end
66
- end
67
-
68
- class Order < ActiveRecord::Base
69
- end
70
-
71
- class ActAsTaggableTest < Test::Unit::TestCase
72
- fixtures :topics
73
-
74
- def test_singleton_methods
75
- assert !Order.respond_to?(:find_tagged_with)
76
- assert Firm.respond_to?(:find_tagged_with)
77
- assert Post.respond_to?(:find_tagged_with)
78
- assert Topic.respond_to?(:find_tagged_with)
79
- assert Topic.respond_to?(:tag_count)
80
- assert Topic.respond_to?(:tags_count)
81
- end
82
-
83
- def test_with_defaults
84
- test_tagging(Topic.find(:first), Tag, :tags)
85
- end
86
-
87
- def test_with_non_defaults
88
- test_tagging(Company.find(:first), Keyword, :keywords)
89
- end
90
-
91
- def test_tag_with_new_object
92
- topic = Topic.new
93
- topic.tag 'brazil rio beach'
94
- topic.save
95
- end
96
-
97
- def test_tagging_with_join_model
98
- Tag.delete_all
99
- TagPost.delete_all
100
- post = Post.find(:first)
101
- tags = %w(brazil rio beach)
102
-
103
- post.tag(tags)
104
- tags.each { |tag| assert post.tagged_with?(tag) }
105
-
106
- post.save
107
- post.tags.reload
108
- tags.each { |tag| assert post.tagged_with?(tag) }
109
-
110
- posts = Post.find_tagged_with(:any => 'brazil sampa moutain')
111
- assert_equal posts[0], post
112
-
113
- posts = Post.find_tagged_with(:all => 'brazil beach')
114
- assert_equal posts[0], post
115
-
116
- posts = Post.find_tagged_with(:all => 'brazil rich')
117
- assert_equal 0, posts.size
118
-
119
- posts = Post.find_tagged_with(:all => 'brazil', :conditions => [ 'tags_posts.position = ?', 1])
120
- assert_equal posts[0], post
121
-
122
- posts = Post.find_tagged_with(:all => 'rio', :conditions => [ 'tags_posts.position = ?', 2])
123
- assert_equal posts[0], post
124
-
125
- posts = Post.find_tagged_with(:all => 'beach', :conditions => [ 'tags_posts.position = ?', 3])
126
- assert_equal posts[0], post
127
- end
128
-
129
- def test_tags_count_with_join_model
130
- p1 = Post.create(:title => 'test1')
131
- p2 = Post.create(:title => 'test2')
132
- p3 = Post.create(:title => 'test3')
133
-
134
- p1.tag 'a b c d'
135
- p2.tag 'a c e f'
136
- p3.tag 'a c f g'
137
-
138
- counts = Post.tags_count :count => '>= 2', :limit => 2
139
- assert_equal counts.keys.size, 2
140
- counts.each { |tag, count| assert count >= 2 }
141
- assert counts.keys.include?('a')
142
- assert counts.keys.include?('c')
143
- end
144
-
145
- def test_tags_count
146
- t1 = Topic.create(:title => 'test1')
147
- t2 = Topic.create(:title => 'test2')
148
- t3 = Topic.create(:title => 'test3')
149
-
150
- t1.tag 'a b c d'
151
- t2.tag 'a c e f'
152
- t3.tag 'a c f g'
153
-
154
- count = Topic.tags_count
155
- assert_equal 3, count['a']
156
- assert_equal 1, count['b']
157
- assert_equal 3, count['c']
158
- assert_equal 1, count['d']
159
- assert_equal 1, count['e']
160
- assert_equal 2, count['f']
161
- assert_equal 1, count['g']
162
- assert_equal nil, count['h']
163
-
164
- count = Topic.tags_count :count => '>= 2'
165
- assert_equal 3, count['a']
166
- assert_equal nil, count['b']
167
- assert_equal 3, count['c']
168
- assert_equal nil, count['d']
169
- assert_equal nil, count['e']
170
- assert_equal 2, count['f']
171
- assert_equal nil, count['g']
172
- assert_equal nil, count['h']
173
-
174
- t4 = Topic.create(:title => 'test4')
175
- t4.tag 'a f'
176
-
177
- count = Topic.tags_count :limit => 3
178
- assert_equal 4, count['a']
179
- assert_equal nil, count['b']
180
- assert_equal 3, count['c']
181
- assert_equal nil, count['d']
182
- assert_equal nil, count['e']
183
- assert_equal 3, count['f']
184
- assert_equal nil, count['g']
185
- assert_equal nil, count['h']
186
-
187
- raw = Topic.tags_count :raw => true
188
- assert_equal 7, raw.size
189
- assert_equal Array, raw.class
190
- assert_equal 'a', raw.first['name']
191
- assert_equal '4', raw.first['count']
192
- assert_not_nil raw.first['id']
193
- assert_equal 'g', raw.last['name']
194
- assert_equal '1', raw.last['count']
195
- assert_not_nil raw.last['id']
196
- end
197
-
198
- def test_find_related_tagged
199
- t1, t2, t3, t4, t5, t6 = create_test_topics
200
-
201
- assert_equal [ t4, t2, t3 ], t1.tagged_related(:limit => 3)
202
- assert_equal [ t5, t1, t3 ], t2.tagged_related(:limit => 3)
203
- assert_equal [ t1, t4, t6 ], t3.tagged_related(:limit => 3)
204
- assert_equal [ t1, t3, t6 ], t4.tagged_related(:limit => 3)
205
- assert_equal [ t2, t1, t3 ], t5.tagged_related(:limit => 3)
206
- assert_equal [ t1, t3, t4 ], t6.tagged_related(:limit => 3)
207
- end
208
-
209
- def test_find_related_tags
210
- t1, t2, t3, t4, t5, t6 = create_test_topics
211
-
212
- tags = Topic.find_related_tags('rome walking')
213
- assert_equal 2, tags['greatview']
214
- assert_equal 4, tags['clean']
215
- assert_equal 2, tags['mustsee']
216
- end
217
-
218
- def test_find_tagged_with_on_subclasses
219
- firm = Firm.find(:first)
220
- firm.tag 'law'
221
- firms = Firm.find_tagged_with :any => 'law'
222
- assert_equal firm, firms[0]
223
- assert_equal 1, firms.size
224
- end
225
-
226
- def test_find_tagged_with_any
227
- topic1 = Topic.create(:title => 'test1')
228
- topic2 = Topic.create(:title => 'test2')
229
- topic3 = Topic.create(:title => 'test3')
230
-
231
- topic1.tag('a b c'); topic1.save
232
- topic2.tag('a c e'); topic2.save
233
- topic3.tag('c d e'); topic3.save
234
-
235
- topics = Topic.find_tagged_with(:any => 'x y z')
236
- assert_equal 0, topics.size
237
-
238
- topics = Topic.find_tagged_with(:any => 'a b c d e x y z')
239
- assert_equal 3, topics.size
240
- assert topics.include?(topic1)
241
- assert topics.include?(topic2)
242
- assert topics.include?(topic3)
243
-
244
- topics = Topic.find_tagged_with(:any => 'a z')
245
- assert_equal 2, topics.size
246
- assert topics.include?(topic1)
247
- assert topics.include?(topic2)
248
-
249
- topics = Topic.find_tagged_with(:any => 'b')
250
- assert_equal 1, topics.size
251
- assert topics.include?(topic1)
252
-
253
- topics = Topic.find_tagged_with(:any => 'c')
254
- assert_equal 3, topics.size
255
- assert topics.include?(topic1)
256
- assert topics.include?(topic2)
257
- assert topics.include?(topic3)
258
-
259
- topics = Topic.find_tagged_with(:any => 'd')
260
- assert_equal 1, topics.size
261
- assert topics.include?(topic3)
262
-
263
- topics = Topic.find_tagged_with(:any => 'e')
264
- assert_equal 2, topics.size
265
- assert topics.include?(topic2)
266
- assert topics.include?(topic3)
267
- end
268
-
269
- def test_find_tagged_with_all
270
- topic1 = Topic.create(:title => 'test1')
271
- topic2 = Topic.create(:title => 'test2')
272
- topic3 = Topic.create(:title => 'test3')
273
-
274
- topic1.tag('a b c'); topic1.save
275
- topic2.tag('a c e'); topic2.save
276
- topic3.tag('c d e'); topic3.save
277
-
278
- topics = Topic.find_tagged_with(:all => 'a b d')
279
- assert_equal 0, topics.size
280
-
281
- topics = Topic.find_tagged_with(:all => 'a c')
282
- assert_equal 2, topics.size
283
- assert topics.include?(topic1)
284
- assert topics.include?(topic2)
285
-
286
- topics = Topic.find_tagged_with(:all => 'a+c', :separator => '+')
287
- assert_equal 2, topics.size
288
- assert topics.include?(topic1)
289
- assert topics.include?(topic2)
290
-
291
- topics = Topic.find_tagged_with(:all => 'c e')
292
- assert_equal 2, topics.size
293
- assert topics.include?(topic2)
294
- assert topics.include?(topic3)
295
-
296
- topics = Topic.find_tagged_with(:all => 'c')
297
- assert_equal 3, topics.size
298
- assert topics.include?(topic1)
299
- assert topics.include?(topic2)
300
- assert topics.include?(topic3)
301
-
302
- topics = Topic.find_tagged_with(:all => 'a b c')
303
- assert_equal 1, topics.size
304
- assert topics.include?(topic1)
305
-
306
- topics = Topic.find_tagged_with(:all => 'a c e')
307
- assert_equal 1, topics.size
308
- assert topics.include?(topic2)
309
- end
310
-
311
- private
312
- def test_tagging(tagged_object, tag_model, collection)
313
- tag_model.delete_all
314
- assert_equal 0, tag_model.count
315
-
316
- tagged_object.tag_names << 'rio brazil'
317
- tagged_object.save
318
-
319
- assert_equal 2, tag_model.count
320
- assert_equal 2, tagged_object.send(collection).size
321
-
322
- tagged_object.tag_names = 'beach surf'
323
- assert_equal 4, tag_model.count
324
- assert_equal 2, tagged_object.send(collection).size
325
-
326
- tagged_object.tag_names.concat 'soccer+pele', :separator => '+'
327
- assert_equal 6, tag_model.count
328
- assert_equal 4, tagged_object.send(collection).size
329
-
330
- tag_model.delete_all
331
- assert_equal 0, tag_model.count
332
- tagged_object.send(collection).reload
333
-
334
- tagged_object.tag_names = 'dhh'
335
- assert_equal 1, tag_model.count
336
- assert_equal 1, tagged_object.send(collection).size
337
-
338
- tagged_object.tag 'dhh rails my', :clear => true
339
-
340
- assert_equal 3, tag_model.count
341
- assert_equal 3, tagged_object.send(collection).size
342
-
343
- tagged_object.tag 'dhh dhh ruby tags', :clear => true
344
- assert_equal 5, tag_model.count
345
- assert_equal 3, tagged_object.send(collection).size
346
-
347
- tagged_object.tag 'tagging, hello, ruby', :separator => ','
348
- assert_equal 7, tag_model.count
349
- assert_equal 5, tagged_object.send(collection).size
350
-
351
- all_tags = %w( dhh rails my ruby tags tagging hello )
352
- first_tags = %w( dhh ruby tags tagging hello )
353
-
354
- tagged_object.send(collection).reload
355
- assert_equal first_tags, tagged_object.tag_names
356
- all_tags.each do |tag_name|
357
- tag_record = tag_model.find_by_name(tag_name)
358
- assert_not_nil tag_record
359
-
360
- if first_tags.include?(tag_name)
361
- assert tagged_object.send(collection).include?(tag_record)
362
- assert tagged_object.tagged_with?(tag_name)
363
- end
364
- end
365
- end
366
-
367
- def create_test_topics
368
- t1 = Topic.create(:title => 't1')
369
- t2 = Topic.create(:title => 't2')
370
- t3 = Topic.create(:title => 't3')
371
- t4 = Topic.create(:title => 't4')
372
- t5 = Topic.create(:title => 't5')
373
- t6 = Topic.create(:title => 't6')
374
-
375
- t1.tag('rome, luxury, clean, mustsee, greatview', :separator => ','); t1.save
376
- t2.tag('rome, luxury, clean, italian, spicy, goodwine', :separator => ','); t2.save
377
- t3.tag('rome, walking, clean, mustsee', :separator => ','); t3.save
378
- t4.tag('rome, italy, clean, mustsee, greatview', :separator => ','); t4.save
379
- t5.tag('rome, luxury, clean, italian, spicy, wine', :separator => ','); t5.save
380
- t6.tag('rome, walking, clean, greatview', :separator => ','); t6.save
381
-
382
- [ t1, t2, t3, t4, t5, t6 ]
383
- end
384
- end
1
+ AR_PATH = 'D:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2' || 'c:/dev/rails/activerecord' || ARGS[0]
2
+ $:.unshift("#{AR_PATH}/test")
3
+ $:.unshift("#{AR_PATH}/test/connections/native_mysql")
4
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'abstract_unit'
7
+ require 'taggable'
8
+
9
+ @@tag_table_column_name = :name
10
+
11
+ ActiveRecord::Base.connection.drop_table :tags rescue nil
12
+ ActiveRecord::Base.connection.drop_table :tags_topics rescue nil
13
+ ActiveRecord::Base.connection.drop_table :keywords rescue nil
14
+ ActiveRecord::Base.connection.drop_table :keywords_companies rescue nil
15
+ ActiveRecord::Base.connection.drop_table :tags_posts rescue nil
16
+
17
+ ActiveRecord::Base.connection.create_table :tags do |t|
18
+ t.column @@tag_table_column_name , :string
19
+ end
20
+
21
+ ActiveRecord::Base.connection.create_table :tags_topics, :id => false do |t|
22
+ t.column :tag_id, :int
23
+ t.column :topic_id, :int
24
+ t.column :created_at, :time
25
+ end
26
+
27
+ ActiveRecord::Base.connection.create_table :keywords do |t|
28
+ t.column :name, :string
29
+ end
30
+
31
+ ActiveRecord::Base.connection.create_table :keywords_companies, :id => false do |t|
32
+ t.column :keyword_id, :int
33
+ t.column :company_id, :int
34
+ end
35
+
36
+ ActiveRecord::Base.connection.create_table :tags_posts do |t|
37
+ t.column :tag_id, :int
38
+ t.column :post_id, :int
39
+ t.column :created_at, :time
40
+ t.column :created_by_id, :int
41
+ t.column :position, :int
42
+ end
43
+
44
+ class Tag < ActiveRecord::Base; end
45
+ class Topic < ActiveRecord::Base
46
+ acts_as_taggable
47
+ end
48
+
49
+ class Keyword < ActiveRecord::Base; end
50
+
51
+ class Company < ActiveRecord::Base
52
+ acts_as_taggable :collection => :keywords, :tag_class_name => 'Keyword'
53
+ end
54
+
55
+ class Firm < Company; end
56
+ class Client < Company; end
57
+
58
+ class Post < ActiveRecord::Base
59
+ acts_as_taggable :join_class_name => 'TagPost'
60
+ end
61
+
62
+ class TagPost
63
+ acts_as_list :scope => :post
64
+
65
+ def before_save
66
+ self.created_by_id = rand(3) + 1
67
+ end
68
+ end
69
+
70
+ class Order < ActiveRecord::Base
71
+ end
72
+
73
+ class ActAsTaggableTest < Test::Unit::TestCase
74
+ fixtures :topics
75
+
76
+ def test_singleton_methods
77
+ assert !Order.respond_to?(:find_tagged_with)
78
+ assert Firm.respond_to?(:find_tagged_with)
79
+ assert Firm.respond_to?(:cloud)
80
+ assert Post.respond_to?(:find_tagged_with)
81
+ assert Post.respond_to?(:cloud)
82
+ assert Topic.respond_to?(:find_tagged_with)
83
+ assert Topic.respond_to?(:tag_count)
84
+ assert Topic.respond_to?(:tags_count)
85
+ assert Topic.respond_to?(:cloud)
86
+ end
87
+
88
+ def test_with_defaults
89
+ test_tagging(Topic.find(:first), Tag, :tags)
90
+ end
91
+
92
+ def test_with_non_defaults
93
+ test_tagging(Company.find(:first), Keyword, :keywords)
94
+ end
95
+
96
+ def test_tag_with_new_object
97
+ topic = Topic.new
98
+ topic.tag 'brazil rio beach'
99
+ topic.save
100
+ end
101
+
102
+ def test_tagging_with_join_model
103
+ Tag.delete_all
104
+ TagPost.delete_all
105
+ post = Post.find(:first)
106
+ tags = %w(brazil rio beach)
107
+
108
+ post.tag(tags)
109
+ tags.each { |tag| assert post.tagged_with?(tag) }
110
+
111
+ post.save
112
+ post.tags.reload
113
+ tags.each { |tag| assert post.tagged_with?(tag) }
114
+
115
+ posts = Post.find_tagged_with(:any => 'brazil sampa moutain')
116
+ assert_equal posts[0], post
117
+
118
+ posts = Post.find_tagged_with(:all => 'brazil beach')
119
+ assert_equal posts[0], post
120
+
121
+ posts = Post.find_tagged_with(:all => 'brazil rich')
122
+ assert_equal 0, posts.size
123
+
124
+ posts = Post.find_tagged_with(:all => 'brazil', :conditions => [ 'tags_posts.position = ?', 1])
125
+ assert_equal posts[0], post
126
+
127
+ posts = Post.find_tagged_with(:all => 'rio', :conditions => [ 'tags_posts.position = ?', 2])
128
+ assert_equal posts[0], post
129
+
130
+ posts = Post.find_tagged_with(:all => 'beach', :conditions => [ 'tags_posts.position = ?', 3])
131
+ assert_equal posts[0], post
132
+ end
133
+
134
+ def test_tags_count_with_join_model
135
+ p1 = Post.create(:title => 'test1')
136
+ p2 = Post.create(:title => 'test2')
137
+ p3 = Post.create(:title => 'test3')
138
+
139
+ p1.tag 'a b c d'
140
+ p2.tag 'a c e f'
141
+ p3.tag 'a c f g'
142
+
143
+ counts = Post.tags_count :count => '>= 2', :limit => 2
144
+ assert_equal counts.keys.size, 2
145
+ counts.each { |tag, count| assert count >= 2 }
146
+ assert counts.keys.include?('a')
147
+ assert counts.keys.include?('c')
148
+ end
149
+
150
+ def test_tags_count
151
+ t1 = Topic.create(:title => 'test1')
152
+ t2 = Topic.create(:title => 'test2')
153
+ t3 = Topic.create(:title => 'test3')
154
+
155
+ t1.tag 'a b c d'
156
+ t2.tag 'a c e f'
157
+ t3.tag 'a c f g'
158
+
159
+ count = Topic.tags_count
160
+ assert_equal 3, count['a']
161
+ assert_equal 1, count['b']
162
+ assert_equal 3, count['c']
163
+ assert_equal 1, count['d']
164
+ assert_equal 1, count['e']
165
+ assert_equal 2, count['f']
166
+ assert_equal 1, count['g']
167
+ assert_equal nil, count['h']
168
+
169
+ count = Topic.tags_count :count => '>= 2'
170
+ assert_equal 3, count['a']
171
+ assert_equal nil, count['b']
172
+ assert_equal 3, count['c']
173
+ assert_equal nil, count['d']
174
+ assert_equal nil, count['e']
175
+ assert_equal 2, count['f']
176
+ assert_equal nil, count['g']
177
+ assert_equal nil, count['h']
178
+
179
+ t4 = Topic.create(:title => 'test4')
180
+ t4.tag 'a f'
181
+
182
+ count = Topic.tags_count :limit => 3
183
+ assert_equal 4, count['a']
184
+ assert_equal nil, count['b']
185
+ assert_equal 3, count['c']
186
+ assert_equal nil, count['d']
187
+ assert_equal nil, count['e']
188
+ assert_equal 3, count['f']
189
+ assert_equal nil, count['g']
190
+ assert_equal nil, count['h']
191
+
192
+ raw = Topic.tags_count :raw => true
193
+ assert_equal 7, raw.size
194
+ assert_equal Array, raw.class
195
+ assert_equal 'a', raw.first[@@tag_table_column_name.to_s]
196
+ assert_equal '4', raw.first['count']
197
+ assert_not_nil raw.first['id']
198
+ assert_equal 'g', raw.last[@@tag_table_column_name.to_s]
199
+ assert_equal '1', raw.last['count']
200
+ assert_not_nil raw.last['id']
201
+ end
202
+
203
+ def test_find_related_tagged
204
+ t1, t2, t3, t4, t5, t6 = create_test_topics
205
+
206
+ assert_equal [ t4, t2, t3 ], t1.tagged_related(:limit => 3)
207
+ assert_equal [ t5, t1, t3 ], t2.tagged_related(:limit => 3)
208
+ assert_equal [ t1, t4, t6 ], t3.tagged_related(:limit => 3)
209
+ assert_equal [ t1, t3, t6 ], t4.tagged_related(:limit => 3)
210
+ assert_equal [ t2, t1, t3 ], t5.tagged_related(:limit => 3)
211
+ assert_equal [ t1, t3, t4 ], t6.tagged_related(:limit => 3)
212
+ end
213
+
214
+ def test_find_related_tags
215
+ t1, t2, t3, t4, t5, t6 = create_test_topics
216
+
217
+ tags = Topic.find_related_tags('rome walking')
218
+ assert_equal 2, tags['greatview']
219
+ assert_equal 4, tags['clean']
220
+ assert_equal 2, tags['mustsee']
221
+ end
222
+
223
+ def test_find_tagged_with_on_subclasses
224
+ firm = Firm.find(:first)
225
+ firm.tag 'law'
226
+ firms = Firm.find_tagged_with :any => 'law'
227
+ assert_equal firm, firms[0]
228
+ assert_equal 1, firms.size
229
+ end
230
+
231
+ def test_find_tagged_with_any
232
+ topic1 = Topic.create(:title => 'test1')
233
+ topic2 = Topic.create(:title => 'test2')
234
+ topic3 = Topic.create(:title => 'test3')
235
+
236
+ topic1.tag('a b c'); topic1.save
237
+ topic2.tag('a c e'); topic2.save
238
+ topic3.tag('c d e'); topic3.save
239
+
240
+ topics = Topic.find_tagged_with(:any => 'x y z')
241
+ assert_equal 0, topics.size
242
+
243
+ topics = Topic.find_tagged_with(:any => 'a b c d e x y z')
244
+ assert_equal 3, topics.size
245
+ assert topics.include?(topic1)
246
+ assert topics.include?(topic2)
247
+ assert topics.include?(topic3)
248
+
249
+ topics = Topic.find_tagged_with(:any => 'a z')
250
+ assert_equal 2, topics.size
251
+ assert topics.include?(topic1)
252
+ assert topics.include?(topic2)
253
+
254
+ topics = Topic.find_tagged_with(:any => 'b')
255
+ assert_equal 1, topics.size
256
+ assert topics.include?(topic1)
257
+
258
+ topics = Topic.find_tagged_with(:any => 'c')
259
+ assert_equal 3, topics.size
260
+ assert topics.include?(topic1)
261
+ assert topics.include?(topic2)
262
+ assert topics.include?(topic3)
263
+
264
+ topics = Topic.find_tagged_with(:any => 'd')
265
+ assert_equal 1, topics.size
266
+ assert topics.include?(topic3)
267
+
268
+ topics = Topic.find_tagged_with(:any => 'e')
269
+ assert_equal 2, topics.size
270
+ assert topics.include?(topic2)
271
+ assert topics.include?(topic3)
272
+ end
273
+
274
+ def test_find_tagged_with_all
275
+ topic1 = Topic.create(:title => 'test1')
276
+ topic2 = Topic.create(:title => 'test2')
277
+ topic3 = Topic.create(:title => 'test3')
278
+
279
+ topic1.tag('a b c'); topic1.save
280
+ topic2.tag('a c e'); topic2.save
281
+ topic3.tag('c d e'); topic3.save
282
+
283
+ topics = Topic.find_tagged_with(:all => 'a b d')
284
+ assert_equal 0, topics.size
285
+
286
+ topics = Topic.find_tagged_with(:all => 'a c')
287
+ assert_equal 2, topics.size
288
+ assert topics.include?(topic1)
289
+ assert topics.include?(topic2)
290
+
291
+ topics = Topic.find_tagged_with(:all => 'a+c', :separator => '+')
292
+ assert_equal 2, topics.size
293
+ assert topics.include?(topic1)
294
+ assert topics.include?(topic2)
295
+
296
+ topics = Topic.find_tagged_with(:all => 'c e')
297
+ assert_equal 2, topics.size
298
+ assert topics.include?(topic2)
299
+ assert topics.include?(topic3)
300
+
301
+ topics = Topic.find_tagged_with(:all => 'c')
302
+ assert_equal 3, topics.size
303
+ assert topics.include?(topic1)
304
+ assert topics.include?(topic2)
305
+ assert topics.include?(topic3)
306
+
307
+ topics = Topic.find_tagged_with(:all => 'a b c')
308
+ assert_equal 1, topics.size
309
+ assert topics.include?(topic1)
310
+
311
+ topics = Topic.find_tagged_with(:all => 'a c e')
312
+ assert_equal 1, topics.size
313
+ assert topics.include?(topic2)
314
+ end
315
+
316
+ def test_tag_cloud
317
+ t1, t2, t3, t4, t5, t6 = create_test_topics
318
+
319
+ tags = Topic.tags_count
320
+ assert_equal 9, tags.size
321
+
322
+ Topic.cloud(tags, ['1', '2', '3', '4', '5', '6']) do |tag, cat|
323
+ case tag
324
+ when 'rome': assert_equal cat, '6'
325
+ when 'luxury': assert_equal cat, '3'
326
+ when 'clean': assert_equal cat, '6'
327
+ when 'mustsee': assert_equal cat, '3'
328
+ when 'greatview': assert_equal cat, '3'
329
+ when 'italian': assert_equal cat, '2'
330
+ when 'spicy': assert_equal cat, '2'
331
+ when 'goodwine': assert_equal cat, '1'
332
+ when 'wine': assert_equal cat, '1'
333
+ else
334
+ flunk 'Unexpected Tag/Category pair'
335
+ end
336
+ end
337
+ end
338
+
339
+ private
340
+ def test_tagging(tagged_object, tag_model, collection)
341
+ tag_model.delete_all
342
+ assert_equal 0, tag_model.count
343
+
344
+ tagged_object.tag_names << 'rio brazil'
345
+ tagged_object.save
346
+
347
+ assert_equal 2, tag_model.count
348
+ assert_equal 2, tagged_object.send(collection).size
349
+
350
+ tagged_object.tag_names = 'beach surf'
351
+ assert_equal 4, tag_model.count
352
+ assert_equal 2, tagged_object.send(collection).size
353
+
354
+ tagged_object.tag_names.concat 'soccer+pele', :separator => '+'
355
+ assert_equal 6, tag_model.count
356
+ assert_equal 4, tagged_object.send(collection).size
357
+
358
+ tag_model.delete_all
359
+ assert_equal 0, tag_model.count
360
+ tagged_object.send(collection).reload
361
+
362
+ tagged_object.tag_names = 'dhh'
363
+ assert_equal 1, tag_model.count
364
+ assert_equal 1, tagged_object.send(collection).size
365
+
366
+ tagged_object.tag 'dhh rails my', :clear => true
367
+
368
+ assert_equal 3, tag_model.count
369
+ assert_equal 3, tagged_object.send(collection).size
370
+
371
+ tagged_object.tag 'dhh dhh ruby tags', :clear => true
372
+ assert_equal 5, tag_model.count
373
+ assert_equal 3, tagged_object.send(collection).size
374
+
375
+ tagged_object.tag 'tagging, hello, ruby', :separator => ','
376
+ assert_equal 7, tag_model.count
377
+ assert_equal 5, tagged_object.send(collection).size
378
+
379
+ all_tags = %w( dhh rails my ruby tags tagging hello )
380
+ first_tags = %w( dhh ruby tags tagging hello )
381
+
382
+ tagged_object.send(collection).reload
383
+ assert_equal first_tags, tagged_object.tag_names
384
+ all_tags.each do |tag_name|
385
+ tag_record = tag_model.find(:first,:conditions=>["#{@@tag_table_column_name.to_s} = ?",tag_name])
386
+ assert_not_nil tag_record
387
+
388
+ if first_tags.include?(tag_name)
389
+ assert tagged_object.send(collection).include?(tag_record)
390
+ assert tagged_object.tagged_with?(tag_name)
391
+ end
392
+ end
393
+ end
394
+
395
+ def create_test_topics
396
+ t1 = Topic.create(:title => 't1')
397
+ t2 = Topic.create(:title => 't2')
398
+ t3 = Topic.create(:title => 't3')
399
+ t4 = Topic.create(:title => 't4')
400
+ t5 = Topic.create(:title => 't5')
401
+ t6 = Topic.create(:title => 't6')
402
+
403
+ t1.tag('rome, luxury, clean, mustsee, greatview', :separator => ','); t1.save
404
+ t2.tag('rome, luxury, clean, italian, spicy, goodwine', :separator => ','); t2.save
405
+ t3.tag('rome, walking, clean, mustsee', :separator => ','); t3.save
406
+ t4.tag('rome, italy, clean, mustsee, greatview', :separator => ','); t4.save
407
+ t5.tag('rome, luxury, clean, italian, spicy, wine', :separator => ','); t5.save
408
+ t6.tag('rome, walking, clean, greatview', :separator => ','); t6.save
409
+
410
+ [ t1, t2, t3, t4, t5, t6 ]
411
+ end
412
+ end