Last active
August 17, 2017 13:51
-
-
Save samacs/79e75d59e68c3b1b20a47faedc2c798c to your computer and use it in GitHub Desktop.
A model concern to add search capabilities to a model.
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 Searchable | |
extend ActiveSupport::Concern | |
included do | |
scope :filtered, lambda { |values| | |
current = all | |
filters.each do |key, method| | |
if values.try(:key?, key) | |
current = current.send(method, values[key]) if | |
curent.respond_to?(method) | |
end | |
end | |
method | |
} | |
end | |
module Class | |
def add_filter(filter_key, method_name, callable = nil) | |
scope(method_name, callable) if callable | |
filters[filter_key] = method_name | |
end | |
def filters | |
@filters ||= {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment