Skip to content

Instantly share code, notes, and snippets.

@katienreed
Created April 28, 2017 17:30
Show Gist options
  • Save katienreed/a44182badabf6be1c30e06b47433d754 to your computer and use it in GitHub Desktop.
Save katienreed/a44182badabf6be1c30e06b47433d754 to your computer and use it in GitHub Desktop.
module Queries
class EventsFilter
FILTER_METHOD_MAP = {
"type" => :filter_type,
}
def self.filtered_relation(filters, events)
relation = events.includes(:mentions, :related)
return relation unless filters.is_a?(Hash)
filters.inject(relation) do |r, (filter, value)|
filter_method = FILTER_METHOD_MAP[filter.to_s]
next r unless filter_method
send(filter_method, r, value)
end
end
class << self
private
def filter_type(relation, type)
type = "Events::" + type.classify
if Event::EVENT_TYPES.include?(type)
relation.where("events.type ILIKE ?", type)
else
relation.none
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment