Method: AbstractResource.search
- Defined in:
- app/models/abstract_resource.rb
.search(lot, query, fields = "id") ⇒ Object
this generic search needs a bit more work but it is a ok first draft
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/abstract_resource.rb', line 19 def self.search(lot, query, fields="id") fields = fields.join(",") if fields.is_a? Array population = lot.pluck fields elements = query.split(' ') constraints = {and: [], or: [], not: []} elements.each do |element| case element.slice 0 when '+'; constraints[:and] += population.collect{ |k,v| k if v =~ /#{element[1..-1]}/i }.compact.flatten when '-'; constraints[:not] += population.collect{ |k,v| k if v =~ /#{element[1..-1]}/i }.compact.flatten when '*'; constraints[:or] += population.collect{ |k,v| k if b=="#{element[1..-1]}" }.compact.flatten else; constraints[:or] += population.collect{ |k,v| k if v =~ /#{element}/i }.compact.flatten end end population = constraints[:or].empty? ? population.collect{|r| r[0]} : constraints[:or] population = population & constraints[:and] if constraints[:and].any? population -= constraints[:not].uniq if constraints[:not].any? population = [] if constraints[:or].empty? and constraints[:and].empty? lot.where id: population end |