From 2fcbd4c3512cd0a4c4ac22c025834f7a71842cc8 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 20 Mar 2020 17:31:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E4=B8=BB=E9=A1=B5=E7=9A=84?= =?UTF-8?q?=E6=94=B6=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/collection.rb | 3 +++ app/models/user.rb | 7 +++++-- app/services/users/shixun_service.rb | 8 +++++--- app/services/users/subject_service.rb | 7 +++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/models/collection.rb b/app/models/collection.rb index 53d2b0f8b..4ed77db60 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -1,4 +1,7 @@ class Collection < ApplicationRecord belongs_to :user belongs_to :container, polymorphic: true, optional: true + + scope :shixuns, -> {where(container_type: 'Shixun')} + scope :subjects, -> {where(container_type: 'Subject')} end diff --git a/app/models/user.rb b/app/models/user.rb index b909d6dd6..02a4eba85 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -48,8 +48,6 @@ class User < ApplicationRecord has_many :course_messages has_many :courses, foreign_key: 'tea_id', dependent: :destroy - has_many :collections, dependent: :destroy - #试卷 has_many :exercise_banks, :dependent => :destroy has_many :exercise_users, :dependent => :destroy @@ -119,6 +117,11 @@ class User < ApplicationRecord has_many :manage_course_members, -> { teachers_and_admin }, class_name: 'CourseMember' has_many :manage_courses, through: :manage_course_members, source: :course + has_many :collections, dependent: :destroy + has_many :shixun_collections, -> { shixuns }, class_name: 'Collection' + has_many :subject_collections, -> { subjects }, class_name: 'Collection' + + # 关注 # has_many :be_watchers, foreign_key: :user_id, dependent: :destroy # 我的关注 # has_many :be_watcher_users, through: :be_watchers, dependent: :destroy # 我关注的用户 diff --git a/app/services/users/shixun_service.rb b/app/services/users/shixun_service.rb index b13b0ab1f..7e95cda84 100644 --- a/app/services/users/shixun_service.rb +++ b/app/services/users/shixun_service.rb @@ -24,15 +24,17 @@ class Users::ShixunService user.study_shixuns.where(shixuns: {id: laboratory.shixuns}) when 'manage' then laboratory.shixuns.where(id: user.shixuns) + when 'collect' then + laboratory.shixuns.where(id: user.shixun_collections.pluck(:container_id)) else - ids = user.study_shixuns.pluck(:id) + user.shixuns.pluck(:id) + ids = user.study_shixuns.pluck(:id) + user.shixuns.pluck(:id) + user.shixun_collections.pluck(:container_id) laboratory.shixuns.where(id: ids) end end def status_filter(relations) case params[:category] - when 'study' then + when 'study', 'collect' then study_shixun_status_filter(relations) when 'manage' then manage_shixun_status_filter(relations) @@ -97,7 +99,7 @@ class Users::ShixunService end case params[:category] - when 'study' then + when 'study', 'collect' then relations.order("myshixuns.#{sort_by} #{sort_direction}") when 'manage' then relations.order("shixuns.#{sort_by} #{sort_direction}") diff --git a/app/services/users/subject_service.rb b/app/services/users/subject_service.rb index d0b995c8e..a8b9b32fb 100644 --- a/app/services/users/subject_service.rb +++ b/app/services/users/subject_service.rb @@ -25,10 +25,13 @@ class Users::SubjectService Subject.joins(stage_shixuns: { shixun: :myshixuns }).where(myshixuns: { user_id: user.id }) when 'manage' then Subject.joins(:subject_members).where(subject_members: { user_id: user.id }) + when 'collect' then + Subject.where(id: user.subject_collections.pluck(:container_id)) else study_subject_ids = StageShixun.where(shixun_id: user.myshixuns.pluck(:shixun_id)).pluck(:subject_id) manage_subject_ids = user.subject_members.pluck(:subject_id) - Subject.where(id: study_subject_ids + manage_subject_ids) + collect_subject_ids = user.subject_collections.pluck(:container_id) + Subject.where(id: study_subject_ids + manage_subject_ids + collect_subject_ids) end end @@ -45,7 +48,7 @@ class Users::SubjectService return relations unless self_or_admin? case params[:category] - when 'study' then + when 'study', 'collect' then study_subject_status_filter(relations) when 'manage' then manage_subject_status_filter(relations)