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.

Parameters:

  • course (Course)

    The course with the conditionals to be retrieved.

Returns:

  • (Object)

    acts_as_conditionals objects belonging to 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.

Parameters:

Returns:

  • (Object)

    acts_as_conditional Objects that depend on the condition_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(<<-SQL)
      SELECT * FROM course_conditions cc
        INNER JOIN course_condition_#{condition_name.demodulize.downcase.pluralize} ccs
        ON cc.actable_type = '#{condition_name}'
          AND cc.actable_id = ccs.id
          AND ccs.#{dependent_object.class.name.demodulize.downcase}_id = #{dependent_object.id}
      WHERE course_id = #{dependent_object.course_id}
    SQL
  end.flatten.map(&:conditional)
end