module ActsAsTaggableOn::Taggable::Core::ClassMethods

Public Instance Methods

grouped_column_names_for(object) click to toggle source

all column names are necessary for PostgreSQL group clause

# File lib/acts_as_taggable_on/taggable/core.rb, line 61
def grouped_column_names_for(object)
  object.column_names.map { |column| "#{object.table_name}.#{column}" }.join(', ')
end
initialize_acts_as_taggable_on_core() click to toggle source
# File lib/acts_as_taggable_on/taggable/core.rb, line 17
      def initialize_acts_as_taggable_on_core
        include taggable_mixin
        tag_types.map(&:to_s).each do |tags_type|
          tag_type = tags_type.to_s.singularize
          context_taggings = "#{tag_type}_taggings".to_sym
          context_tags = tags_type.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: tags_type) },
                     as: :taggable,
                     class_name: 'ActsAsTaggableOn::Tagging',
                     dependent: :destroy

            has_many context_tags, -> { order(taggings_order) },
                     class_name: 'ActsAsTaggableOn::Tag',
                     through: context_taggings,
                     source: :tag
          end

          taggable_mixin.class_eval "            def #{tag_type}_list
              tag_list_on('#{tags_type}')
            end

            def #{tag_type}_list=(new_tags)
              set_tag_list_on('#{tags_type}', new_tags)
            end

            def all_#{tags_type}_list
              all_tags_list_on('#{tags_type}')
            end
", __FILE__, __LINE__ + 1
        end
      end
is_taggable?() click to toggle source
# File lib/acts_as_taggable_on/taggable/core.rb, line 96
def is_taggable?
  true
end
taggable_mixin() click to toggle source
# File lib/acts_as_taggable_on/taggable/core.rb, line 100
def taggable_mixin
  @taggable_mixin ||= Module.new
end
taggable_on(preserve_tag_order, *tag_types) click to toggle source
Calls superclass method
# File lib/acts_as_taggable_on/taggable/core.rb, line 55
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 = {}) click to toggle source

Return a scope of objects that are tagged with the specified tags.

@param tags The tags that we want to query for @param [Hash] options A hash of options to alter you query:

* <tt>:exclude</tt> - if set to true, return objects that are *NOT* tagged with the specified tags
* <tt>:any</tt> - if set to true, return objects that are tagged with *ANY* of the specified tags
* <tt>:order_by_matching_tag_count</tt> - if set to true and used with :any, sort by objects matching the most tags, descending
* <tt>:match_all</tt> - if set to true, return objects that are *ONLY* tagged with the specified tags
* <tt>:owned_by</tt> - return objects that are *ONLY* owned by the owner
* <tt>:start_at</tt> - Restrict the tags to those created after a certain time
* <tt>:end_at</tt> - Restrict the tags to those created before a certain time

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
# File lib/acts_as_taggable_on/taggable/core.rb, line 86
def tagged_with(tags, options = {})
  tag_list = ActsAsTaggableOn.default_parser.new(tags).parse
  options = options.dup
  empty_result = where('1 = 0')

  return empty_result if tag_list.empty?

  ::ActsAsTaggableOn::Taggable::TaggedWithQuery.build(self, ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, tag_list, options)
end