Class: Course::ControllerComponentHost

Inherits:
Object
  • Object
show all
Includes:
Componentize
Defined in:
app/controllers/components/course/controller_component_host.rb

Overview

The course component framework isolates features as components. The intent is to allow each feature to be enabled / disabled indepenedently within a course.

When creating a component:

  • Your component class should include Course::ControllerComponentHost::Component. This injects the methods found in the Sidebar and Settings modules into the component. Override these methods to customise your component.

  • Your component class's initializer should take in the component host's context (a controller) as its only argument. You may do this by having your component inherit from SimpleDelegator. This allows you to call methods on the given context from your component, e.g. a call to Course::Controller#current_course will be delegated to the controller.

  • If your component has settings, you may define a settings model for it. (See Settings::ClassMethods#settings_class for conventions to follow.)

  • You will also need to associate controllers for a component with the component class in order for it to be automatically enabled / disabled based on the course's settings (see ComponentController).

Defined Under Namespace

Modules: Settings, Sidebar

Instance Method Summary collapse

Methods included from Componentize

become_component_host

Constructor Details

#initialize(context) ⇒ ControllerComponentHost

Initializes the component host instance. This loads all components.

Parameters:

  • context

    The context to execute all component instance methods on.


129
130
131
132
# File 'app/controllers/components/course/controller_component_host.rb', line 129

def initialize(context)
  @context = context
  components
end

Instance Method Details

#[](component_key) ⇒ Object?

Gets the component instance with the given key.

Parameters:

  • component_key (String|Symbol)

    The key of the component to find.

Returns:

  • (Object)

    The component with the given key.

  • (nil)

    If component is not enabled.


151
152
153
154
# File 'app/controllers/components/course/controller_component_host.rb', line 151

def [](component_key)
  validate_component_key!(component_key)
  components.find { |component| component.key == component_key.to_sym }
end

#componentsArray

Instantiates the enabled components.

Returns:

  • (Array)

    The instantiated enabled components.


142
143
144
# File 'app/controllers/components/course/controller_component_host.rb', line 142

def components
  @components ||= enabled_components.map { |component| component.new(@context) }
end

#enabled_componentsArray<Class>

Returns Classes of effectively enabled components.

Returns:

  • (Array<Class>)

    Classes of effectively enabled components.


135
136
137
# File 'app/controllers/components/course/controller_component_host.rb', line 135

def enabled_components
  @enabled_components ||= @context.current_course.enabled_components
end

Gets the sidebar elements.

Sidebar elements have the given format:

  {
     key: :item_key, # The unique key of the item to identify it among others. Can be nil if
                     # there is no need to distinguish between items.
                     # +normal+ type elements must have a key because their ordering is a
                     # user setting.
     title: 'Sidebar Item Title',
     type: :admin, # Will be considered as +:normal+ if not set. Currently +:normal+, +:admin+,
                   # and +:settings+ are used.
     weight: 100, # The default weight of the item. Larger weights (heavier items) sink.
     path: path_to_the_component,
     unread: 0 # Number of unread items. Can be +nil+, if not needed.
  }

The elements are rendered on all Course controller subclasses as part of a nested template.


176
177
178
# File 'app/controllers/components/course/controller_component_host.rb', line 176

def sidebar_items
  @sidebar_items ||= components.flat_map(&:sidebar_items)
end