Class: Course::Level

Inherits:
ApplicationRecord show all
Includes:
ModelComponentHost::Component
Defined in:
app/models/course/level.rb

Constant Summary collapse

DEFAULT_THRESHOLD =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_course_initialize(course) ⇒ Object

Build default level when a new course is initalised. The default level has 0 experience_points_threshold.


32
33
34
35
36
# File 'app/models/course/level.rb', line 32

def self.after_course_initialize(course)
  return if course.persisted? || course.default_level?

  course.levels.build(experience_points_threshold: DEFAULT_THRESHOLD)
end

Instance Method Details

#default_level?Boolean

Returns true if level is a default level. Default level is currently implemented as a level with 0 threshold

Returns:

  • (Boolean)

42
43
44
# File 'app/models/course/level.rb', line 42

def default_level?
  experience_points_threshold == DEFAULT_THRESHOLD
end

#initialize_duplicate(duplicator, _other) ⇒ Object


65
66
67
# File 'app/models/course/level.rb', line 65

def initialize_duplicate(duplicator, _other)
  self.course = duplicator.options[:destination_course]
end

#nextCourse::Level?

Returns the next higher level in the course nil is returned if current level is the highest level

Returns:

  • (Course::Level)

    For levels with next level in the course.

  • (nil)

    If current level is the highest in the course.


51
52
53
54
# File 'app/models/course/level.rb', line 51

def next
  return @next if defined? @next
  @next = course.levels.offset(level_number + 1).first
end

#next_level_thresholdInteger

Returns the experience_points_threshold of the next level. If current level is highest the current experience_points_threshold will be returned.

level if current level is the highest.

Returns:

  • (Integer)

    The experience_points_threshold of the next level, or threshold of current


61
62
63
# File 'app/models/course/level.rb', line 61

def next_level_threshold
  self.next ? self.next.experience_points_threshold : experience_points_threshold
end