Last active
December 21, 2015 18:38
-
-
Save BenEddy/6348292 to your computer and use it in GitHub Desktop.
This file contains 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
class UserSearch | |
attr_reader :name, :email | |
def initialize(options = {}) | |
@name = options[:name] | |
@email = options[:email] | |
end | |
def results | |
scope = User.scoped | |
if scope_by_name? | |
scope = scope.where("lower(first_name) LIKE lower(:name) | |
OR lower(last_name) LIKE lower(:name)", {name: name + "%"}) | |
end | |
if scope_by_email? | |
scope = scope.where("lower(email) LIKE lower(?)", email + "%") | |
end | |
scope | |
end | |
private | |
def scope_by_name? | |
name.present? | |
end | |
def scope_by_email? | |
email.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment