Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding tagged_with_any to find models with any of the given tags

Signed-off-by: Snax Fauna <evan+fauna@cloudbur.st>
  • Loading branch information...
commit bcd9626411c9d0658ab527f1e6a0d0622e4f6e15 1 parent 227a941
@saturnflyer saturnflyer authored Snax Fauna committed
Showing with 41 additions and 0 deletions.
  1. +41 −0 generators/tagging/templates/tagging_extensions.rb
View
41 generators/tagging/templates/tagging_extensions.rb
@@ -147,6 +147,47 @@ def tagged_with(*tag_list)
find_by_sql(sql)
end
+
+ def self.tagged_with_any(*tag_list)
+ options = tag_list.last.is_a?(Hash) ? tag_list.pop : {}
+ tag_list = parse_tags(tag_list)
+
+ scope = scope(:find)
+ options[:select] ||= "#{table_name}.*"
+ options[:from] ||= "#{table_name}, meta_tags, taggings"
+
+ sql = "SELECT #{(scope && scope[:select]) || options[:select]} "
+ sql << "FROM #{(scope && scope[:from]) || options[:from]} "
+
+ add_joins!(sql, options, scope)
+
+ sql << "WHERE #{table_name}.#{primary_key} = taggings.taggable_id "
+ sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' "
+ sql << "AND taggings.meta_tag_id = meta_tags.id "
+
+ sql << "AND ("
+ or_options = []
+ tag_list.each do |name|
+ or_options << "meta_tags.name = '#{name}'"
+ end
+ or_options_joined = or_options.join(" OR ")
+ sql << "#{or_options_joined}) "
+
+
+ sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions]
+
+ columns = column_names.map do |column|
+ "#{table_name}.#{column}"
+ end.join(", ")
+
+ sql << "GROUP BY #{columns} "
+
+ add_order!(sql, options[:order], scope)
+ add_limit!(sql, options, scope)
+ add_lock!(sql, options, scope)
+
+ find_by_sql(sql)
+ end
def parse_tags(tags)
return [] if tags.blank?
Please sign in to comment.
Something went wrong with that request. Please try again.