202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/tagging_extensions.rb', line 202
def tagged_with_any(*tag_list)
options = tag_list.last.is_a?(Hash) ? tag_list.pop : {}
tag_list = parse_tags(tag_list)
if(options[:getfrequency])
options[:select] ||= "#{table_name}.*, tags.id as tag_id, COUNT(taggings.tag_id) as frequency, SUM(taggings.weight) as weightedfrequency"
else
options[:select] ||= "#{table_name}.*"
end
options[:from] ||= "#{table_name}, tags, taggings"
sql = "SELECT #{options[:select]} "
sql << "FROM #{options[:from]} "
sql << "WHERE #{table_name}.#{primary_key} = taggings.taggable_id "
sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' "
sql << "AND taggings.tag_id = tags.id "
tag_list_condition = tag_list.map {|t| "'#{t}'"}.join(", ")
sql << "AND (tags.name IN (#{sanitize_sql(tag_list_condition)})) "
sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions]
if(options[:getfrequency])
sql << "GROUP BY #{table_name}.id,tag_id "
if(!options[:minweight].nil?)
sql << "HAVING SUM(taggings.weight) >= #{options[:minweight]} "
elsif(!options[:mincount].nil?)
sql << "HAVING COUNT(taggings.tag_id) >= #{options[:mincount]} "
end
else
sql << "GROUP BY #{table_name}.id "
matchall = options[:matchall] || false
if(matchall)
sql << "HAVING COUNT(taggings.tag_id) = #{tag_list.size} "
end
end
add_order!(sql, options[:order], nil)
add_limit!(sql, options, nil)
add_lock!(sql, options, nil)
paginate = options[:paginate] || false
if(paginate and !options[:getfrequency])
paginate_by_sql(sql,{:page => options[:page], :per_page => self.per_page})
else
find_by_sql(sql)
end
end
|