Created
June 12, 2018 17:44
-
-
Save kopylovvlad/ce03d4966fbba3686898be62feb91b2d 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 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