Created
May 10, 2016 21:39
-
-
Save everblut/cfdb10661b3e824da4920b4f5e16c19e 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
# A naive approach to implement a picky search | |
module SelectiveSearchable | |
def searchable_on(*attrs) | |
search_attrs = attrs.map { |attr| "#{attr}_cont".to_sym } | |
define_singleton_method(:picky_search) do |query, page = 0, limit = 10| | |
if query.try(:empty?) | |
sentence = Proc.new { self.all } | |
else | |
q = search_attrs.reduce({}) {|dict, attr| dict.merge({attr => query}) } | |
sentence = Proc.new { self.ransack(q).result(distinct: true) } | |
end | |
sentence.call.page(page).per(limit) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment