Class: Course::CourseUserPreloadService

Inherits:
Object
  • Object
show all
Defined in:
app/services/course/course_user_preload_service.rb

Overview

Preloads CourseUsers for a collection of Users for a given Course.

Instance Method Summary collapse

Constructor Details

#initialize(users, course) ⇒ Hash{User => CourseUser}

Preloads CourseUsers and returns a hash that maps a User to its CourseUsers for the given course.

Parameters:

  • users (Array<User>|Array<Integer>)

    Users or their ids

  • course (Course)

11
12
13
14
15
16
# File 'app/services/course/course_user_preload_service.rb', line 11

def initialize(users, course)
  course_users = CourseUser.includes(:user, :course).where(user: users.uniq, course: course)
  @user_course_user_hash = course_users.map do |course_user|
    [course_user.user, course_user]
  end.to_h
end

Instance Method Details

#course_user_for(user) ⇒ CourseUser|nil

Finds the user's course_user for the given course.

Parameters:

  • The (User)

    user to find a course_user for

Returns:

  • (CourseUser|nil)

    The course_user, if found, else nil


22
23
24
# File 'app/services/course/course_user_preload_service.rb', line 22

def course_user_for(user)
  @user_course_user_hash[user]
end