Class: Course::Condition::Achievement

Inherits:
ApplicationRecord show all
Includes:
DuplicationStateTrackingConcern
Defined in:
app/models/course/condition/achievement.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DuplicationStateTrackingConcern

#clear_duplication_flag, #duplicating?, #set_duplication_flag

Class Method Details

.dependent_classObject

Class that the condition depends on.


38
39
40
# File 'app/models/course/condition/achievement.rb', line 38

def self.dependent_class
  Course::Achievement.name
end

.on_dependent_status_change(achievement) ⇒ Object


42
43
44
45
# File 'app/models/course/condition/achievement.rb', line 42

def self.on_dependent_status_change(achievement)
  return unless achievement.saved_changes.any? || achievement.destroyed?
  achievement.execute_after_commit { evaluate_conditional_for(achievement.course_user) }
end

Instance Method Details

#initialize_duplicate(duplicator, other) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/course/condition/achievement.rb', line 47

def initialize_duplicate(duplicator, other)
  self.achievement = duplicator.duplicate(other.achievement)
  self.conditional_type = other.conditional_type # this is a simple string
  self.conditional = duplicator.duplicate(other.conditional)

  if duplicator.mode == :course
    self.course = duplicator.duplicate(other.course)
  elsif duplicator.mode == :object
    self.course = duplicator.options[:destination_course]
  end

  set_duplication_flag
end

#satisfied_by?(course_user) ⇒ Boolean

Checks if the user has the required achievement.


30
31
32
33
34
35
# File 'app/models/course/condition/achievement.rb', line 30

def satisfied_by?(course_user)
  # Unpublished achievements are considered not satisfied.
  return false unless achievement.published?

  course_user.achievements.exists?(achievement.id)
end