Skip to content

Instantly share code, notes, and snippets.

@slopeofhope81
Forked from justinweiss/filterable.rb
Created April 2, 2020 11:19
Show Gist options
  • Save slopeofhope81/ba20e4a110208def4ca888f71b9115ee to your computer and use it in GitHub Desktop.
Save slopeofhope81/ba20e4a110208def4ca888f71b9115ee to your computer and use it in GitHub Desktop.
Filterable
# Call scopes directly from your URL params:
#
# @products = Product.filter(params.slice(:status, :location, :starts_with))
module Filterable
extend ActiveSupport::Concern
module ClassMethods
# Call the class methods with names based on the keys in <tt>filtering_params</tt>
# with their associated values. For example, "{ status: 'delayed' }" would call
# `filter_by_status('delayed')`. Most useful for calling named scopes from
# URL params. Make sure you don't pass stuff directly from the web without
# whitelisting only the params you care about first!
def filter(filtering_params)
results = self.where(nil) # create an anonymous scope
filtering_params.each do |key, value|
results = results.public_send("filter_by_#{key}", value) if value.present?
end
results
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment