Class: Vident2::ViewComponent::Base

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Component
Defined in:
lib/vident2/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

Constants included from Component

Component::MUTATOR_METHODS, Component::PLURAL_PARSERS, Component::SINGULAR_NAMES, Component::SINGULAR_PARSERS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Component

#after_component_initialize, #child_element, #class_list_for_stimulus_classes, #clone, #component_name, #id, #inspect, #outlet_id, #prop_names, #random_id, #resolve_stimulus_attributes_at_render_time, #root, #root_element_attributes, #root_element_classes, #stimulus_identifier, #stimulus_scoped_event, #stimulus_scoped_event_on_window

Methods included from Tailwind

#tailwind_merge_available?, #tailwind_merger

Class Method Details

.cache_component_modified_timeObject



11
12
13
# File 'lib/vident2/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_timeObject



19
20
21
# File 'lib/vident2/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_timeObject



15
16
17
# File 'lib/vident2/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_pathObject



35
36
37
# File 'lib/vident2/view_component/base.rb', line 35

def component_path
  Rails.root.join(components_base_path, "#{virtual_path}.rb").to_s
end

.components_base_pathObject



39
40
41
# File 'lib/vident2/view_component/base.rb', line 39

def components_base_path
  ::Rails.configuration.view_component.view_component_path || "app/components"
end

.template_pathObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vident2/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_actionObject Also known as: as_action



74
# File 'lib/vident2/view_component/base.rb', line 74

def as_stimulus_action(...)   = to_data_attribute_string(**stimulus_action(...).to_h)

#as_stimulus_actionsObject



73
# File 'lib/vident2/view_component/base.rb', line 73

def as_stimulus_actions(...)  = to_data_attribute_string(**stimulus_actions(...).to_h)

#as_stimulus_classObject Also known as: as_class



84
# File 'lib/vident2/view_component/base.rb', line 84

def as_stimulus_class(...)    = to_data_attribute_string(**stimulus_class(...).to_h)

#as_stimulus_classesObject



83
# File 'lib/vident2/view_component/base.rb', line 83

def as_stimulus_classes(...)  = to_data_attribute_string(**stimulus_classes(...).to_h)

#as_stimulus_controllerObject Also known as: as_controller



76
# File 'lib/vident2/view_component/base.rb', line 76

def as_stimulus_controller(...)  = to_data_attribute_string(**stimulus_controller(...).to_h)

#as_stimulus_controllersObject



75
# File 'lib/vident2/view_component/base.rb', line 75

def as_stimulus_controllers(...) = to_data_attribute_string(**stimulus_controllers(...).to_h)

#as_stimulus_outletObject Also known as: as_outlet



78
# File 'lib/vident2/view_component/base.rb', line 78

def as_stimulus_outlet(...)   = to_data_attribute_string(**stimulus_outlet(...).to_h)

#as_stimulus_outletsObject



77
# File 'lib/vident2/view_component/base.rb', line 77

def as_stimulus_outlets(...)  = to_data_attribute_string(**stimulus_outlets(...).to_h)

#as_stimulus_paramObject Also known as: as_param



82
# File 'lib/vident2/view_component/base.rb', line 82

def as_stimulus_param(...)    = to_data_attribute_string(**stimulus_param(...).to_h)

#as_stimulus_paramsObject



81
# File 'lib/vident2/view_component/base.rb', line 81

def as_stimulus_params(...)   = to_data_attribute_string(**stimulus_params(...).to_h)

#as_stimulus_targetObject Also known as: as_target



72
# File 'lib/vident2/view_component/base.rb', line 72

def as_stimulus_target(...)   = to_data_attribute_string(**stimulus_target(...).to_h)

#as_stimulus_targetsObject

Inline ERB helpers: emit ‘data-*=“…”` attribute strings for the given stimulus input. Thin wrappers around the plural/singular parsers — Collection#to_h is the splat target used by v1’s helpers too. Returns an ActiveSupport::SafeBuffer.



71
# File 'lib/vident2/view_component/base.rb', line 71

def as_stimulus_targets(...)  = to_data_attribute_string(**stimulus_targets(...).to_h)

#as_stimulus_valueObject Also known as: as_value



80
# File 'lib/vident2/view_component/base.rb', line 80

def as_stimulus_value(...)    = to_data_attribute_string(**stimulus_value(...).to_h)

#as_stimulus_valuesObject



79
# File 'lib/vident2/view_component/base.rb', line 79

def as_stimulus_values(...)   = to_data_attribute_string(**stimulus_values(...).to_h)

#before_renderObject

ViewComponent lifecycle hook: resolve stimulus DSL procs now that ‘@view_context` is set (so `helpers` works inside them).



48
49
50
51
# File 'lib/vident2/view_component/base.rb', line 48

def before_render
  resolve_stimulus_attributes_at_render_time
  super
end

#root_element(**overrides, &block) ⇒ Object

Same block-capture-first ordering as the Phlex adapter. Children instantiated inside the block can still mutate this instance’s Draft (outlet-host pattern) before we seal.



56
57
58
59
60
61
62
63
64
65
# File 'lib/vident2/view_component/base.rb', line 56

def root_element(**overrides, &block)
  tag_type = root_element_tag_type
  child_content = view_context.capture(self, &block) if block
  options = build_root_element_attributes(overrides)
  if SELF_CLOSING_TAGS.include?(tag_type)
    view_context.tag(tag_type, options)
  else
    view_context.(tag_type, child_content, options)
  end
end