Posted By

rduv on 11/20/06


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


acts_as_recent


 / Published in: Ruby
 

  1. module Marmalade
  2. module ActiveRecord
  3. module Acts
  4. module Recent
  5.  
  6. def self.included( klass )
  7. super
  8. klass.extend(ClassMethods)
  9. end
  10.  
  11. module ClassMethods
  12.  
  13. def acts_as_recent( threshold = 5.days )
  14. class_inheritable_accessor :recent_threshold
  15. self.recent_threshold = threshold.is_a?( Fixnum ) ? threshold : 5.days
  16. send :include, Marmalade::ActiveRecord::Acts::Recent::InstanceMethods
  17. end
  18. end
  19.  
  20. module InstanceMethods
  21.  
  22. def fresh?
  23. created_at && created_at > Time.now.utc.ago(recent_threshold)
  24. end
  25.  
  26. def recently_updated?
  27. updated_at && updated_at > Time.now.utc.ago(recent_threshold)
  28. end
  29.  
  30. end
  31.  
  32. end
  33. end
  34. end
  35. end
  36.  
  37. ActiveRecord::Base.class_eval { include Marmalade::ActiveRecord::Acts::Recent }

Report this snippet  

You need to login to post a comment.