Module: ActsAsTaggableOn::Taggable::Related::InstanceMethods

Defined in:
lib/acts_as_taggable_on/acts_as_taggable_on/related.rb

Instance Method Summary collapse

Instance Method Details

#matching_contexts_for(search_context, result_context, klass, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/related.rb', line 40

def matching_contexts_for(search_context, result_context, klass, options = {})
  tags_to_find = tags_on(search_context).collect { |t| t.name }

  exclude_self = "#{klass.table_name}.id != #{id} AND" if self.class == klass
  
  klass.scoped({ :select     => "#{klass.table_name}.*, COUNT(#{Tag.table_name}.id) AS count",
                 :from       => "#{klass.table_name}, #{Tag.table_name}, #{Tagging.table_name}",
                 :conditions => ["#{exclude_self} #{klass.table_name}.id = #{Tagging.table_name}.taggable_id AND #{Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND #{Tag.table_name}.name IN (?) AND #{Tagging.table_name}.context = ?", tags_to_find, result_context],
                 :group      => grouped_column_names_for(klass),
                 :order      => "count DESC" }.update(options))
end


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/related.rb', line 52

def related_tags_for(context, klass, options = {})
  tags_to_find = tags_on(context).collect { |t| t.name }

  exclude_self = "#{klass.table_name}.id != #{id} AND" if self.class == klass

  klass.scoped({ :select     => "#{klass.table_name}.*, COUNT(#{Tag.table_name}.id) AS count",
                 :from       => "#{klass.table_name}, #{Tag.table_name}, #{Tagging.table_name}",
                 :conditions => ["#{exclude_self} #{klass.table_name}.id = #{Tagging.table_name}.taggable_id AND #{Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND #{Tag.table_name}.name IN (?)", tags_to_find],
                 :group      => grouped_column_names_for(klass),
                 :order      => "count DESC" }.update(options))
end