tagtools 0.0.1 → 0.0.2

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 CHANGED
@@ -1,2 +1,7 @@
1
+ == TagTools 0.0.2
2
+ * fixed a bug in delete_records for user tag collections
3
+ * added include? method to all tag collections
4
+ * the delete method no longer errors out for nil parameters
5
+ * removed code duplication
1
6
  == TagTools 0.0.1
2
7
  * folksonomy!
data/README CHANGED
@@ -28,13 +28,11 @@ schema.
28
28
  slashdot_bookmark.user_tags(current_user.id).concat( "geeky", "news", "technology" )
29
29
  slashdot_bookmark.tags
30
30
  => [ tag:"geeky", tag:"news", tag:"technology" ]
31
- slashdot_bookmark.user_tags(current_user).delete( "news" )
31
+ slashdot_bookmark.user_tags(current_user.id).delete( "news" )
32
32
  slashdot_bookmark.tags
33
33
  => [ tag:"geeky", tag:"technology" ]
34
- slashdot_bookmark.user_tags(current_user).concat( "technology", "linux" )
34
+ slashdot_bookmark.user_tags(current_user.id).concat( "technology", "linux" )
35
35
  slashdot_bookmark.tags
36
36
  => [ tag:"geeky", tag:"technology", tag:"linux" ]
37
- Bookmark.tag_query( :user_id => current_user, :with_any_tags => ["linux"] )
38
- => []
39
37
  Bookmark.tag_query( :with_any_tags => ["linux"] )
