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.
- data/CHANGELOG +10 -0
- data/README +89 -70
- data/lib/taggable.rb +610 -467
- data/test/acts_as_taggable_test.rb +412 -384
- 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
|
-
|
10
|
-
|
11
|
-
ActiveRecord::Base.connection.drop_table :
|
12
|
-
ActiveRecord::Base.connection.drop_table :
|
13
|
-
ActiveRecord::Base.connection.drop_table :
|
14
|
-
|
15
|
-
ActiveRecord::Base.connection.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
t.column :
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
t.column :
|
38
|
-
t.column :
|
39
|
-
t.column :
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
class
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
class
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
assert
|
78
|
-
assert
|
79
|
-
assert
|
80
|
-
assert
|
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
|
-
posts
|
117
|
-
|
118
|
-
|
119
|
-
posts
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
posts
|
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
|
-
assert_equal
|
161
|
-
assert_equal 1, count['
|
162
|
-
assert_equal
|
163
|
-
|
164
|
-
|
165
|
-
assert_equal
|
166
|
-
assert_equal
|
167
|
-
assert_equal
|
168
|
-
|
169
|
-
|
170
|
-
assert_equal
|
171
|
-
assert_equal nil, count['
|
172
|
-
assert_equal
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
assert_equal
|
184
|
-
assert_equal nil, count['
|
185
|
-
assert_equal
|
186
|
-
|
187
|
-
|
188
|
-
assert_equal
|
189
|
-
assert_equal
|
190
|
-
assert_equal
|
191
|
-
|
192
|
-
|
193
|
-
assert_equal
|
194
|
-
assert_equal
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
assert_equal [
|
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
|
-
assert topics.include?(
|
247
|
-
assert topics.include?(
|
248
|
-
|
249
|
-
topics = Topic.find_tagged_with(:any => '
|
250
|
-
assert_equal
|
251
|
-
assert topics.include?(topic1)
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
assert topics.include?(
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
assert topics.include?(
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
assert topics.include?(topic3)
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
topics = Topic.find_tagged_with(:all => 'a
|
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
|
292
|
-
assert_equal 2, topics.size
|
293
|
-
assert topics.include?(
|
294
|
-
assert topics.include?(
|
295
|
-
|
296
|
-
topics = Topic.find_tagged_with(:all => 'c')
|
297
|
-
assert_equal
|
298
|
-
assert topics.include?(
|
299
|
-
assert topics.include?(
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
assert topics.include?(
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
assert_equal
|
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
|
-
assert_equal
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
tagged_object.
|
355
|
-
assert_equal
|
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
|
-
|
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
|