No commit activity in last 3 years
No release in over 3 years
adds daterange to AR models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

 Project Readme

ActsAsDaterange

This is home to the acts_as_daterange GEM.

It's useful for all models that have (optional) start date and end date (e.g. coupons, campaigns, orders, events, etc.)

Just add 'acts_as_daterange' to your ActiveRecord models and get useful validations, methods and scopes.

Installation

Add this line to your application's Gemfile:

gem 'acts_as_daterange'

And then execute:

$ bundle

Or install it yourself as:

$ gem install acts_as_daterange

Usage

Given an AR model, for example Coupon, with start_date and end_date fields:

class Coupon < ActiveRecord::Base
  attr_accessible :start_date, :end_date
  acts_as_daterange
end

> @coupon = Coupon.create start_date: 3.hours.ago, end_date: 3.hours.from_now

methods:

> @coupon.active_now?
> @coupon.started?
> @coupon.ended?

scopes:

> Coupon.active_now
> Coupon.inactive_now
> Coupon.active_at Date.today
> Coupon.inactive_at 5.hours.ago

validations:

> @coupon = Coupon.create start_date: 3.hours.from_now, end_date: 3.hours.ago
> @coupon.errors[:end_date]
=> must be after start_date

Notice that:

  • start_date=nil means started? == true always
  • and end_date=nil means that ended? == false always
> Coupon.new.started?
=> true
> Coupon.new.ended?
=> false

You can also override the field names:

acts_as_daterange :started_at, :ended_at
#  or
acts_as_daterange start_date: :started_at, end_date: :ended_at

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request