Module: ActiveRecord::Acts::Taggable::SingletonMethods

Defined in:
lib/acts_as_taggable.rb

Instance Method Summary collapse

Instance Method Details

#caching_tag_list?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/acts_as_taggable.rb', line 159

def caching_tag_list?
  column_names.include?(cached_tag_list_column_name)
end

#find_options_for_find_tagged_with(tags, options = {}) ⇒ Object



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
# File 'lib/acts_as_taggable.rb', line 71

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_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_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 = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/acts_as_taggable.rb', line 137

def find_options_for_tag_counts(options = {})
  options = options.dup
  scope = scope(:find)
  
  conditions = []
  conditions << send(:sanitize_conditions, options.delete(:conditions)) if options[:conditions]
  conditions << send(:sanitize_conditions, scope[:conditions]) if scope && scope[:conditions]
  conditions << "#{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)}"
  conditions << type_condition unless descends_from_active_record? 
  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]
  joins << scope[:joins] if scope && scope[:joins]
  joins = joins.join(" ")
  
  options = { :conditions => conditions, :joins => joins }.update(options)
  
  Tag.options_for_counts(options)
end

Returns an array of related tags. Related tags are all the other tags that are found on the models tagged with the provided tags.

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

Options:

:order - SQL Order how to order the tags. Defaults to "count DESC, tags.name".


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/acts_as_taggable.rb', line 41

def find_related_tags(tags, options = {})
  tags = tags.is_a?(Array) ? TagList.new(tags.map(&:to_s)) : TagList.from(tags)
  
  related_models = find_tagged_with(tags)
  
  return [] if related_models.blank?
  
  related_ids = related_models.to_s(:db)
  
  Tag.find(:all, options.merge({
    :select => "#{Tag.table_name}.*, COUNT(#{Tag.table_name}.id) AS count",
    :joins  => "JOIN #{Tagging.table_name} ON #{Tagging.table_name}.taggable_type = '#{base_class.name}'
      AND  #{Tagging.table_name}.taggable_id IN (#{related_ids})
      AND  #{Tagging.table_name}.tag_id = #{Tag.table_name}.id",
    :order => options[:order] || "count DESC, #{Tag.table_name}.name",
    :group => "#{Tag.table_name}.id, #{Tag.table_name}.name HAVING #{Tag.table_name}.name NOT IN (#{tags.map { |n| quote_value(n) }.join(",")})"
  }))
end

#find_tagged_with(*args) ⇒ Object

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


66
67
68
69
# File 'lib/acts_as_taggable.rb', line 66

def find_tagged_with(*args)
  options = find_options_for_find_tagged_with(*args)
  options.blank? ? [] : find(:all, options)
end

#joins_for_match_all_tags(tags) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/acts_as_taggable.rb', line 108

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_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 = {}) ⇒ Object

Calculate the tag counts for all tags.

See Tag.counts for available options.



133
134
135
# File 'lib/acts_as_taggable.rb', line 133

def tag_counts(options = {})
  Tag.find(:all, find_options_for_tag_counts(options))
end