Guest User

Untitled

a guest
Jun 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. module DateObject
  2. # waits till it's imported AND THEN calls validate
  3. # needed because of the "strange" way Ruby parses through code
  4. def self.included(base) # base = class that this module is imported into
  5. base.validate :date_not_in_past
  6. end
  7.  
  8.  
  9. def date_in_past?
  10. Time.now > date
  11. end
  12.  
  13. ################################
  14. def self.find_in_future
  15. date_objects = self.find(:all)
  16. date_objects.delete_if { |obj| obj.date_in_past? }
  17. date_objects
  18. end
  19. ################################
  20.  
  21. protected
  22. def date_not_in_past
  23. if date_in_past?
  24. errors.add(:date, ": You can't schedule Festivals in the past!")
  25. end
  26. end
  27. end
  28.  
  29. class Party < ActiveRecord::Base
  30. include DateObject
  31. end
  32.  
  33. class Dance < ActiveRecord::Base
  34. include DateObject
  35. end
Add Comment
Please, Sign In to add comment