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
51
52
# 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
  
  group_columns = ActsAsTaggableOn::Tag.using_postgresql? ? grouped_column_names_for(klass) : "#{klass.table_name}.#{klass.primary_key}"
  
  klass.scoped({ :select     => "#{klass.table_name}.*, COUNT(#{ActsAsTaggableOn::Tag.table_name}.id) AS count",
                 :from       => "#{klass.table_name}, #{ActsAsTaggableOn::Tag.table_name}, #{ActsAsTaggableOn::Tagging.table_name}",
                 :conditions => ["#{exclude_self} #{klass.table_name}.id = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id AND #{ActsAsTaggableOn::Tag.table_name}.name IN (?) AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", tags_to_find, result_context],
                 :group      => group_columns,
                 :order      => "count DESC" }.update(options))
end


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/related.rb', line 54

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

group_columns = ActsAsTaggableOn::Tag.using_postgresql? ? grouped_column_names_for(klass) : "#{klass.table_name}.#{klass.primary_key}"

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