SimpleScope
Allows you to define magic scopes on an ActiveRecord model without having to provide where conditions. The conditions are derived from the name of the scope. Multiple conditions can be defined via the name by separating them with and much like the magic find_by_attribute1_and_attribute2 methods.
Installation
Add this line to your application's Gemfile:
gem 'simple_scope'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_scope
Usage
Simply define a scope:
class Color < ActiveRecord::Base
scope :color_red
end
Now, calling Color.color_red will build the query: where 'color' => 'red'. Multiple conditions can be separated by _and_:
class Color < ActiveRecord::Base
scope :color_red_and_status_enabled
end
Calling Color.color_red_and_status_enabled will build the query: where 'color' => 'red', 'status' => 'enabled'.
Contributing
- Fork it ( https://github.com/[my-github-username]/simple_scope/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request