smart_time
The smart_time gem allows your date/time ActiveRecord attributes to be parsed using the formats already defined by you or Rails in Time::DATE_FORMATS and Date::DATE_FORMATS. This allows you to capture date/time from text input fields using format strings that are already used by Rails to convert Dates and Times to Strings. If a :format option is passed smart_time will force the use of the supplied format, otherwise it will attempt to parse using the formats defined in Time::DATE_FORMATS (for Times) and Date::DATE_FORMATS (for Dates). If none of the formats match it will fail back to using the default Rails/Ruby parsing technique.
Getting Started
-
Install the smart_time gem
gem install smart_time
-
Use the
smart_time
macro in your modelsclass Event < ActiveRecord::Base # Calling smart_time with no parameters activates smart_time for all columns that have a klass of Date or Time (aka db types :date, :time, :datetime, and :timestamp). smart_time # Or you can name just the attributes you want to activate # smart_time :start_at, :end_at # You can also explicitly pass a format string # smart_time :start_at, :format => "%m/%d/%Y" end
-
Enjoy! If something breaks, complain to the author or (preferably) fork it, fix it, make sure all tests still pass and create a pull request.
Contributing to smart_time
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2011 Matthew Daubert. See LICENSE.txt for further details.