Acts as Toggled Date

For use in your models. Accepts one or more date columns as arguments and makes a method for each that allows the checking of whether or not the date is nil.

Got the idea from Jamis Buck here: http://weblog.jamisbuck.org/2005/12/14/two-tips-for-working-with-databases-in-rails

1 class Action < ActiveRecord::Base
2         acts_as_toggled_date :completed_at
3 end

...is the same as…

 1 class Action < ActiveRecord::Base
 2         def completed?
 3                 !completed_at.blank?
 4         end
 5         
 6         def        completed
 7                 self.update_attribute(:completed_at, Time.now.utc)
 8         end
 9         
10         def        uncompleted
11                 self.update_attribute(:completed_at, '')
12         end
13 end

...which means you can…

1 @action = Action.new
2 @action.completed? ? "Completed" : "Git 'r dun"

It works best with dates named like completed_at, published_at, etc.

Tags

Currently tagged with: activerecord, date
You need to Login to tag this item.