Skip to content

Instantly share code, notes, and snippets.

@BenEddy
Last active December 21, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenEddy/6348292 to your computer and use it in GitHub Desktop.
Save BenEddy/6348292 to your computer and use it in GitHub Desktop.
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