Class: Course::Condition

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

Defined Under Namespace

Modules: ConditionsHelper Classes: Achievement, AchievementsController, Assessment, AssessmentsController, Level, LevelsController

Constant Summary collapse

ALL_CONDITIONS =
[
  Course::Condition::Achievement.name,
  Course::Condition::Assessment.name,
  Course::Condition::Level.name
].freeze

Class Method Summary collapse

Class Method Details

.conditionals_for(course) ⇒ Object

Finds all the conditionals for the given course.


21
22
23
24
25
26
27
28
29
# File 'app/models/course/condition.rb', line 21

def conditionals_for(course)
  dependent_class_to_condition_class_mapping.keys.map do |conditional_name|
    next unless conditional_name.constantize.include?(
      ActiveRecord::Base::ConditionalInstanceMethods
    )

    conditional_name.constantize.where(course_id: course)
  end.flatten
end

.find_conditionals_of(dependent_object) ⇒ Object

Finds all conditionals that depend on the given object.


36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/course/condition.rb', line 36

def find_conditionals_of(dependent_object)
  condition_classes_of(dependent_object).map do |condition_name|
    Course::Condition.find_by_sql("SELECT * FROM course_conditions cc\nINNER JOIN course_condition_\#{condition_name.demodulize.downcase.pluralize} ccs\nON cc.actable_type = '\#{condition_name}'\nAND cc.actable_id = ccs.id\nAND ccs.\#{dependent_object.class.name.demodulize.downcase}_id = \#{dependent_object.id}\nWHERE course_id = \#{dependent_object.course_id}\n")
  end.flatten.map(&:conditional)
end