Created
April 28, 2017 17:30
-
-
Save katienreed/a44182badabf6be1c30e06b47433d754 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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