Skip to content

Instantly share code, notes, and snippets.

@keernel
Last active June 5, 2016 14:25
Show Gist options
  • Select an option

  • Save keernel/ed9d31b4d0500d57abf01acf9172023f to your computer and use it in GitHub Desktop.

Select an option

Save keernel/ed9d31b4d0500d57abf01acf9172023f to your computer and use it in GitHub Desktop.
Simple reusable search module
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
class Product
include Filterable
...
end
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