Created
September 16, 2009 00:15
-
-
Save jamesarosen/187769 to your computer and use it in GitHub Desktop.
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
class SomeModel < ActiveRecord::Base | |
include TemporalHelper | |
def my_temporal_attribute=(value) | |
write_attribute :my_temporal_attribute, parse_temporal(value) | |
end | |
end |
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
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