2015-03-16 15:14:33 -04:00
|
|
|
class DirectoryItemsController < ApplicationController
|
|
|
|
PAGE_SIZE = 50
|
|
|
|
|
|
|
|
def index
|
2015-03-19 11:48:16 -04:00
|
|
|
period = params.require(:period)
|
|
|
|
period_type = DirectoryItem.period_types[period.to_sym]
|
2015-03-16 15:14:33 -04:00
|
|
|
raise Discourse::InvalidAccess.new(:period_type) unless period_type
|
|
|
|
|
|
|
|
result = DirectoryItem.where(period_type: period_type).includes(:user)
|
|
|
|
|
2015-03-19 15:31:29 -04:00
|
|
|
if current_user.present?
|
|
|
|
result = result.order("CASE WHEN users.id = #{current_user.id.to_i} THEN 0 ELSE 1 END")
|
|
|
|
end
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
order = params[:order] || DirectoryItem.headings.first
|
|
|
|
if DirectoryItem.headings.include?(order.to_sym)
|
|
|
|
dir = params[:asc] ? 'ASC' : 'DESC'
|
|
|
|
result = result.order("directory_items.#{order} #{dir}")
|
|
|
|
end
|
|
|
|
|
|
|
|
if period_type == DirectoryItem.period_types[:all]
|
|
|
|
result = result.includes(:user_stat)
|
|
|
|
end
|
|
|
|
page = params[:page].to_i
|
2015-03-19 15:31:29 -04:00
|
|
|
|
2015-03-19 18:07:31 -04:00
|
|
|
user_ids = nil
|
|
|
|
if params[:name].present?
|
|
|
|
user_ids = UserSearch.new(params[:name]).search.pluck(:id)
|
|
|
|
if user_ids.present?
|
|
|
|
# Add the current user if we have at least one other match
|
|
|
|
if current_user && result.dup.where(user_id: user_ids).count > 0
|
|
|
|
user_ids << current_user.id
|
|
|
|
end
|
|
|
|
result = result.where(user_id: user_ids)
|
|
|
|
else
|
|
|
|
result = result.where('false')
|
|
|
|
end
|
|
|
|
end
|
2015-03-19 15:31:29 -04:00
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
result = result.order('users.username')
|
|
|
|
result_count = result.dup.count
|
2015-03-19 18:07:31 -04:00
|
|
|
result = result.limit(PAGE_SIZE).offset(PAGE_SIZE * page).to_a
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2015-03-19 11:48:16 -04:00
|
|
|
more_params = params.slice(:period, :order, :asc)
|
2015-03-16 15:14:33 -04:00
|
|
|
more_params[:page] = page + 1
|
|
|
|
|
2015-03-19 14:53:52 -04:00
|
|
|
render_json_dump directory_items: serialize_data(result, DirectoryItemSerializer),
|
2015-03-16 15:14:33 -04:00
|
|
|
total_rows_directory_items: result_count,
|
|
|
|
load_more_directory_items: directory_items_path(more_params)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|