Skip to content

Instantly share code, notes, and snippets.

@johnhamelink
Created March 14, 2016 13:02
Show Gist options
  • Select an option

  • Save johnhamelink/c73e25a667e35904505d to your computer and use it in GitHub Desktop.

Select an option

Save johnhamelink/c73e25a667e35904505d to your computer and use it in GitHub Desktop.
def search_param_datetime(query: nil, attribute_key: nil, attribute_array: [])
fail 'Must have a valid query' if query.nil?
fail 'Must have a valid attribute key' if attribute_key.nil?
logger.debug "[Search param DateTime] Searching #{attribute_key} -> #{attribute_array.to_json}"
if attribute_array.nil? || attribute_array.empty?
logger.debug "[Search param DateTime] Backing out of filter - no values exist for #{attribute_key}"
return query
end
attribute_array.each do |attrs|
attrs.each do |attr|
attr = attr.to_a.flatten
logger.debug "[Search param DateTime] Filtering by #{attribute_key} #{attr.first} #{attr.last}"
case attr.first
when 'before'
query = query.where(query.model.arel_table[attribute_key.to_sym].lt(attr.last))
when 'after'
query = query.where(query.model.arel_table[attribute_key.to_sym].gt(attr.last))
when 'on'
query = query.where(attribute_key => (attr.last..attr.last.end_of_day))
end
end
end
@queries += 1
query
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment