Skip to content

Instantly share code, notes, and snippets.

@msroot
Last active April 27, 2020 17:16
Show Gist options
  • Save msroot/eaed830759e767436ffd32f43277f46d to your computer and use it in GitHub Desktop.
Save msroot/eaed830759e767436ffd32f43277f46d to your computer and use it in GitHub Desktop.
require 'search_object'
require 'search_object/plugin/graphql'
module Resolvers
class BaseSearchResolver
include ::SearchObject.module(:graphql)
scope { [] }
FILTER_BY_DATE_INPUT = begin
Class.new(Types::BaseInputObject) do
graphql_name "FilterByDateInput"
argument :from, GraphQL::Types::ISO8601DateTime, required: true
argument :to, GraphQL::Types::ISO8601DateTime, required: true
end
end
def self.add_filter_by_datetime_for(klass, exclude: [])
klass.datetime_attributes.map do |attribute|
next if exclude.includes?(attribute)
option("filter_by_#{attribute}".to_sym, type: FILTER_BY_DATE_INPUT) { |scope, value|
if value
value = value.to_h
scope.where(attribute => value[:from]..value[:to])
end
}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment