Extract inline query to User scope & refactor

This commit is contained in:
Bertocq
2018-02-19 00:24:28 +01:00
parent 62d75af892
commit 9925e2b02f
2 changed files with 6 additions and 6 deletions

View File

@@ -2,12 +2,8 @@ class Admin::UsersController < Admin::BaseController
load_and_authorize_resource
def index
if params[:search]
s = params[:search]
@users = User.where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", "%#{s}%", "%#{s}%", "%#{s}%").page(params[:page])
else
@users = @users.page(params[:page])
end
@users = User.by_username_email_or_document_number(params[:search]) if params[:search]
@users = @users.page(params[:page])
respond_to do |format|
format.html
format.js

View File

@@ -64,6 +64,10 @@ class User < ActiveRecord::Base
scope :public_for_api, -> { all }
scope :by_comments, ->(query, topics_ids) { joins(:comments).where(query, topics_ids).uniq }
scope :by_authors, ->(author_ids) { where("users.id IN (?)", author_ids) }
scope :by_username_email_or_document_number, ->(search_string) do
string = "%#{search_string}%"
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string)
end
before_validation :clean_document_number