Module: InstanceUserSearchConcern::ClassMethods

Defined in:
app/models/concerns/instance_user_search_concern.rb

Instance Method Summary collapse

Instance Method Details

#search(keyword) ⇒ Array<User>

Search and filter users by their names or emails.

Parameters:

  • keyword (String)

    The keywords for filtering users.

Returns:

  • (Array<User>)

    The users which match the keyword. All users will be returned if keyword is blank.


11
12
13
14
15
16
17
18
# File 'app/models/concerns/instance_user_search_concern.rb', line 11

def search(keyword)
  return all if keyword.blank?

  condition = "%#{keyword}%"
  joining { user.emails.outer }.
    where.has { (sql('users.name') =~ condition) | (sql('user_emails.email') =~ condition) }.
    group('instance_users.id')
end