Skip to content

Instantly share code, notes, and snippets.

@samacs
Last active August 17, 2017 13:51
Show Gist options
  • Save samacs/79e75d59e68c3b1b20a47faedc2c798c to your computer and use it in GitHub Desktop.
Save samacs/79e75d59e68c3b1b20a47faedc2c798c to your computer and use it in GitHub Desktop.
A model concern to add search capabilities to a model.
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