FlexDate integration for Rails.
Flexible dates for Rails/ActiveRecord, as a gem. Originally written by Alex Reisner, packaged as a gem. github.com/alexreisner/flex_date
Use with Bundler
Add the following to your Gemfile.
gem 'flex_date_rails', '0.2.2'
And create an intializer in config/intializers/flex_date.rb
Integration with Rails/ActiveRecord
The MultipartDate module facilitates integration of FlexDate with ActiveRecord models. It requires that you store each date in three separate columns, each with a _y, _m, or _d suffix. So, if you have a Person model that stores birthdays, the columns required would be (all integers):
birthday_y birthday_m birthday_d
Then, simply put this in your Person model (you can list multiple attributes if needed):
and you'll be able to do things like this:
# Initialize a new Person object (don't know year they were born). p = Person.new(:birthday_m => 8, :birthday_d => 23) # See if their birthday is set. p.birthday? => true # Get their birthday as a FlexDate object. b = p.birthday => #<FlexDate:0xb78b542c ...> # Display their birthday nicely. p.birthday.to_s(:long) => "August 23" # Add the year when you find out. p.birthday_y = 1964 p.birthday.to_s(:long) => "August 23, 1964"
MultipartDate::multipart_date should also generate a setter method.
Released under the MIT license.