jferris / sconnect

Combine named scopes without "and"

This URL has Read+Write access

jferris (author)
Sun Feb 22 18:31:14 -0800 2009
commit  3e11308e7b69e8324768bd1553454decaab12225
tree    b5979815841f40c2ea0fa9243aab4ee0348bf7ea
parent  d9d2ed6f68221fc8a03014fd853ce8451e073ac9
name age message
file .gitignore Loading commit data...
file README.rdoc
file Rakefile
directory lib/
file sconnect.gemspec
directory spec/
README.rdoc

Sconnect

Sconnect is an extension to ActiveRecord’s named_scopes that allows you to combine scopes in more interesting and useful ways.

Download

Github: github.com/jferris/sconnect/tree/master

Examples

Given the following model:

  class Post < ActiveRecord::Base
    named_scope :published, :conditions => { :published => true }
    named_scope :titled, :conditions => "title IS NOT NULL"
    named_scope :from_today, lambda {
      { :conditions => ['created_at >= ?', 1.day.ago] }
    }
  end

ActiveRecord provides scope chains:

  # All published posts with titles
  Post.published.titled

  # All published posts from today
  Post.published.from_today

Sconnect extends these scopes:

  # All posts that are either published or titled
  Post.published.or.titled

  # All posts that are published but not created today
  Post.published.not.from_today

  # All posts that are either published or untitled
  Post.published.or.not.titled

  # All posts from today that are either published or titled
  Post.published.or.titled.from_today

Author

Sconnect was written by Joe Ferris.