Skip to content

Instantly share code, notes, and snippets.

@sfaxon
Created September 7, 2012 22:36
Show Gist options
  • Save sfaxon/3670345 to your computer and use it in GitHub Desktop.
Save sfaxon/3670345 to your computer and use it in GitHub Desktop.
dynamic assignment of dates
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