Created
September 7, 2012 22:36
-
-
Save sfaxon/3670345 to your computer and use it in GitHub Desktop.
dynamic assignment of dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Post.class_eval do | |
columns.select { |c| c.type == :datetime }.map { |c| c.name }.each do |date_time_field| | |
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | |
def #{date_time_field}_date_picker | |
read_attribute(:#{date_time_field}).to_s(:date_picker) | |
end | |
def #{date_time_field}_time_picker | |
read_attribute(:#{date_time_field}).to_s(:time_picker) | |
end | |
def #{date_time_field}_date_picker=(date) | |
# this needs to deal with timezone, and not rewrite the existing time fields | |
write_attribute(:#{date_time_field}, DateTime.strptime(date, Time::DATE_FORMATS[:date_picker])) | |
end | |
RUBY | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment