rxcfc / or_scopes

Easy chaining of ActiveRecord Scopes with ORs instead of ANDs

This URL has Read+Write access

rxcfc (author)
Sat Oct 03 16:21:30 -0700 2009
commit  f3d0c4f334806eed5efc990888c6e4932f6ce8d9
tree    85941a36c783c3e0803b0469bed6f77550474cc9
parent  5f372407654b0ab566b99c27f941a82623f71d21
name age message
file MIT-LICENSE Loading commit data...
file README.markdown
file Rakefile
file init.rb
directory lib/
README.markdown

OrScopes

OrScopes allows you easily to chain ActiveRecord Scopes with ORs instead of ANDs.

Examples

Consider the following setup:

class Person < ActiveRecord::Base
    named_scope :men,      :conditions => { :sex => 'M' }
    named_scope :women,    :conditions => { :sex => 'F' }
    named_scope :children, :conditions => "age < 18"
    named_scope :named, lambda{|name|
      { :conditions => { :name => name } }
    }
end

You call it with the names of a series of scopes as such:

Person.or_scopes(:women, :children)

This returns a scope like this:

Person.or_scopes(:women, :children).proxy_options
# => {:conditions=>"(`people`.`sex` = 'F') OR (age < 18)"}

You can also call it with an array of arrays when the scope requires parameters:

Person.or_scopes(:women, [:named, 'Sue']).proxy_options
# => {:conditions=>"(`people`.`sex` = 'F') OR (`people`.`name` = 'Sue')"}

You can also chain it as you would a normal scope:

Person.men.or_scopes(:children, [:named, 'Joe']).scope(:find)
# => {:conditions=>"((age < 18) OR (`people`.`name` = 'Joe')) AND (`people`.`sex` = 'M')"}

ToDo

  • Write tests

Copyright (c) 2009 Peter Wagenet, released under the MIT license