Skip to content

Instantly share code, notes, and snippets.

@paulca
Created March 28, 2014 12:12
Show Gist options
  • Save paulca/9831315 to your computer and use it in GitHub Desktop.
Save paulca/9831315 to your computer and use it in GitHub Desktop.
# 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