avatar

17

Refactored the find_with method, and added IN [2,1,6] capability

by abloke, 01 Feb, 2007 04:13 PM
16 17  
1111 * Added find_for and latest methods to ActivityLog
1212 
1313 0.3.1 - 01-02-2007
14 * Renamed find_for to find_with, the conditions are now stackable
14 * Renamed find_for to find_with, the conditions are now stackable
15 
16 0.3.2 - 01-02-2007
17 * Refactored the find_with method, and added IN [2,1,6] capability
16 17  
4343   # options are :culprit, :referenced, :activity_loggable, :limit
4444   def self.find_with(options={})
4545     limit = (options.delete(:limit) || 10)
46     conditions = []
47     conditions << self.send(:sanitize_sql, ["culprit_id = ?", options[:culprit]]) if options.keys.include? :culprit
48     conditions << self.send(:sanitize_sql, ["referenced_id = ?", options[:referenced]]) if options.keys.include? :referenced
49     conditions << self.send(:sanitize_sql, ["activity_loggable_id = ?", options[:activity_loggable]]) if options.keys.include? :activity_loggable
50     self.find(:all, :conditions => conditions.join(" AND "), :limit => limit)
46     conditions = build_sql_conditional_for(options)
47     self.find(:all, :conditions => conditions, :limit => limit)
5148   rescue
5249     raise "I couldn't run the find with the options you gave me, sorry"
5350   end
5451 
5552 private
53   def decide_conditional(option)
54     if option.value.is_a?Array
55       "IN"
56     else
57       "="
58     end
59   end
60   
61   def build_sql_conditional_for(options={})
62     conditions = []
63     options.each do |key, value|
64       conditional = decide_conditional(option)
65       conditions << self.send(:sanitize_sql, ["#{option.key.to_s}_id #{conditional} ?", option.value])
66     end
67     return conditions
68   end
69 
5670   def l_klass
5771     Object.const_get(self.activity_loggable_type.to_s)
5872   end