Skip to content

Instantly share code, notes, and snippets.

@jacobsimeon
Last active December 19, 2015 18:39
Show Gist options
  • Save jacobsimeon/6000668 to your computer and use it in GitHub Desktop.
Save jacobsimeon/6000668 to your computer and use it in GitHub Desktop.
Search for (with exclusions)
def search_for(resources, criteria={}, exclusions=nil)
if(criteria[:from])
date = DateTime.parse(criteria.delete(:from))
criteria[:time_utc.gte] = date
end
if criteria[:to]
date = DateTime.parse(criteria.delete(:to))
criteria[:time_utc.lte] = date+1 # lame but we wanted to make the demo
end
if exclusions
exclusions = exclusions.inject({}) { |e, x| e[x] = 0 }
resources.where(locomotive_id: self.locomotive_id, exclusions)
else
resources.where(locomotive_id: self.locomotive_id)
end.tap do |query|
query.merge!(resources.where(criteria)) unless criteria.empty?
end.order_by(:time_utc.asc)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment