Extract inline query to User scope & refactor
This commit is contained in:
@@ -2,12 +2,8 @@ class Admin::UsersController < Admin::BaseController
|
|||||||
load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
|
|
||||||
def index
|
def index
|
||||||
if params[:search]
|
@users = User.by_username_email_or_document_number(params[:search]) 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])
|
@users = @users.page(params[:page])
|
||||||
end
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
format.js
|
format.js
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ class User < ActiveRecord::Base
|
|||||||
scope :public_for_api, -> { all }
|
scope :public_for_api, -> { all }
|
||||||
scope :by_comments, ->(query, topics_ids) { joins(:comments).where(query, topics_ids).uniq }
|
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_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
|
before_validation :clean_document_number
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user