Skip to content

gzigzigzeo/arel_date_scopes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arel Date Scopes

This gem is useful when you want to navigate through your records by years, months or days - for example, at news archive page.

  • AREL 2 date functions (DATE(), YEAR(), DAYOFMONTH() for MySQL, CAST(STRFTIME(…)) for SQLite)

  • AR 3 scopes.

Installation

Put following line in your Gemfile:

gem 'arel_date_scopes'

Pure AREL example:

Let the users table have created_at field.

users = Table[:users]
users.where(users[:created_at].year.eq(2009))
users.where(users[:created_at].month.gt(2))
users.where(users[:created_at].dayofmonth.in(1..20))

AR example:

Let the news table have created_at field and contain news for two years: 2008 and 2009.

class News < ActiveRecord::Base
  date_scopes_for :created_at # Creates scopes for created_at field.
end

The News model gets the following scopes:

  • created_at_year_eq

  • created_at_month_eq

  • created_at_day_eq

  • created_at_years

  • created_at_months

  • created_at_days

  • ascend_by_created_at

  • descend_by_created_at

Usage example:

News.created_at_year_eq(2009).all
News.created_at_year_eq(2009).created_at_month_eq(5).all
News.descend_by_created_at.created_at_years.all.first['created_at_year']     # 2009
News.ascend_by_created_at.created_at_years.all_column                        # [2008, 2009]

TODO:

  • Create scopes through method_missing like searchlogic.

About

MySQL date functions for AREL 2 + AR 3 date scopes

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages