Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Store time periods in a single column, with reasonable simple_form support

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 config
Octocat-spinner-32 doc
Octocat-spinner-32 lib
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.textile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 time_period.gemspec
README.textile

time_period gem

Mission Objective

Store time periods as “3 days” or “7 weeks” in a database.

We use one simple varchar column in which we store the value as above in the format “<integer> <day|week|month|year>”.

Usage

First add the time_period gem to your Gemfile. Then run the bundle install command.

In the migration, define a string field

  ...
  t.string :duration_period
  ...

and migrate your database rake db:migrate

In the model, declare the field as ‘being a time period’:

  class MyModel < ActiveRecord::Base
    ...
    time_period :duration_period, default: 7.days
    ...
  end

then, you get the following methods:

  • duration_period – raw reader of the saved string.
  • duration_period_unit – getter for the unit, i.e., “day”, “week”, …
  • duration_period_unit= – setter for the unit, i.e., “day”, “week”, …
  • duration_period_number – getter for the number, i.e., 1, 2, 3
  • duration_period_number= – setter for the number, i.e., 1, 2, 3
  • duration_period= – setter for the complete period, accepts either strings as above or ActiveSupport::Duration objects as 3.weeks
  • duration_period_value – reads the value as an ActiveSupport::Duration object
  >> m = MyModel.new
  >> m.duration_period_unit = "day"
  "day"
  >> m.duration_period
  => "0 day"
  >> m.duration_period_number = 7
  => 7
  >> m.duration_period
  => "7 day"
  >> m.duration_period_value
  => 7 days
  >> m.duration_period = '3 week'
  => "3 week"
  >> m.duration_period_value
  => 21 days

For simple_form forms, we offer an input:

  <%= simple_form_for @my_thing do |f| %>
    ...
    <%= f.input :duration_period,  as: :time_period %>
    ...
  <% end %>

The result then looks a little bit like this:

Disclaimer

This is pretty much work-in-progress-without-reasonable-documentation. So if you use it, don’t complain ;)

Nevertheless, bug reports and improvements are highly appreciated:

Contributing to time_period

  • 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
  • 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.
  • Feel free to send a pull request if you think others (me, for example) would like to have your change incorporated into future versions of time_period.

License

Copyright © 2011-2012 Peter Horn, metaminded UG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.