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[*%i[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



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_timeObject



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_timeObject



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_pathObject



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_pathObject



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_pathObject



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_actionObject 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_actionsObject



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

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

#as_stimulus_classObject 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_classesObject



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

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

#as_stimulus_controllerObject 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_controllersObject



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

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

#as_stimulus_outletObject 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_outletsObject



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

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

#as_stimulus_paramObject 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_paramsObject



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

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

#as_stimulus_targetObject 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_targetsObject



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

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

#as_stimulus_valueObject 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_valuesObject



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

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

#before_renderObject

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
  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