From 008b15a967cdc6378f76cc3899b4b3df09c11f49 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 29 Nov 2019 17:14:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=90=E7=AB=99=E5=90=8C=E6=AD=A5=E5=AE=9E?= =?UTF-8?q?=E8=B7=B5=E8=AF=BE=E7=A8=8B=E5=92=8C=E5=AE=9E=E8=AE=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/concerns/laboratory_helper.rb | 2 +- app/controllers/shixuns_controller.rb | 17 +---------------- app/controllers/users/shixuns_controller.rb | 10 +--------- app/services/search_service.rb | 8 +------- app/services/shixun_search_service.rb | 19 +++++-------------- app/services/users/shixun_service.rb | 12 +++++++----- 6 files changed, 16 insertions(+), 52 deletions(-) diff --git a/app/controllers/concerns/laboratory_helper.rb b/app/controllers/concerns/laboratory_helper.rb index a2a10d352..e03273d68 100644 --- a/app/controllers/concerns/laboratory_helper.rb +++ b/app/controllers/concerns/laboratory_helper.rb @@ -10,7 +10,7 @@ module LaboratoryHelper end def current_laboratory - @_current_laboratory ||= (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(2)) + @_current_laboratory ||= (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1)) end def default_laboratory diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index a321beb25..03fdbf97f 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -26,22 +26,7 @@ class ShixunsController < ApplicationController ## 获取课程列表 def index - ## 我的实训 - @shixuns = - if params[:order_by] == 'mine' - tip_exception(401, "..") unless current_user.logged? - current_user.my_shixuns - else - Shixun.unhidden - end - - ## 云上实验室过滤 - unless current_laboratory.main_site? - @shixuns = @shixuns.joins(:laboratory_shixuns).where(laboratory_shixuns: { laboratory_id: current_laboratory.id }) - else - not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{current_laboratory.id}") - @shixuns = @shixuns.where.not(id: not_shixun_ids) - end + @shixuns = current_laboratory.shixuns.unhidden ## 方向 if params[:tag_level].present? && params[:tag_id].present? diff --git a/app/controllers/users/shixuns_controller.rb b/app/controllers/users/shixuns_controller.rb index fcf9e3618..978a2e8e7 100644 --- a/app/controllers/users/shixuns_controller.rb +++ b/app/controllers/users/shixuns_controller.rb @@ -1,14 +1,6 @@ class Users::ShixunsController < Users::BaseController def index - shixuns = Users::ShixunService.new(observed_user, query_params).call - - ## 云上实验室过滤 - if current_laboratory.main_site? - not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{current_laboratory.id}") - shixuns = shixuns.where.not(id: not_shixun_ids) - else - shixuns = shixuns.joins(:laboratory_shixuns).where(laboratory_shixuns: { laboratory_id: current_laboratory.id }) - end + shixuns = Users::ShixunService.new(observed_user, query_params, current_laboratory).call @count = shixuns.count @shixuns = paginate(shixuns.includes(:first_tag_repertoire), special: observed_user.is_teacher?) diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 8138f4e33..c717c31e5 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -40,13 +40,7 @@ class SearchService < ApplicationService def extra_options case params[:type].to_s.strip when 'shixun' then - if Laboratory.current.main_site? - not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{Laboratory.current.id}") - shixun_ids = Shixun.where.not(id: not_shixun_ids).pluck(:id) - else - shixun_ids = Laboratory.current.shixuns.pluck(:id) - end - { where: { id: shixun_ids } } + { where: { id: Laboratory.current.shixuns.pluck(:id) } } when 'subject' then { where: { id: Laboratory.current.subjects.pluck(:id) } } when 'course' then diff --git a/app/services/shixun_search_service.rb b/app/services/shixun_search_service.rb index 63e10d6b0..31073eca3 100644 --- a/app/services/shixun_search_service.rb +++ b/app/services/shixun_search_service.rb @@ -14,16 +14,18 @@ class ShixunSearchService < ApplicationService # 状态:已发布/未发布 status = params[:status] || "all" + @shixuns = laboratory.shixuns.none_closed + # 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭) if type == "mine" - @shixuns = User.current.shixuns.none_closed + @shixuns = @shixuns.where(id: User.current.shixuns) else if User.current.admin? || User.current.business? - @shixuns = Shixun.none_closed.where(hidden: 0) + @shixuns = @shixuns.where(hidden: 0) else none_shixun_ids = ShixunSchool.where("school_id != #{User.current.school_id}").pluck(:shixun_id) - @shixuns = Shixun.where.not(id: none_shixun_ids).none_closed.where(hidden: 0) + @shixuns = @shixuns.where.not(id: none_shixun_ids).where(hidden: 0) end end @@ -36,17 +38,6 @@ class ShixunSearchService < ApplicationService @shixuns = @shixuns.where(trainee: params[:diff]) end - ## 云上实验室过滤 - if laboratory.main_site? - not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{laboratory.id}") - @shixuns = @shixuns.where.not(id: not_shixun_ids) - else - @shixuns = @shixuns.joins(:laboratory_shixuns).where(laboratory_shixuns: { laboratory_id: laboratory.id }) - end - - # laboratory = Laboratory.find_by_subdomain(subdomain) - # @shixuns = @shixuns.where(id: laboratory.shixuns) if laboratory - Shixun.search(keyword, search_options) end diff --git a/app/services/users/shixun_service.rb b/app/services/users/shixun_service.rb index 9460d3a88..1c7fe2cea 100644 --- a/app/services/users/shixun_service.rb +++ b/app/services/users/shixun_service.rb @@ -1,12 +1,14 @@ class Users::ShixunService - attr_reader :user, :params + attr_reader :user, :params, :laboratory - def initialize(user, params) + def initialize(user, params, laboratory) @user = user @params = params + @laboratory = laboratory end def call + shixuns = category_scope_shixuns shixuns = user_policy_filter(shixuns) @@ -19,12 +21,12 @@ class Users::ShixunService def category_scope_shixuns case params[:category] when 'study' then - user.study_shixuns + laboratory.shixuns.where(id: user.study_shixuns) when 'manage' then - user.shixuns + laboratory.shixuns.where(id: user.shixuns) else ids = user.study_shixuns.pluck(:id) + user.shixuns.pluck(:id) - Shixun.where(id: ids) + laboratory.shixuns.where(id: ids) end end