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

#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