Module: ActsAsTaggableOn::Taggable::Core::ClassMethods
- Defined in:
- lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Instance Method Summary collapse
- #acts_as_taggable_on(*args) ⇒ Object
-
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause.
- #initialize_acts_as_taggable_on_core ⇒ Object
- #is_taggable? ⇒ Boolean
-
#tagged_with(tags, options = {}) ⇒ Object
Return a scope of objects that are tagged with the specified tags.
Instance Method Details
#acts_as_taggable_on(*args) ⇒ Object
44 45 46 47 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 44 def acts_as_taggable_on(*args) super(*args) initialize_acts_as_taggable_on_core end |
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause
50 51 52 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 50 def grouped_column_names_for(object) object.column_names.map { |column| "#{object.table_name}.#{column}" }.join(", ") end |
#initialize_acts_as_taggable_on_core ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 16 def initialize_acts_as_taggable_on_core tag_types.map(&:to_s).each do || tag_type = .to_s.singularize context_taggings = "#{tag_type}_taggings".to_sym = .to_sym class_eval do has_many context_taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "Tagging", :conditions => ['#{Tagging.table_name}.tagger_id IS NULL AND #{Tagging.table_name}.context = ?', ] has_many , :through => context_taggings, :source => :tag end class_eval %( def #{tag_type}_list tag_list_on('#{}') end def #{tag_type}_list=(new_tags) set_tag_list_on('#{}', new_tags) end def all_#{}_list all_tags_list_on('#{}') end ) end end |
#is_taggable? ⇒ Boolean
123 124 125 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 123 def is_taggable? true end |
#tagged_with(tags, options = {}) ⇒ Object
Return a scope of objects that are tagged with the specified tags.
Example:
User.tagged_with("awesome", "cool") # Users that are tagged with awesome and cool
User.tagged_with("awesome", "cool", :exclude => true) # Users that are not tagged with awesome or cool
User.tagged_with("awesome", "cool", :any => true) # Users that are tagged with awesome or cool
User.tagged_with("awesome", "cool", :match_all => true) # Users that are tagged with just awesome and cool
68 69 70 71 72 73 74 75 76 77 78 79 80 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 117 118 119 120 121 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 68 def tagged_with(, = {}) tag_list = TagList.from() return {} if tag_list.empty? joins = [] conditions = [] context = .delete(:on) if .delete(:exclude) = tag_list.map { |t| sanitize_sql(["#{Tag.table_name}.name LIKE ?", t]) }.join(" OR ") conditions << "#{table_name}.#{primary_key} NOT IN (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name} JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND (#{}) WHERE #{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})" elsif .delete(:any) = tag_list.map { |t| sanitize_sql(["#{Tag.table_name}.name LIKE ?", t]) }.join(" OR ") conditions << "#{table_name}.#{primary_key} IN (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name} JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND (#{}) WHERE #{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})" else = Tag.named_any(tag_list) return where("1 = 0") unless .length == tag_list.length .each do |tag| safe_tag = tag.name.gsub(/[^a-zA-Z0-9]/, '') prefix = "#{safe_tag}_#{rand(1024)}" taggings_alias = "#{table_name}_taggings_#{prefix}" tagging_join = "JOIN #{Tagging.table_name} #{taggings_alias}" + " ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" + " AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}" + " AND #{taggings_alias}.tag_id = #{tag.id}" tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context joins << tagging_join end end taggings_alias, = "#{table_name}_taggings_group", "#{table_name}_tags_group" if .delete(:match_all) joins << "LEFT OUTER JOIN #{Tagging.table_name} #{taggings_alias}" + " ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" + " AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}" group = "#{grouped_column_names_for(self)} HAVING COUNT(#{taggings_alias}.taggable_id) = #{.size}" end scoped(:joins => joins.join(" "), :group => group, :conditions => conditions.join(" AND "), :readonly => false) end |