You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/users/course_service.rb

53 lines
1.1 KiB

6 years ago
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
6 years ago
courses = category_scope_courses.not_deleted
6 years ago
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
6 years ago
relations.processing
6 years ago
when 'end' then
6 years ago
relations.ended
6 years ago
else
relations
end
end
def observed_logged_user?
User.current.id == user.id
end
end