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



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

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

#as_stimulus_actionsObject



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

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

#as_stimulus_classObject



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

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

#as_stimulus_classesObject



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

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

#as_stimulus_controllerObject



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

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

#as_stimulus_controllersObject



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

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

#as_stimulus_outletObject



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

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

#as_stimulus_outletsObject



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

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

#as_stimulus_targetObject



59
60
61
# File 'lib/vident/view_component/base.rb', line 59

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

#as_stimulus_targetsObject



55
56
57
# File 'lib/vident/view_component/base.rb', line 55

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

#as_stimulus_valueObject



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

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

#as_stimulus_valuesObject



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

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

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/vident/view_component/base.rb', line 44

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