CARVIEW |
ryanb / scope-builder
- Source
- Commits
- Network (3)
- Issues (2)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
name | age | message | |
---|---|---|---|
![]() |
.gitignore | Thu Jun 26 09:27:18 -0700 2008 | rewriting builder to act as a simple delegator [ryanb] |
![]() |
CHANGELOG | Thu Aug 28 07:10:35 -0700 2008 | releasing v0.1.2 [ryanb] |
![]() |
LICENSE | Thu Jun 26 07:38:34 -0700 2008 | initial import [ryanb] |
![]() |
Manifest | Thu Aug 28 07:10:35 -0700 2008 | releasing v0.1.2 [ryanb] |
![]() |
README.rdoc | Mon May 18 13:53:35 -0700 2009 | renaming readme for rdoc [ryanb] |
![]() |
Rakefile | Thu Aug 28 07:10:35 -0700 2008 | releasing v0.1.2 [ryanb] |
![]() |
lib/ | Thu Aug 28 07:07:08 -0700 2008 | merging scope additions into model additions si... [ryanb] |
![]() |
scope-builder.gemspec | Thu Aug 28 07:10:35 -0700 2008 | releasing v0.1.2 [ryanb] |
![]() |
script/ | Thu Jun 26 09:41:41 -0700 2008 | fixing script/console so it properly loads the ... [ryanb] |
![]() |
spec/ | Thu Aug 28 07:04:59 -0700 2008 | scope_builder can now be called on association ... [ryanb] |
![]() |
tasks/ | Thu Jun 26 07:38:34 -0700 2008 | initial import [ryanb] |
Scope Builder
Build up named scopes conditionally.
Install
First specify it in your Rails config.
config.gem 'ryanb-scope-builder', :lib => 'scope_builder', :source => 'https://gems.github.com'
And then install it.
rake gems:install
Rails 2.1 or later required.
Usage
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 builder.cheap 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 def self.search(options) scope_builder do |builder| builder.released.visible builder.cheap if options[:cheap] end end
The scope_builder method can also be called on an existing scope.
products = Product.released.visible builder = products.scope_builder builder.cheap if only_show_cheap_products?
Development
This project can be found on github at the following URL.
github.com/ryanb/scope-builder/
If you would like to contribute to this project, please fork the repository and send me a pull request.