Skip to content

Instantly share code, notes, and snippets.

@nfabian13
Forked from justinweiss/filterable.rb
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save nfabian13/1142b8712bf0f7ab7abd to your computer and use it in GitHub Desktop.

Select an option

Save nfabian13/1142b8712bf0f7ab7abd to your computer and use it in GitHub Desktop.

Revisions

  1. @justinweiss justinweiss revised this gist Feb 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion filterable.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ module ClassMethods
    # 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
    results = self.where(nil) # create an anonymous scope
    filtering_params.each do |key, value|
    results = results.public_send(key, value) if value.present?
    end
  2. @justinweiss justinweiss revised this gist Feb 18, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions filterable.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,15 @@
    # 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 the same name as the keys in <tt>filtering_params</tt>
    # with their associated values. 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
    filtering_params.each do |key, value|
  3. @justinweiss justinweiss created this gist Feb 18, 2014.
    13 changes: 13 additions & 0 deletions filterable.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    module Filterable
    extend ActiveSupport::Concern

    module ClassMethods
    def filter(filtering_params)
    results = self
    filtering_params.each do |key, value|
    results = results.public_send(key, value) if value.present?
    end
    results
    end
    end
    end