40
38
  => [ #<Bookmark:0x77dc54 @attributes={"url"=>"http://slashdot.org"}> ]
@@ -21,7 +21,7 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #++
23
23
 
24
- TAG_TOOLS_VERSION = "0.0.1"
24
+ TAG_TOOLS_VERSION = "0.0.2"
25
25
 
26
26
  $:.unshift(File.dirname(__FILE__))
27
27
  $:.unshift(File.dirname(__FILE__) + "/../../activerecord/lib")
@@ -122,6 +122,12 @@ module ActiveRecord #:nodoc:
122
122
 
123
123
  alias :concat_with_attributes :push_with_attributes
124
124
 
125
+ def include?(raw_tag)
126
+ actual_tag = get_tag(raw_tag)
127
+ return false if actual_tag.nil?
128
+ return @target.include?(actual_tag)
129
+ end
130
+
125
131
  def size
126
132
  count_records
127
133
  end
@@ -132,6 +138,7 @@ module ActiveRecord #:nodoc:
132
138
  @owner.transaction do
133
139
  flatten_deeper(records).each do |record|
134
140
  record = create_tag(record)
141
+ next if self.include?(record)
135
142
  callback(:before_add, record)
136
143
  result &&= insert_record(record) unless @owner.new_record?
137
144
  @target << record
@@ -152,7 +159,13 @@ module ActiveRecord #:nodoc:
152
159
  records[index] = create_tag(records[index])
153
160
  end
154
161
  records.reject! do |record|
155
- @target.delete(record) if record.new_record?
162
+ if record.nil?
163
+ true
164
+ elsif record.new_record?
165
+ @target.delete(record)
166
+ else
167
+ false
168
+ end
156
169
  end
157
170
  return if records.empty?
158
171
 
@@ -168,6 +181,7 @@ module ActiveRecord #:nodoc:
168
181
 
169
182
  protected
170
183
  def create_tag(raw_tag)
184
+ return nil if raw_tag.nil?
171
185
  if raw_tag.kind_of? @tag_class
172
186
  return raw_tag
173
187
  end
@@ -180,6 +194,15 @@ module ActiveRecord #:nodoc:
180
194
  return tag_object
181
195
  end
182
196
 
197
+ def get_tag(raw_tag)
198
+ return nil if raw_tag.nil?
199
+ if raw_tag.kind_of? @tag_class
200
+ return raw_tag
201
+ end
202
+ tag_object = @tag_class.find_by_name(raw_tag.to_s)
203
+ return tag_object
204
+ end
205
+
183
206
  def find_target(sql = @finder_sql)
184
207
  records = @tag_class.find_by_sql(sql)
185
208
  uniq(records)
@@ -235,18 +258,31 @@ module ActiveRecord #:nodoc:
235
258
  end
236
259
 
237
260
  def construct_sql
238
- @finder_sql =
239
- "SELECT t.*, j.* FROM #{@join_table} j, " +
240
- " #{@tag_class.table_name} t " +
241
- "WHERE t.#{@tag_class.primary_key} = " +
242
- " j.#{@tag_foreign_key} " +
243
- "AND j.#{@item_foreign_key} = " +
244
- " #{@owner.quoted_id} " +
245
- "ORDER BY t.name"
261
+ if @user_id.nil?
262
+ @finder_sql =
263
+ "SELECT t.*, j.* FROM #{@join_table} j, " +
264
+ " #{@tag_class.table_name} t " +
265
+ "WHERE t.#{@tag_class.primary_key} = " +
266
+ " j.#{@tag_foreign_key} " +
267
+ "AND j.#{@item_foreign_key} = " +
268
+ " #{@owner.quoted_id} " +
269
+ "ORDER BY t.name"
270
+ else
271
+ @finder_sql =
272
+ "SELECT t.*, j.* FROM #{@join_table} j, " +
273
+ " #{@tag_class.table_name} t " +
274
+ "WHERE t.#{@tag_class.primary_key} = " +
275
+ " j.#{@tag_foreign_key} " +
276
+ "AND j.#{@item_foreign_key} = " +
277
+ " #{@owner.quoted_id} " +
278
+ "AND j.#{@user_foreign_key} = " +
279
+ " '#{@user_id}' " +
280
+ "ORDER BY t.name"
281
+ end
246
282
  end
247
283
  end
248
284
 
249
- class UserTagsAssociation < AssociationCollection #:nodoc:
285
+ class UserTagsAssociation < GlobalTagsAssociation #:nodoc:
250
286
  def initialize(owner, user_id, tag_class, user_class, item_class,
251
287
  options)
252
288
  @owner = owner
@@ -358,16 +394,13 @@ module ActiveRecord #:nodoc:
358
394
 
359
395
  alias :concat_with_attributes :push_with_attributes
360
396
 
361
- def size
362
- count_records
363
- end
364
-
365
397
  def <<(*records)
366
398
  result = true
367
399
  load_target
368
400
  @owner.transaction do
369
401
  flatten_deeper(records).each do |record|
370
402
  record = create_tag(record)
403
+ next if self.include?(record)
371
404
  callback(:before_add, record)
372
405
  result &&= insert_record(record) unless @owner.new_record?
373
406
  @target << record
@@ -382,50 +415,7 @@ module ActiveRecord #:nodoc:
382
415
  alias_method :push, :<<
383
416
  alias_method :concat, :<<
384
417
 
385
- # Remove +records+ from this association. Does not destroy +records+.
386
- def delete(*records)
387
- records = flatten_deeper(records)
388
- for index in 0..records.size
389
- records[index] = create_tag(records[index])
390
- end
391
- records.reject! do |record|
392
- @target.delete(record) if record.new_record?
393
- end
394
- return if records.empty?
395
-
396
- @owner.transaction do
397
- records.each { |record| callback(:before_remove, record) }
398
- delete_records(records)
399
- records.each do |record|
400
- @target.delete(record)
401
- callback(:after_remove, record)
402
- end
403
- end
404
- end
405
-
406
418
  protected
407
- def create_tag(raw_tag)
408
- if raw_tag.kind_of? @tag_class
409
- return raw_tag
410
- end
411
- tag_object = @tag_class.find_by_name(raw_tag.to_s)
412
- if tag_object.nil?
413
- tag_object = @tag_class.new
414
- tag_object.name = raw_tag.to_s
415
- tag_object.save
416
- end
417
- return tag_object
418
- end
419
-
420
- def find_target(sql = @finder_sql)
421
- records = @tag_class.find_by_sql(sql)
422
- uniq(records)
423
- end
424
-
425
- def count_records
426
- load_target.size
427
- end
428
-
429
419
  def insert_record(record, join_attributes = {})
430
420
  if @user_id.nil?
431
421
  raise "Cannot insert record if the user id is not set."
@@ -476,34 +466,10 @@ module ActiveRecord #:nodoc:
476
466
  sql = "DELETE FROM #{@join_table} " +
477
467
  "WHERE #{@item_foreign_key} = " +
478
468
  " #{@owner.quoted_id} " +
479
- "AND #{@user_foreign_key} = '#{@user_id.to_s}'"
469
+ "AND #{@user_foreign_key} = '#{@user_id.to_s}'" +
480
470
  "AND #{@tag_foreign_key} IN (#{ids})"
481
471
  @owner.connection.execute(sql)
482
472
  end
483
-
484
- def construct_sql
485
- if @user_id.nil?
486
- @finder_sql =
487
- "SELECT t.*, j.* FROM #{@join_table} j, " +
488
- " #{@tag_class.table_name} t " +
489
- "WHERE t.#{@tag_class.primary_key} = " +
490
- " j.#{@tag_foreign_key} " +
491
- "AND j.#{@item_foreign_key} = " +
492
- " #{@owner.quoted_id} " +
493
- "ORDER BY t.name"
494
- else
495
- @finder_sql =
496
- "SELECT t.*, j.* FROM #{@join_table} j, " +
497
- " #{@tag_class.table_name} t " +
498
- "WHERE t.#{@tag_class.primary_key} = " +
499
- " j.#{@tag_foreign_key} " +
500
- "AND j.#{@item_foreign_key} = " +
501
- " #{@owner.quoted_id} " +
502
- "AND j.#{@user_foreign_key} = " +
503
- " '#{@user_id}' " +
504
- "ORDER BY t.name"
505
- end
506
- end
507
473
  end
508
474
  end
509
475
 
data/rakefile CHANGED
@@ -7,7 +7,7 @@ require 'rake/gempackagetask'
7
7
  require 'rake/contrib/rubyforgepublisher'
8
8
 
9
9
  PKG_NAME = 'tagtools'
10
- PKG_VERSION = '0.0.1'
10
+ PKG_VERSION = '0.0.2'
11
11
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
12
12
 
13
13
  RELEASE_NAME = "REL #{PKG_VERSION}"
@@ -100,4 +100,46 @@ class GlobalTagsTest < Test::Unit::TestCase
100
100
  assert_equal(ruby_on_rails, results.first)
101
101
  assert(!results.include?(red_handed))
102
102
  end
103
+
104
+ def test_include
105
+ ruby_on_rails = Image.new
106
+ ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif"
107
+ ruby_on_rails.save
108
+ ruby_on_rails.tags << "ruby"
109
+ ruby_on_rails.tags << "rails"
110
+ ruby_on_rails.tags << "framework"
111
+ assert_equal(true,
112
+ ruby_on_rails.tags(true).include?("rails"))
113
+ assert_equal(false,
114
+ ruby_on_rails.tags(true).include?("notpresent"))
115
+ assert_equal(true,
116
+ ruby_on_rails.tags(true).include?(
117
+ Tag.find_by_name("framework")))
118
+ assert_equal(false,
119
+ ruby_on_rails.tags(true).include?(
120
+ Tag.find_by_name("alsonotpresent")))
121
+ end
122
+
123
+ def test_size
124
+ ruby_on_rails = Image.new
125
+ ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif"
126
+ ruby_on_rails.save
127
+ ruby_on_rails.tags << "ruby"
128
+ ruby_on_rails.tags << "rails"
129
+ ruby_on_rails.tags << "framework"
130
+ assert_equal(3,
131
+ ruby_on_rails.tags(true).size)
132
+ ruby_on_rails.tags(true) << "rails"
133
+ assert_equal(3,
134
+ ruby_on_rails.tags(true).size)
135
+ ruby_on_rails.tags << "web"
136
+ assert_equal(4,
137
+ ruby_on_rails.tags(true).size)
138
+ ruby_on_rails.tags.delete("framework")
139
+ assert_equal(3,
140
+ ruby_on_rails.tags.size)
141
+ ruby_on_rails.tags.delete("ruby")
142
+ assert_equal(2,
143
+ ruby_on_rails.tags(true).size)
144
+ end
103
145
  end
@@ -113,4 +113,52 @@ class UserTagsTest < Test::Unit::TestCase
113
113
  assert_equal(ruby_on_rails, results.first)
114
114
  assert(!results.include?(red_handed))
115
115
  end
116
+
117
+ def test_include
118
+ some_guy = User.new
119
+ some_guy.name = "Joe Normal"
120
+ some_guy.save
121
+ ruby_on_rails = Bookmark.new
122
+ ruby_on_rails.url = "http://www.rubyonrails.com"
123
+ ruby_on_rails.save
124
+ ruby_on_rails.user_tags(some_guy.id) << "ruby"
125
+ ruby_on_rails.user_tags(some_guy.id) << "rails"
126
+ ruby_on_rails.user_tags(some_guy.id) << "framework"
127
+ assert_equal(true,
128
+ ruby_on_rails.user_tags(some_guy.id, true).include?("rails"))
129
+ assert_equal(false,
130
+ ruby_on_rails.user_tags(some_guy.id, true).include?("notpresent"))
131
+ assert_equal(true,
132
+ ruby_on_rails.user_tags(some_guy.id, true).include?(
133
+ Tag.find_by_name("framework")))
134
+ assert_equal(false,
135
+ ruby_on_rails.user_tags(some_guy.id, true).include?(
136
+ Tag.find_by_name("alsonotpresent")))
137
+ end
138
+
139
+ def test_size
140
+ some_guy = User.new
141
+ some_guy.name = "Joe Normal"
142
+ some_guy.save
143
+ ruby_on_rails = Bookmark.new
144
+ ruby_on_rails.url = "http://www.rubyonrails.com"
145
+ ruby_on_rails.save
146
+ ruby_on_rails.user_tags(some_guy.id) << "ruby"
147
+ ruby_on_rails.user_tags(some_guy.id) << "rails"
148
+ ruby_on_rails.user_tags(some_guy.id) << "framework"
149
+ assert_equal(3,
150
+ ruby_on_rails.user_tags(some_guy.id, true).size)
151
+ ruby_on_rails.user_tags(some_guy.id, true) << "rails"
152
+ assert_equal(3,
153
+ ruby_on_rails.user_tags(some_guy.id, true).size)
154
+ ruby_on_rails.user_tags(some_guy.id) << "web"
155
+ assert_equal(4,
156
+ ruby_on_rails.user_tags(some_guy.id, true).size)
157
+ ruby_on_rails.user_tags(some_guy.id).delete("framework")
158
+ assert_equal(3,
159
+ ruby_on_rails.user_tags(some_guy.id).size)
160
+ ruby_on_rails.user_tags(some_guy.id).delete("ruby")
161
+ assert_equal(2,
162
+ ruby_on_rails.user_tags(some_guy.id, true).size)
163
+ end
116
164
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: tagtools
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.1
7
- date: 2005-08-28 00:00:00 -04:00
6
+ version: 0.0.2
7
+ date: 2005-09-09 00:00:00 -04:00
8
8
  summary: Folksonomy system for Rails.
9
9
  require_paths:
10
10
  - lib