diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e83ecc11..c86b173e 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -2,13 +2,10 @@ class Admin::UsersController < Admin::BaseController inherit_resources def index - @users = User.order('nick ASC') + @users = User.natural_order # if somebody uses the search field: - unless params[:user_name].blank? - @users = @users.where("first_name LIKE :user_name OR last_name LIKE :user_name OR nick LIKE :user_name", - user_name: "%#{params[:user_name]}%") - end + @users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? @users = @users.page(params[:page]).per(@per_page) end diff --git a/app/controllers/foodcoop/users_controller.rb b/app/controllers/foodcoop/users_controller.rb index f73147bc..31539dce 100644 --- a/app/controllers/foodcoop/users_controller.rb +++ b/app/controllers/foodcoop/users_controller.rb @@ -1,19 +1,16 @@ class Foodcoop::UsersController < ApplicationController def index - @users = User.order('nick ASC') + @users = User.natural_order # if somebody uses the search field: - unless params[:user_name].blank? - @users = @users.where("first_name LIKE :user_name OR last_name LIKE :user_name OR nick LIKE :user_name", - user_name: "%#{params[:user_name]}%") - end + @users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? if params[:ordergroup_name] @users = @users.joins(:groups).where("groups.type = 'Ordergroup' AND groups.name LIKE ?", "%#{params[:ordergroup_name]}%") end - @users = @users.page(params[:page]).per(@per_page).order('users.nick ASC') + @users = @users.page(params[:page]).per(@per_page) respond_to do |format| format.html # index.html.haml diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 916b33f7..b6a7bb98 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,7 +2,7 @@ class UsersController < ApplicationController # Currently used to display users nick and ids for autocomplete def index - @users = User.where("nick LIKE ?", "%#{params[:q]}%") + @users = User.natural_search(params[:q]) respond_to do |format| format.json { render :json => @users.map { |u| u.token_attributes } } end diff --git a/app/models/user.rb b/app/models/user.rb index cf5829d5..29c070c0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,6 +56,22 @@ class User < ActiveRecord::Base self.settings.merge!(key, value) end end + + # sorted by display name + def self.natural_order + # would be sensible to match ApplicationController#show_user + if FoodsoftConfig[:use_nick] + order('nick ASC') + else + order('first_name ASC, last_name ASC') + end + end + + # search by (nick)name + def self.natural_search(q) + # we always use both nick and name, to make sure a user is found + where("CONCAT(first_name, CONCAT(' ', last_name)) LIKE :q OR nick LIKE :q", q: "%#{q}%") + end def locale settings.profile['language']