Class: Vident::ViewComponent::Base
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Vident::ViewComponent::Base
- Includes:
- Component
- Defined in:
- lib/vident/view_component/base.rb
Constant Summary collapse
- SELF_CLOSING_TAGS =
Set[*%i[area base br col embed hr img input link meta param source track wbr]].freeze
Class Method Summary collapse
- .cache_component_modified_time ⇒ Object
- .cache_rb_component_modified_time ⇒ Object
- .cache_sidecar_view_modified_time ⇒ Object
- .component_path ⇒ Object
- .components_base_path ⇒ Object
- .template_path ⇒ Object
Instance Method Summary collapse
- #as_stimulus_action ⇒ Object (also: #as_action)
- #as_stimulus_actions ⇒ Object
- #as_stimulus_class ⇒ Object (also: #as_class)
- #as_stimulus_classes ⇒ Object
- #as_stimulus_controller ⇒ Object (also: #as_controller)
- #as_stimulus_controllers ⇒ Object
- #as_stimulus_outlet ⇒ Object (also: #as_outlet)
- #as_stimulus_outlets ⇒ Object
- #as_stimulus_param ⇒ Object (also: #as_param)
- #as_stimulus_params ⇒ Object
- #as_stimulus_target ⇒ Object (also: #as_target)
- #as_stimulus_targets ⇒ Object
- #as_stimulus_value ⇒ Object (also: #as_value)
- #as_stimulus_values ⇒ Object
-
#before_render ⇒ Object
DSL procs stay unresolved until ‘@view_context` is set; resolve them here.
-
#cache_component(*extra_keys, expires_in: nil, &block) ⇒ Object
Fragment-cache the block’s rendered String using the Vident-computed ‘cache_key`.
-
#root_element(**overrides, &block) ⇒ Object
Capture block first so children can mutate this Draft before it seals (outlet-host pattern).
Class Method Details
.cache_component_modified_time ⇒ Object
11 12 13 |
# File 'lib/vident/view_component/base.rb', line 11 def cache_component_modified_time cache_sidecar_view_modified_time + cache_rb_component_modified_time end |
.cache_rb_component_modified_time ⇒ Object
19 20 21 |
# File 'lib/vident/view_component/base.rb', line 19 def cache_rb_component_modified_time ::File.exist?(component_path) ? ::File.mtime(component_path).to_i.to_s : "" end |
.cache_sidecar_view_modified_time ⇒ Object
15 16 17 |
# File 'lib/vident/view_component/base.rb', line 15 def cache_sidecar_view_modified_time ::File.exist?(template_path) ? ::File.mtime(template_path).to_i.to_s : "" end |
.component_path ⇒ Object
35 36 37 |
# File 'lib/vident/view_component/base.rb', line 35 def component_path Rails.root.join(components_base_path, "#{virtual_path}.rb").to_s end |
.components_base_path ⇒ Object
39 40 41 |
# File 'lib/vident/view_component/base.rb', line 39 def components_base_path ::Rails.configuration.view_component.view_component_path || "app/components" end |
.template_path ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vident/view_component/base.rb', line 23 def template_path extensions = [".html.erb", ".erb", ".html.haml", ".haml", ".html.slim", ".slim"] base_path = Rails.root.join(components_base_path, virtual_path) extensions.each do |ext| potential_path = "#{base_path}#{ext}" return potential_path if File.exist?(potential_path) end Rails.root.join(components_base_path, "#{virtual_path}.html.erb").to_s end |
Instance Method Details
#as_stimulus_action ⇒ Object Also known as: as_action
81 |
# File 'lib/vident/view_component/base.rb', line 81 def as_stimulus_action(...) = to_data_attribute_string(**stimulus_action(...).to_h) |
#as_stimulus_actions ⇒ Object
79 |
# File 'lib/vident/view_component/base.rb', line 79 def as_stimulus_actions(...) = to_data_attribute_string(**stimulus_actions(...).to_h) |
#as_stimulus_class ⇒ Object Also known as: as_class
101 |
# File 'lib/vident/view_component/base.rb', line 101 def as_stimulus_class(...) = to_data_attribute_string(**stimulus_class(...).to_h) |
#as_stimulus_classes ⇒ Object
99 |
# File 'lib/vident/view_component/base.rb', line 99 def as_stimulus_classes(...) = to_data_attribute_string(**stimulus_classes(...).to_h) |
#as_stimulus_controller ⇒ Object Also known as: as_controller
85 |
# File 'lib/vident/view_component/base.rb', line 85 def as_stimulus_controller(...) = to_data_attribute_string(**stimulus_controller(...).to_h) |
#as_stimulus_controllers ⇒ Object
83 |
# File 'lib/vident/view_component/base.rb', line 83 def as_stimulus_controllers(...) = to_data_attribute_string(**stimulus_controllers(...).to_h) |
#as_stimulus_outlet ⇒ Object Also known as: as_outlet
89 |
# File 'lib/vident/view_component/base.rb', line 89 def as_stimulus_outlet(...) = to_data_attribute_string(**stimulus_outlet(...).to_h) |
#as_stimulus_outlets ⇒ Object
87 |
# File 'lib/vident/view_component/base.rb', line 87 def as_stimulus_outlets(...) = to_data_attribute_string(**stimulus_outlets(...).to_h) |
#as_stimulus_param ⇒ Object Also known as: as_param
97 |
# File 'lib/vident/view_component/base.rb', line 97 def as_stimulus_param(...) = to_data_attribute_string(**stimulus_param(...).to_h) |
#as_stimulus_params ⇒ Object
95 |
# File 'lib/vident/view_component/base.rb', line 95 def as_stimulus_params(...) = to_data_attribute_string(**stimulus_params(...).to_h) |
#as_stimulus_target ⇒ Object Also known as: as_target
77 |
# File 'lib/vident/view_component/base.rb', line 77 def as_stimulus_target(...) = to_data_attribute_string(**stimulus_target(...).to_h) |
#as_stimulus_targets ⇒ Object
75 |
# File 'lib/vident/view_component/base.rb', line 75 def as_stimulus_targets(...) = to_data_attribute_string(**stimulus_targets(...).to_h) |
#as_stimulus_value ⇒ Object Also known as: as_value
93 |
# File 'lib/vident/view_component/base.rb', line 93 def as_stimulus_value(...) = to_data_attribute_string(**stimulus_value(...).to_h) |
#as_stimulus_values ⇒ Object
91 |
# File 'lib/vident/view_component/base.rb', line 91 def as_stimulus_values(...) = to_data_attribute_string(**stimulus_values(...).to_h) |
#before_render ⇒ Object
DSL procs stay unresolved until ‘@view_context` is set; resolve them here.
47 48 49 50 |
# File 'lib/vident/view_component/base.rb', line 47 def before_render resolve_stimulus_attributes_at_render_time super end |
#cache_component(*extra_keys, expires_in: nil, &block) ⇒ Object
Fragment-cache the block’s rendered String using the Vident-computed ‘cache_key`. Useful inside a `call` method; for sidecar ERB templates use the native `<% cache cache_key do %>` pattern instead.
55 56 57 58 59 60 61 |
# File 'lib/vident/view_component/base.rb', line 55 def cache_component(*extra_keys, expires_in: nil, &block) unless respond_to?(:cacheable?) && cacheable? raise ::Vident::ConfigurationError, "#{self.class.name} is not cacheable — `include Vident::Caching` and declare `with_cache_key` first." end ::Rails.cache.fetch([cache_key, *extra_keys], expires_in: expires_in) { capture(&block) } end |
#root_element(**overrides, &block) ⇒ Object
Capture block first so children can mutate this Draft before it seals (outlet-host pattern).
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vident/view_component/base.rb', line 64 def root_element(**overrides, &block) tag_type = root_element_tag_type child_content = view_context.capture(self, &block) if block = build_root_element_attributes(overrides) if SELF_CLOSING_TAGS.include?(tag_type) view_context.tag(tag_type, ) else view_context.content_tag(tag_type, child_content, ) end end |