Method: ActiveComponent::Base#initialize

Defined in:
lib/active_component/base.rb

#initialize(args = {}) ⇒ Base

Whenever the user creates a new object from a class inherited from ActiveComponent::Base it needs to define its attributes as ‘attr_accessor :var` in order to be able to initialize the object with specific values.

class ViewComponent < ActiveComponent::Base

attr_accessor :var

end

The values shall be provided as hash, active component automatically will know who’s going to receive the value otherwise raise an error.

component = ViewComponent.new(var: var)

In addition to this we provide the controller context from where the view component is called.

self.controller from anywhere in the inherited classes.



37
38
39
40
# File 'lib/active_component/base.rb', line 37

def initialize(args = {})
  assign_variables args
  @controller = ActiveComponent.get_controller if defined?(Rails)
end