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
|