Skip to content

Simple ActiveRecord extension to add a #filter method that takes a hash and uses it to select scope(s)

License

Notifications You must be signed in to change notification settings

neophiliac/filterable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Filterable

Build Status

Based on Justin Weiss' blog post Search and Filter Rails Models Without Bloating Your Controller, Filterable enables you to replace MyModel.find.long.string.of.conditions with MyModel.filter(list_of_scopes).

Installation

Add this line to your application's Gemfile:

gem 'filterable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install filterable

Usage

  1. In your model, consider the attributes you want to match.
  2. Create scopes that match appropriately.
scope :name, ->(p) { where(name: p) }
scope :category, ->(p) { where(category: p) }
  1. In your view, make sure these params will be sent on submit.
  2. In your controller, use ModelName.filter(params.slice(:name, :category))

TODO

  • Add methods to create simple scopes
  • Keep a list of auto-created scopes to use as the default list if no params are passed to the #filter method
  • Add a method to refer to existing scopes, (add them to the default list)

Similar Gems

Contributing

  1. Fork it ( http://github.com/neophiliac/filterable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Simple ActiveRecord extension to add a #filter method that takes a hash and uses it to select scope(s)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages