Class: ActionComponent::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/actioncomponent/component.rb,
lib/actioncomponent/component/renderer.rb,
lib/actioncomponent/component/view_model.rb

Overview

Renders a given component

Defined Under Namespace

Classes: InvalidVMError, Renderer, ViewModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_path:, lookup_context: nil, view_model_data: {}) ⇒ Component

Returns a new instance of Component.



23
24
25
26
27
# File 'lib/actioncomponent/component.rb', line 23

def initialize(component_path:, lookup_context: nil, view_model_data: {})
  @component_path = component_path.to_s.gsub(%r{^/}, '')
  @lookup_context = lookup_context
  @view_model_data = view_model_data
end

Instance Attribute Details

#component_pathvoid (readonly)

Returns the value of attribute component_path.



21
22
23
# File 'lib/actioncomponent/component.rb', line 21

def component_path
  @component_path
end

#view_model_datavoid (readonly)

Returns the value of attribute view_model_data.



21
22
23
# File 'lib/actioncomponent/component.rb', line 21

def view_model_data
  @view_model_data
end

Class Method Details

.helper_objectvoid



6
7
8
9
10
11
# File 'lib/actioncomponent/component.rb', line 6

def helper_object
  @helper_object = Class.new(ActionView::Base) do
    include ::Rails.application.routes.url_helpers
    include ::Rails.application.routes.mounted_helpers
  end.new
end

.helper_vm_paramsvoid



13
14
15
16
17
18
# File 'lib/actioncomponent/component.rb', line 13

def helper_vm_params
  {
    h: helper_object,
    helper: helper_object
  }
end

Instance Method Details

#create_view_modelvoid



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/actioncomponent/component.rb', line 45

def create_view_model
  vm_class = ActionComponent::Component::ViewModel

  begin
    vm_class = find_custom_vm_class!
  rescue NameError
    vm_class = ActionComponent::Component::ViewModel
  end

  vm_class.new(**view_model_data.merge(view_model_default_data))
end

#full_component_pathvoid



63
64
65
# File 'lib/actioncomponent/component.rb', line 63

def full_component_path
  Rails.root.join(ActionComponent.configuration.components_path)
end

#lookup_contextvoid



37
38
39
40
41
42
43
# File 'lib/actioncomponent/component.rb', line 37

def lookup_context
  @lookup_context ||= ActionView::LookupContext.new(
    [
      full_component_path
    ]
  )
end

#rendervoid



29
30
31
# File 'lib/actioncomponent/component.rb', line 29

def render
  renderer.render(component_path: @component_path)
end

#renderervoid



33
34
35
# File 'lib/actioncomponent/component.rb', line 33

def renderer
  ActionComponent::Component::Renderer.new(lookup_context, create_view_model)
end

#view_model_default_datavoid



57
58
59
60
61
# File 'lib/actioncomponent/component.rb', line 57

def view_model_default_data
  # lookup_context is necessary for when there is an exception in our template
  # this is used in order to better describe the error stack
  self.class.helper_vm_params.merge(lookup_context: lookup_context)
end