hi,
I started today to use the acts as taggable plugin and it''s working as
expected.
I added a method find_tagged_with_all since I needed to find the
elements tagged with all the items in a list while find_tagged_with
finds all the elements tagged with any of the list terms.
Attached to this mail there''s a patch which adds the
find_tagged_with_all method and which solves a small problem with having
duplicate elements being found by he find_tagged_with method when more
than one tag matches (it just required adding a "distinct" to the
query).
I have just one question regarding the plugins which i have not clear:
is there any standard or convention to be used when preparng tests for
them? While i have clear the process for regular rails apps, I wanted to
build them for this diff but didn''t know where to start.
Thanks
Luca Mearelli
--
Email mailto://l.mearelli@spazidigitali.com
Skype callto://l.mearelli
Blog: http://spazidigitali.com
--
-------------- next part --------------
Index: lib/acts_as_taggable.rb
==================================================================---
lib/acts_as_taggable.rb (revision 3214)
+++ lib/acts_as_taggable.rb (working copy)
@@ -25,12 +25,23 @@
module SingletonMethods
def find_tagged_with(list)
find_by_sql(
- "SELECT #{table_name}.* FROM #{table_name}, tags, taggings
" +
+ "SELECT DISTINCT #{table_name}.* FROM #{table_name}, tags,
taggings " +
"WHERE #{table_name}.#{primary_key} = taggings.taggable_id
" +
"AND taggings.taggable_type =
''#{acts_as_taggable_options[:taggable_type]}'' " +
"AND taggings.tag_id = tags.id AND tags.name IN
(#{list.collect { |name| "''#{name}''" }.join(",
")})"
)
end
+
+ def find_tagged_with_all(list)
+ find_by_sql(
+ "SELECT #{table_name}.* FROM #{table_name}, " +
+ "(SELECT taggings.taggable_id as id, count(*) as cnt FROM
tags, taggings " +
+ " WHERE taggings.taggable_type =
''#{acts_as_taggable_options[:taggable_type]}'' " +
+ " AND taggings.tag_id = tags.id AND tags.name IN
(#{list.collect { |name| "''#{name}''" }.join(",
")}) " +
+ " GROUP BY taggings.taggable_id) A " +
+ "WHERE #{table_name}.#{primary_key} = A.id AND A.cnt=
#{list.uniq.size} "
+ )
+ end
end
module InstanceMethods