Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module DateObject
- # waits till it's imported AND THEN calls validate
- # needed because of the "strange" way Ruby parses through code
- def self.included(base) # base = class that this module is imported into
- base.validate :date_not_in_past
- end
- def date_in_past?
- Time.now > date
- end
- ################################
- def self.find_in_future
- date_objects = self.find(:all)
- date_objects.delete_if { |obj| obj.date_in_past? }
- date_objects
- end
- ################################
- protected
- def date_not_in_past
- if date_in_past?
- errors.add(:date, ": You can't schedule Festivals in the past!")
- end
- end
- end
- class Party < ActiveRecord::Base
- include DateObject
- end
- class Dance < ActiveRecord::Base
- include DateObject
- end
Add Comment
Please, Sign In to add comment