module YeshuaCrm::ActsAsTaggable::Taggable::SingletonMethods

Public Instance Methods

all_tag_counts(options = {})
Alias for: tag_counts
available_tags(options = {}) click to toggle source

Return all avalible tags for a cell or global Example: Question.available_tags(:cell => @cell_id )

# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 92
def available_tags(options = {})
  cell = options[:cell]
  limit = options[:limit].to_i.zero? ? 30 : options[:limit].to_i
  scope = Tag.where({})
  class_name = quote_string_value(base_class.name)
  join = []
  join << "JOIN #{Tagging.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id "
  join << "JOIN #{table_name} ON #{table_name}.id = #{Tagging.table_name}.taggable_id
    AND #{Tagging.table_name}.taggable_type = #{class_name} "
  if attribute_names.include?('cell_id') && cell
    join << "JOIN #{Cell.table_name} ON #{Cell.table_name}.id = #{table_name}.cell_id"
    scope = scope.where("#{table_name}.cell_id = ?", cell.id)
  end

  if options[:name_like]
    scope = scope.where("LOWER(#{Tag.table_name}.name) LIKE LOWER(?)", "%#{options[:name_like]}%")
  end

  group_fields = ''
  group_fields << ", #{Tag.table_name}.created_at" if Tag.respond_to?(:created_at)
  group_fields << ", #{Tag.table_name}.updated_at" if Tag.respond_to?(:updated_at)

  scope = scope.joins(join.join(' '))
  scope = scope.select("#{Tag.table_name}.*, COUNT(DISTINCT #{Tagging.table_name}.taggable_id) AS count")
  scope = scope.group("#{Tag.table_name}.id, #{Tag.table_name}.name #{group_fields}")
  scope = scope.having('COUNT(*) > 0')
  scope = scope.order("#{Tag.table_name}.name")
  scope = scope.limit(limit)
  scope
end
caching_tag_list?() click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 252
def caching_tag_list?
  column_names.include?(cached_tag_list_column_name)
end
find_options_for_find_tagged_with(tags, options = {}) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 159
        def find_options_for_find_tagged_with(tags, options = {})
          tags = tags.is_a?(Array) ? TagList.new(tags.map(&:to_s)) : TagList.from(tags)
          options = options.dup

          return {} if tags.empty?

          conditions = []
          conditions << sanitize_sql(options.delete(:conditions)) if options[:conditions]

          taggings_alias, tags_alias = "#{table_name}_taggings", "#{table_name}_tags"

          joins = [
            "INNER JOIN #{Tagging.table_name} #{taggings_alias} ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND #{taggings_alias}.taggable_type = #{quote_string_value(base_class.name)}",
            "INNER JOIN #{Tag.table_name} #{tags_alias} ON #{tags_alias}.id = #{taggings_alias}.tag_id"
          ]

          if options.delete(:exclude)
            conditions << <<-END
              #{table_name}.id NOT IN
                (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name}
                 INNER JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id
                 WHERE #{tags_condition(tags)} AND #{Tagging.table_name}.taggable_type = #{quote_string_value(base_class.name)})
            END
          else
            if options.delete(:match_all)
              joins << joins_for_match_all_tags(tags)
            else
              conditions << tags_condition(tags, tags_alias)
            end
          end

          { :select => "DISTINCT #{table_name}.* ",
            :joins => joins.join(" "),
            :conditions => conditions.join(" AND ")
          }.reverse_merge!(options)
        end
find_options_for_tag_counts(options = {}) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 228
def find_options_for_tag_counts(options = {})
  options = options.dup
  scope = scope_attributes
  # scope(:find)

  conditions = []
  conditions << send(:sanitize_sql_for_assignment, options.delete(:conditions)) if options[:conditions]
  conditions << send(:sanitize_sql_for_assignment, scope) if scope
  conditions << "#{Tagging.table_name}.taggable_type = #{quote_string_value(base_class.name)}"
  conditions << type_condition unless descends_from_active_record?
  conditions.delete('')
  conditions.compact!
  conditions = conditions.join(" AND ")

  joins = ["INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{Tagging.table_name}.taggable_id"]
  joins << options.delete(:joins) if options[:joins].present?
  # joins << scope[:joins] if scope && scope[:joins].present?
  joins = joins.join(" ")

  options = { :conditions => conditions, :joins => joins }.update(options)

  Tag.options_for_counts(options)
end
find_tagged_with(*args) click to toggle source

Pass either a tag, string, or an array of strings or tags.

Options:

:exclude - Find models that are not tagged with the given tags
:match_all - Find models that match all of the given tags, not just one
:conditions - A piece of SQL conditions to add to the query
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 152
def find_tagged_with(*args)
  options = find_options_for_find_tagged_with(*args)
  options.blank? ? [] : select(options[:select]).where(options[:conditions]).joins(options[:joins]).order(options[:order]).to_a
end
Also aliased as: tagged_with
joins_for_match_all_tags(tags) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 196
        def joins_for_match_all_tags(tags)
          joins = []

          tags.each_with_index do |tag, index|
            taggings_alias, tags_alias = "taggings_#{index}", "tags_#{index}"

            join = <<-END
              INNER JOIN #{Tagging.table_name} #{taggings_alias} ON
                #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND
                #{taggings_alias}.taggable_type = #{quote_string_value(base_class.name)}

              INNER JOIN #{Tag.table_name} #{tags_alias} ON
                #{taggings_alias}.tag_id = #{tags_alias}.id AND
                #{tags_alias}.name = ?
            END

            joins << sanitize_sql([join, tag])
          end

          joins.join(' ')
        end
tag_counts(options = {}) click to toggle source

Calculate the tag counts for all tags.

See Tag.counts for available options.

# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 221
def tag_counts(options = {})
  # Tag.find(:all, find_options_for_tag_counts(options))
  opt = find_options_for_tag_counts(options)
  Tag.select(opt[:select]).where(opt[:conditions]).joins(opt[:joins]).group(opt[:group]).having(opt[:having]).order(opt[:order]).limit(options[:limit])
end
Also aliased as: all_tag_counts
tagged_with(*args)
Alias for: find_tagged_with

Private Instance Methods

merge_conditions(*conditions) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 267
def merge_conditions(*conditions)
  segments = []

  conditions.each do |condition|
    unless condition.blank?
      sql = sanitize_sql(condition)
      segments << sql unless sql.blank?
    end
  end

  "(#{segments.join(') AND (')})" unless segments.empty?
end
quote_string_value(object) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 258
def quote_string_value(object)
  connection.quote(object)
end
tags_condition(tags, table_name = Tag.table_name) click to toggle source
# File lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb, line 262
def tags_condition(tags, table_name = Tag.table_name)
  condition = tags.map { |t| sanitize_sql(["#{table_name}.name LIKE ?", t]) }.join(" OR ")
  "(" + condition + ")" unless condition.blank?
end