Class: Course::Assessment::AchievementPreloadService

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

Overview

This service preloads all Achievement conditionals which lists Assessments as conditions. Used for Assessments#Index to reduce n+1 queries.

Instance Method Summary collapse

Constructor Details

#initialize(assessments) ⇒ AchievementPreloadService

Initialises the service with the listed assessments.

Parameters:


9
10
11
# File 'app/services/course/assessment/achievement_preload_service.rb', line 9

def initialize(assessments)
  @assessment_ids = assessments.map(&:id)
end

Instance Method Details

#achievement_conditional_for(assessment) ⇒ Array<Course::Achievement>

Returns all achievement conditionals listing the given assessment as a condition.

Parameters:

Returns:


17
18
19
20
21
# File 'app/services/course/assessment/achievement_preload_service.rb', line 17

def achievement_conditional_for(assessment)
  achievement_ids = assessment_achievement_hash[assessment.id]
  return [] unless achievement_ids
  achievements.select { |ach| achievement_ids.include?(ach.id) }
end