Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Created September 16, 2009 00:15
Show Gist options
  • Save jamesarosen/187769 to your computer and use it in GitHub Desktop.
Save jamesarosen/187769 to your computer and use it in GitHub Desktop.
class SomeModel < ActiveRecord::Base
include TemporalHelper
def my_temporal_attribute=(value)
write_attribute :my_temporal_attribute, parse_temporal(value)
end
end
module TemporalHelper
def parse_temporal(temporal)
case temporal
when NilClass, ''
nil
when Time, Date, DateTime
temporal
else
temporal = temporal.to_s
Chronic.parse(temporal) || Time.parse(temporal)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment