class Admins::SchoolQuery < ApplicationQuery include CustomSortable attr_reader :params sort_columns :users_count, :created_at, default_by: :created_at, default_direction: :desc def initialize(params) @params = params end def call schools = School.all keyword = strip_param(:keyword) schools = schools.where('schools.name LIKE ?', "%#{keyword}%") if keyword schools = schools.joins(:user_extensions).group(:id) schools = schools.select('schools.*, COUNT(*) AS users_count') custom_sort schools, params[:sort_by], params[:sort_direction] end end