Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

nubis/has_duration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rails plugin to validate and serialize ActiveSupport::Duration objects in your models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages