Last active
June 5, 2016 14:25
-
-
Save keernel/ed9d31b4d0500d57abf01acf9172023f to your computer and use it in GitHub Desktop.
Simple reusable search module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Searchable | |
| extend ActiveSupport::Concern | |
| module ClassMethods | |
| def filter(filter_params) | |
| filter_params_keys = filter_params.symbolize_keys | |
| results = self.where(nil) | |
| filter_params_keys.each do |key, value| | |
| results = results.where(self.arel_table[key].matches("%#{value}%")) | |
| end | |
| return results | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Product | |
| include Filterable | |
| ... | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def index | |
| @products = Product.filter(params.slice(:name, :email)) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment