avatar

16

Renamed find_for to find_with, the conditions are now stackable by abloke, 01 Feb, 2007 03:39 PM
Diff this changeset:
activity_log.rb
class ActivityLog < ActiveRecord::Base
  belongs_to :activity_loggable, :polymorphic => true
  belongs_to :referenced, :polymorphic => true
  belongs_to :culprit, :polymorphic => true

  alias the_culprit culprit
  def culprit(options="nil")
    options.to_sym == :value ? 
      ((defined? l_klass.models[:culprit] && defined? l_klass.models[:culprit][:method]) ?
        the_culprit.send(l_klass.models[:culprit][:method].to_s) :
        "You must set a :method in the options to use this") : 
      the_culprit
  rescue
    return "anonymous"
  end
  
  alias the_referenced referenced
  def referenced(options="nil")
    options.to_sym == :value ? 
      ((defined? l_klass.models[:referenced] && defined? l_klass.models[:referenced][:method]) ?
        the_referenced.send(l_klass.models[:referenced][:method].to_s) :
        "You must set a :method in the options to use this") :
        the_referenced
  rescue
    return "anonymous"  
  end

  alias the_activity_loggable activity_loggable
  def activity_loggable(options="nil")
    options.to_sym == :value ? 
      ((defined? l_klass.models[:activity_loggable] && defined? l_klass.models[:activity_loggable][:method]) ?
        the_activity_loggable.send(l_klass.models[:activity_loggable][:method].to_s) :
        "You must set a :method in the options to use this") :
        the_activity_loggable
  rescue
    return "anonymous"  
  end
  
  def self.latest(limit=5)
    self.find(:all, :limit => limit)
  end
  
  # options are :culprit, :referenced, :activity_loggable, :limit
  def self.find_with(options={})
    limit = (options.delete(:limit) || 10)
    conditions = []
    conditions << self.send(:sanitize_sql, ["culprit_id = ?", options[:culprit]]) if options.keys.include? :culprit
    conditions << self.send(:sanitize_sql, ["referenced_id = ?", options[:referenced]]) if options.keys.include? :referenced
    conditions << self.send(:sanitize_sql, ["activity_loggable_id = ?", options[:activity_loggable]]) if options.keys.include? :activity_loggable
    self.find(:all, :conditions => conditions.join(" AND "), :limit => limit)
  rescue
    raise "I couldn't run the find with the options you gave me, sorry"
  end

private
  def l_klass
    Object.const_get(self.activity_loggable_type.to_s)
  end
  
  def r_klass
    Object.const_get(self.referenced_type.to_s)
  end
  
  def c_klass
    Object.const_get(self.culprit_type.to_s)
  end
end

Check out the code: svn co trunk/lib/activity_log.rb