Module: ActiveRecord::Base::TaggingFinders

Included in:
ActiveRecord::Base
Defined in:
lib/tagging_extensions.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) parse_tags(tags)



279
280
281
282
283
284
# File 'lib/tagging_extensions.rb', line 279

def parse_tags(tags)
  return [] if tags.blank?
  tags = Array(tags).first
  tags = tags.respond_to?(:flatten) ? tags.flatten : tags.split(Tag::SPLITTER)
  tags.map { |tag| Tag.normalizename(tag) }.flatten.compact.uniq
end

- (Object) tag_frequency(options = {})



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tagging_extensions.rb', line 253

def tag_frequency(options={})
  options[:from] ||= "#{table_name}, tags, taggings"
  
  sql  = "SELECT tags.*, COUNT(taggings.tag_id) as frequency, SUM(taggings.weight) as weightedfrequency "
  sql << "FROM #{options[:from]} "

  #add_joins!(sql, options)
  
  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 "            
  sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions]
  sql << "GROUP BY tags.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
        
  add_order!(sql, options[:order], nil)
  add_limit!(sql, options, nil)
  add_lock!(sql, options, nil)
  return Tag.find_by_sql(sql)
end

- (Object) tagged_with_any(*tag_list)



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]} "

  #add_joins!(sql, options, nil)
  
  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