0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Extends ActiveRecord to let you store time durations like '1.month' or '10.years'. It does it by providing a serializer and validator for ActiveSupport::Duration objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

~> 3.2
 Project Readme

HasDuration

Do you use rails time convenience methods? Those things are great, they let you do stuff like 1.day.from_now or Time.now - 10.years. The 10.years part is an {ActiveSupport::Duration} object.

This plugin extends ActiveRecord so you can conveniently store durations like: how often to contact a given customer, or how long can your users stay in the bathtub without their fingertips wrinkling.

Installation: Add this to your Gemfile

gem 'has_duration'

Given a table defined as:

create_table :users do |t|
  t.string :contact_every, null: false
  t.string :bathtub_tolerance
end

You can have a model like this

class User < ActiveRecord::Base
  has_duration :contact_every
  # Has duration does not validate presence, you have to do that yourself.
  validates :contact_every, presence: true
  has_duration :bathtub_tolerance
end

user = User.create!(contact_every: 1.year, bathtub_tolerance: 30.minutes)
puts 'that must be a record' if customer.bathtub_tolerance > 3.hours
puts "If contacted today, next contact would be made on #{user.contact_every.from_now}"

This project uses MIT-LICENSE.