Created
December 26, 2018 23:03
-
-
Save adamgavlak/77d687d69cf48a953193baf93df92d1e to your computer and use it in GitHub Desktop.
This file contains 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 Filterable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def filter(filtering_params) | |
results = self.where(nil) | |
filtering_params.each do |key, value| | |
results = results.public_send(key, value) if value.present? | |
end | |
results | |
end | |
end | |
end | |
#def index | |
#@products = Product.filter(params.slice(:status, :location, :starts_with)) | |
#end | |
#class Product < ActiveRecord::Base | |
#include Filterable | |
#scope :status, -> (status) { where status: status } | |
#scope :location, -> (location_id) { where location_id: location_id } | |
#scope :starts_with, -> (name) { where("name like ?", "#{name}%")} | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment