Created
March 28, 2014 12:12
-
-
Save paulca/9831315 to your computer and use it in GitHub Desktop.
This file contains 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
# class Event | |
# parse_dates :start_date, :end_date | |
# end | |
class ActiveRecord::Base | |
def self.parse_dates(*args) | |
args.flatten.each do |method_name| | |
class_eval do | |
define_method "#{method_name}=" do |date| | |
return super(date) unless date.kind_of?(String) | |
if self.respond_to?(:timezone) and self.timezone.present? | |
if date.match('UTC') | |
self.timezone = 'UTC' | |
end | |
Time.zone = self.timezone | |
Chronic.time_class = Time.zone | |
end | |
super((Chronic.parse(date) rescue nil) || (Time.zone.parse(date) rescue nil) || date) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment