make search work without nickname usage

This commit is contained in:
wvengen 2013-09-20 23:18:06 +02:00
parent 17c1ee803f
commit 6d3db7636a
4 changed files with 22 additions and 12 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -57,6 +57,22 @@ class User < ActiveRecord::Base
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']
end