Skip to content

Instantly share code, notes, and snippets.

@everblut
Created May 10, 2016 21:39
Show Gist options
  • Save everblut/cfdb10661b3e824da4920b4f5e16c19e to your computer and use it in GitHub Desktop.
Save everblut/cfdb10661b3e824da4920b4f5e16c19e to your computer and use it in GitHub Desktop.
# 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