Class: ActionComponent::Component
- Inherits:
-
Object
- Object
- ActionComponent::Component
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_path ⇒ void
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_data ⇒ void
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_object ⇒ void
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_params ⇒ void
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_model ⇒ void
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_path ⇒ void
63
64
65
|
# File 'lib/actioncomponent/component.rb', line 63
def full_component_path
Rails.root.join(ActionComponent.configuration.components_path)
end
|
#lookup_context ⇒ void
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
|
#render ⇒ void
29
30
31
|
# File 'lib/actioncomponent/component.rb', line 29
def render
renderer.render(component_path: @component_path)
end
|
#view_model_default_data ⇒ void
57
58
59
60
61
|
# File 'lib/actioncomponent/component.rb', line 57
def view_model_default_data
self.class.helper_vm_params.merge(lookup_context: lookup_context)
end
|