Module: ActsAsTaggableOn::Taggable::Core::ClassMethods
- Defined in:
- lib/acts_as_taggable_on/taggable/core.rb
Instance Method Summary collapse
-
#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
- #taggable_mixin ⇒ Object
- #taggable_on(preserve_tag_order, *tag_types) ⇒ Object
-
#tagged_with(tags, options = {}) ⇒ Object
Return a scope of objects that are tagged with the specified tags.
Instance Method Details
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause
84 85 86 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 84 def grouped_column_names_for(object) object.column_names.map { |column| "#{object.table_name}.#{column}" }.join(', ') end |
#initialize_acts_as_taggable_on_core ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 22 def initialize_acts_as_taggable_on_core include taggable_mixin tag_types.map(&:to_s).each do || tag_type = .to_s.singularize context_taggings = "#{tag_type}_taggings".to_sym = .to_sym taggings_order = (preserve_tag_order? ? "#{ActsAsTaggableOn::Tagging.table_name}.id" : []) class_eval do # when preserving tag order, include order option so that for a 'tags' context # the associations tag_taggings & tags are always returned in created order has_many context_taggings, -> { includes(:tag).order(taggings_order).where(context: ) }, as: :taggable, class_name: 'ActsAsTaggableOn::Tagging', dependent: :destroy, after_add: :dirtify_tag_list, after_remove: :dirtify_tag_list has_many , -> { order(taggings_order) }, class_name: 'ActsAsTaggableOn::Tag', through: context_taggings, source: :tag attribute "#{.singularize}_list".to_sym, ActsAsTaggableOn::Taggable::TagListType.new end taggable_mixin.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{tag_type}_list tag_list_on('#{}') end def #{tag_type}_list=(new_tags) parsed_new_list = ActsAsTaggableOn.default_parser.new(new_tags).parse if self.class.preserve_tag_order? || (parsed_new_list.sort != #{tag_type}_list.sort) unless #{tag_type}_list_changed? @attributes["#{tag_type}_list"] = ActiveModel::Attribute.from_user("#{tag_type}_list", #{tag_type}_list, ActsAsTaggableOn::Taggable::TagListType.new) end write_attribute("#{tag_type}_list", parsed_new_list) end set_tag_list_on('#{}', new_tags) end def all_#{}_list all_tags_list_on('#{}') end private def dirtify_tag_list(tagging) attribute_will_change! tagging.context.singularize+"_list" end RUBY end end |
#is_taggable? ⇒ Boolean
119 120 121 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 119 def is_taggable? true end |
#taggable_mixin ⇒ Object
123 124 125 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 123 def taggable_mixin @taggable_mixin ||= Module.new end |
#taggable_on(preserve_tag_order, *tag_types) ⇒ Object
78 79 80 81 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 78 def taggable_on(preserve_tag_order, *tag_types) super(preserve_tag_order, *tag_types) initialize_acts_as_taggable_on_core 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"], :any => true, :order_by_matching_tag_count => true) # Sort by users who match the most tags, descending
User.tagged_with(["awesome", "cool"], :match_all => true) # Users that are tagged with just awesome and cool
User.tagged_with(["awesome", "cool"], :owned_by => foo ) # Users that are tagged with just awesome and cool by 'foo'
User.tagged_with(["awesome", "cool"], :owned_by => foo, :start_at => Date.today ) # Users that are tagged with just awesome, cool by 'foo' and starting today
109 110 111 112 113 114 115 116 117 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 109 def tagged_with(, = {}) tag_list = ActsAsTaggableOn.default_parser.new().parse = .dup return none if tag_list.empty? ::ActsAsTaggableOn::Taggable::TaggedWithQuery.build(self, ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, tag_list, ) end |