Module: CourseComponentQueryConcern

Extended by:
ActiveSupport::Concern
Included in:
Course::CourseComponentsConcern, Instance::CourseComponentsConcern
Defined in:
app/models/concerns/course_component_query_concern.rb

Overview

This concern provides methods to query which course components are set as enabled/disabled for the models in which they are included (e.g. Course, Instance).

The core functionality that this concern provides is the logic to reconcile:

  1. Settings specified by users who are managers at the current level (e.g. course or instance level).
  2. Settings implicitly casacaded down (via available_components) from a parent model, if any.
  3. Settings that are hard-coded within the component.

It expects the models to have a settings_on_rails settings column and also provides methods to persist course component settings for them.

Instance Method Summary collapse

Instance Method Details

#available_componentsArray<Class>

Returns The classes of the components that are available

Returns:

  • (Array<Class>)

    The classes of the components that are available

Raises:

  • (NotImplementedError)

17
18
19
# File 'app/models/concerns/course_component_query_concern.rb', line 17

def available_components
  raise NotImplementedError, 'Concrete concern must implement available_components'
end

#disableable_componentsArray<Class>

Returns The subset of available_components that the user can disable.

Returns:

  • (Array<Class>)

    The subset of available_components that the user can disable.

Raises:

  • (NotImplementedError)

22
23
24
# File 'app/models/concerns/course_component_query_concern.rb', line 22

def disableable_components
  raise NotImplementedError, 'Concrete concern must implement disableable_components'
end

#enabled_componentsArray<Class>

Applies user preferences to components that can be disabled.

Returns:

  • (Array<Class>)

    Array of components that are effectively enabled.


33
34
35
# File 'app/models/concerns/course_component_query_concern.rb', line 33

def enabled_components
  @enabled_components ||= undisableable_components | user_enabled_components
end

#enabled_components_keys=(keys) ⇒ Object

Updates the list of enabled components given a list of key.

Parameters:

  • keys (Array<Symbol|String>)

60
61
62
63
64
65
66
# File 'app/models/concerns/course_component_query_concern.rb', line 60

def enabled_components_keys=(keys)
  keys = keys.reject(&:blank?).map(&:to_sym)
  validate_settable_component_keys!(keys)
  disableable_components.each do |component|
    unsafe_set_component_enabled_boolean(component.key, keys.include?(component.key))
  end
end

#set_component_enabled_boolean(key, value) ⇒ Object

Set component's enabled key only if it is disableable


46
47
48
49
# File 'app/models/concerns/course_component_query_concern.rb', line 46

def set_component_enabled_boolean(key, value)
  validate_settable_component_keys!([key])
  unsafe_set_component_enabled_boolean(key, value)
end

#set_component_enabled_boolean!(key, value) ⇒ Object

Sets and saves component's enabled key


52
53
54
55
# File 'app/models/concerns/course_component_query_concern.rb', line 52

def set_component_enabled_boolean!(key, value)
  set_component_enabled_boolean(key, value)
  save!
end

#undisableable_componentsObject


26
27
28
# File 'app/models/concerns/course_component_query_concern.rb', line 26

def undisableable_components
  @undisableable_components ||= available_components - disableable_components
end

#user_enabled_componentsArray<Class>

Returns Components specified as 'enabled' by the user.

Returns:

  • (Array<Class>)

    Components specified as 'enabled' by the user.


38
39
40
41
42
43
# File 'app/models/concerns/course_component_query_concern.rb', line 38

def user_enabled_components
  @user_enabled_components ||= available_components.select do |component|
    enabled = component_setting(component.key).enabled
    enabled.nil? ? component.enabled_by_default? : enabled
  end
end