Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Created June 12, 2018 17:44
Show Gist options
  • Save kopylovvlad/ce03d4966fbba3686898be62feb91b2d to your computer and use it in GitHub Desktop.
Save kopylovvlad/ce03d4966fbba3686898be62feb91b2d to your computer and use it in GitHub Desktop.
module UserSearcher
def self.call(params = {})
scope = ::User.active
# by id
scope = scope.find(params['id']).to_a if params['id'].present?
# simple fields
%w[first_name last_name email city].each do |item|
next unless params[item].present?
scope = scope.where(item => params[item])
end
if params['gender'].present? and %w[male female].include?(params['gender'])
scope = scope.where(gender: params['gender'])
end
# complex fields
if params['birthdate_before'].present?
scope = scope.where(
birthdate: { :$lte => Date.parse(params['birthdate_before']) }
)
end
if params['birthdate_after'].present?
scope = scope.where(
birthdate: { :$gte => Date.parse(params['birthdate_after']) }
)
end
scope
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment