Class: Vident::ViewComponent::Base

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Component
Defined in:
lib/vident/view_component/base.rb

Constant Summary collapse

SELF_CLOSING_TAGS =
Set[:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_component_modified_timeObject



7
8
9
# File 'lib/vident/view_component/base.rb', line 7

def cache_component_modified_time
  cache_sidecar_view_modified_time + cache_rb_component_modified_time
end

.cache_rb_component_modified_timeObject



15
16
17
# File 'lib/vident/view_component/base.rb', line 15

def cache_rb_component_modified_time
  ::File.exist?(component_path) ? ::File.mtime(component_path).to_i.to_s : ""
end

.cache_sidecar_view_modified_timeObject



11
12
13
# File 'lib/vident/view_component/base.rb', line 11

def cache_sidecar_view_modified_time
  ::File.exist?(template_path) ? ::File.mtime(template_path).to_i.to_s : ""
end

.component_pathObject



33
34
35
# File 'lib/vident/view_component/base.rb', line 33

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

.components_base_pathObject



37
38
39
# File 'lib/vident/view_component/base.rb', line 37

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

.template_pathObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vident/view_component/base.rb', line 19

def template_path
  # Check for common ViewComponent template extensions in order of preference
  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

  # Return the default .html.erb path if no template is found
  Rails.root.join(components_base_path, "#{virtual_path}.html.erb").to_s
end

Instance Method Details

#as_stimulus_actionObject



74
75
76
# File 'lib/vident/view_component/base.rb', line 74

def as_stimulus_action(...)
  to_data_attribute_string(**stimulus_action(...))
end

#as_stimulus_actionsObject



70
71
72
# File 'lib/vident/view_component/base.rb', line 70

def as_stimulus_actions(...)
  to_data_attribute_string(**stimulus_actions(...))
end

#as_stimulus_classObject



114
115
116
# File 'lib/vident/view_component/base.rb', line 114

def as_stimulus_class(...)
  to_data_attribute_string(**stimulus_class(...))
end

#as_stimulus_classesObject



110
111
112
# File 'lib/vident/view_component/base.rb', line 110

def as_stimulus_classes(...)
  to_data_attribute_string(**stimulus_classes(...))
end

#as_stimulus_controllerObject



82
83
84
# File 'lib/vident/view_component/base.rb', line 82

def as_stimulus_controller(...)
  to_data_attribute_string(**stimulus_controller(...))
end

#as_stimulus_controllersObject



78
79
80
# File 'lib/vident/view_component/base.rb', line 78

def as_stimulus_controllers(...)
  to_data_attribute_string(**stimulus_controllers(...))
end

#as_stimulus_outletObject



90
91
92
# File 'lib/vident/view_component/base.rb', line 90

def as_stimulus_outlet(...)
  to_data_attribute_string(**stimulus_outlet(...))
end

#as_stimulus_outletsObject



86
87
88
# File 'lib/vident/view_component/base.rb', line 86

def as_stimulus_outlets(...)
  to_data_attribute_string(**stimulus_outlets(...))
end

#as_stimulus_paramObject



106
107
108
# File 'lib/vident/view_component/base.rb', line 106

def as_stimulus_param(...)
  to_data_attribute_string(**stimulus_param(...))
end

#as_stimulus_paramsObject



102
103
104
# File 'lib/vident/view_component/base.rb', line 102

def as_stimulus_params(...)
  to_data_attribute_string(**stimulus_params(...))
end

#as_stimulus_targetObject



66
67
68
# File 'lib/vident/view_component/base.rb', line 66

def as_stimulus_target(...)
  to_data_attribute_string(**stimulus_target(...))
end

#as_stimulus_targetsObject



62
63
64
# File 'lib/vident/view_component/base.rb', line 62

def as_stimulus_targets(...)
  to_data_attribute_string(**stimulus_targets(...))
end

#as_stimulus_valueObject



98
99
100
# File 'lib/vident/view_component/base.rb', line 98

def as_stimulus_value(...)
  to_data_attribute_string(**stimulus_value(...))
end

#as_stimulus_valuesObject



94
95
96
# File 'lib/vident/view_component/base.rb', line 94

def as_stimulus_values(...)
  to_data_attribute_string(**stimulus_values(...))
end

#before_renderObject

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



46
47
48
49
# File 'lib/vident/view_component/base.rb', line 46

def before_render
  resolve_stimulus_attributes_at_render_time
  super
end

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/vident/view_component/base.rb', line 51

def root_element(**overrides, &block)
  tag_type = root_element_tag_type
  child_content = view_context.capture(self, &block) if block_given? # Evaluate before generating the outer tag options to ensure DSL methods are executed
  options = resolve_root_element_attributes_before_render(overrides)
  if SELF_CLOSING_TAGS.include?(tag_type)
    view_context.tag(tag_type, options)
  else
    view_context.(tag_type, child_content, options)
  end
end