Module: Tagtical::Taggable::Related::InstanceMethods

Defined in:
lib/tagtical/taggable/related.rb

Instance Method Summary collapse

Instance Method Details

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



40
41
42
# File 'lib/tagtical/taggable/related.rb', line 40

def matching_contexts_for(search_context, result_context, klass, options = {})
  related_tags_for(search_context, klass, options.update(:result_context => result_context))
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tagtical/taggable/related.rb', line 44

def related_tags_for(context, klass, options = {})
  tags_to_find = tags_on(context).collect { |t| t.value }
  exclude_self = "#{klass.table_name}.id != #{id} AND" if self.class == klass
  group_columns = Tagtical::Tag.using_postgresql? ? grouped_column_names_for(klass) : "#{klass.table_name}.#{klass.primary_key}"

  conditions = ["#{exclude_self} #{klass.table_name}.id = #{Tagtical::Tagging.table_name}.taggable_id AND #{Tagtical::Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{Tagtical::Tagging.table_name}.tag_id = #{Tagtical::Tag.table_name}.id AND #{Tagtical::Tag.table_name}.value IN (?)", tags_to_find]
  conditions[0] << find_tag_type!(options.delete(:result_context)).finder_type_condition(:sql => :append).to_s if options[:result_context]
          
  klass.scoped({ :select     => "#{klass.table_name}.*, COUNT(#{Tagtical::Tag.table_name}.id) AS count",
                 :from       => "#{klass.table_name}, #{Tagtical::Tag.table_name}, #{Tagtical::Tagging.table_name}",
                 :conditions => conditions,
                 :group      => group_columns,
                 :order      => "count DESC" }.update(options))
end