class Users::CourseService include CustomSortable sort_columns :updated_at, default_by: :updated_at, default_direction: :desc attr_reader :user, :params def initialize(user, params) @user = user @params = params end def call courses = category_scope_courses.not_deleted courses = status_filter(courses) custom_sort(courses, :updated_at, params[:sort_direction]) end private def category_scope_courses case params[:category] when 'study' then user.as_student_courses when 'manage' then user.manage_courses else ids = user.as_student_courses.pluck(:id) + user.manage_courses.pluck(:id) Course.where(id: ids) end end def status_filter(relations) # 只有自己查看才有过滤 return relations unless observed_logged_user? case params[:status] when 'processing' then relations.processing when 'end' then relations.ended else relations end end def observed_logged_user? User.current.id == user.id end end