|
|
| 16 |
17 |
|
| 43 | 43 | # options are :culprit, :referenced, :activity_loggable, :limit |
| 44 | 44 | def self.find_with(options={}) |
| 45 | 45 | 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) |
| 51 | 48 | rescue |
| 52 | 49 | raise "I couldn't run the find with the options you gave me, sorry" |
| 53 | 50 | end |
| 54 | 51 | |
| 55 | 52 | 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 | |
| 56 | 70 | def l_klass |
| 57 | 71 | Object.const_get(self.activity_loggable_type.to_s) |
| 58 | 72 | end |