Build up named scopes conditionally.
Latest commit 5f6df72 May 19, 2009 @ryanb renaming readme for rdoc
Failed to load latest commit information.
script fixing script/console so it properly loads the environment Jun 26, 2008
spec scope_builder can now be called on association proxy Aug 28, 2008
CHANGELOG releasing v0.1.2 Aug 28, 2008
LICENSE initial import Jun 26, 2008
Manifest releasing v0.1.2 Aug 28, 2008
README.rdoc renaming readme for rdoc May 18, 2009
Rakefile releasing v0.1.2 Aug 28, 2008


Scope Builder

Build up named scopes conditionally.


First specify it in your Rails config.

config.gem 'ryanb-scope-builder', :lib => 'scope_builder', :source => ''

And then install it.

rake gems:install

Rails 2.1 or later required.


This gem adds the scope_builder method to all Active Record models. A builder behaves exactly like any other named scope except that calling other named scopes on it will alter the builder itself rather than returning a new named scope.

builder = Product.scope_builder
builder.released.visible # call a couple named scopes to change builder if only_show_cheap_products? # build scopes conditionally

The scope_builder method can also take a block which will return the builder. This is useful when you are using the builder in a model search method.

# in product model
  scope_builder do |builder|
    builder.released.visible if options[:cheap]

The scope_builder method can also be called on an existing scope.

products = Product.released.visible
builder = products.scope_builder if only_show_cheap_products?


This project can be found on github at the following URL.

If you would like to contribute to this project, please fork the repository and send me a pull request.