Project

scopy

0.01
No commit activity in last 3 years
No release in over 3 years
Rails ActiveRecord scopes for id, created_at, and name
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.4
~> 12.0
~> 1.3

Runtime

 Project Readme

Scopy

Gem Version Build Status

Scopy provides common ActiveRecord scopes as ActiveSupport model concerns.

Common scopes for the following attributes are provided:

  • created_at
  • name

Install

Add this line to your Gemfile:

gem 'scopy'

scopy works with Rails 4.0 and later.

Version 0.4 works with Rails 3.2 and later.

Usage

Include the modules you would like to use in your models:

class Dog < ActiveRecord::Base
  include Scopy::CreatedAtScopes
  include Scopy::NameScopes

The following examples assume you have a model named Dog:

Scopy::CreatedAtScopes
Dog.newest
# => most recently created dogs are first

Dog.oldest
# => most recently created dogs are last

Dog.created_since(1.month.ago)
# => only dogs created since one month ago

Dog.created_before(1.week.ago)
# => only dogs created before 1 week ago

Dog.created_between(2.weeks.ago, 1.week.ago)
# => only dogs created between 2 weeks ago & 1 week ago

Dog.created_on_day(1.day.ago)
# => dogs created yesterday

Dog.created_in_week(Date.parse("31/10/2013"))
# => dogs created in the week Sunday-Saturday around October 31, 2013

Dog.created_in_month(Time.new(2013, 1))
# => dogs created in January 2013

Dog.created_in_year(Time.new(2013))
# => dogs created in 2013
Scopy::NameScopes
Dog.name_like('snOOp')
# => dogs with names containing 'snoop' (case insensitive)

Dog.name_like('Snoop', case_sensitive: true)
# => dogs with names containing 'Snoop' (case sensitive)

Dog.name_starts_with('snOOp')
# => dogs with names starting with 'snoop' (case insensitive)

Dog.name_starts_with('Snoop', case_sensitive: true)
# => dogs with names starting with 'Snoop' (case sensitive)

Upgrading

Version 2.0
  • .excluding_ids and .excluding_id scopes were removed in version 2.0. Replace those calls with .excluding.
Version 3.0
  • Scopy::IdScopes was removed in version 3. Instead, use:
where.not(id: id)
where.not(id: ids)
where.not(id: model)
where.not(id: models)