Module: ViewComponentReducible::Component

Defined in:
lib/view_component_reducible/component.rb

Overview

Include to enable the component state DSL and helpers.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#vcr_envelopeHash? (readonly)

Returns:

  • (Hash, nil)


18
19
20
# File 'lib/view_component_reducible/component.rb', line 18

def vcr_envelope
  @vcr_envelope
end

#vcr_state_tokenString? (readonly)

Returns:

  • (String, nil)


20
21
22
# File 'lib/view_component_reducible/component.rb', line 20

def vcr_state_token
  @vcr_state_token
end

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Hook to include DSL and class methods.

Parameters:

  • base (Class)


12
13
14
15
# File 'lib/view_component_reducible/component.rb', line 12

def self.included(base)
  base.include(State::DSL)
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(vcr_envelope: nil, vcr_state_token: nil, **kwargs) ⇒ Object

Parameters:

  • vcr_envelope (Hash, nil) (defaults to: nil)
  • vcr_state_token (String, nil) (defaults to: nil)


24
25
26
27
28
29
30
# File 'lib/view_component_reducible/component.rb', line 24

def initialize(vcr_envelope: nil, vcr_state_token: nil, **kwargs)
  @vcr_envelope = vcr_envelope
  @vcr_state_token = vcr_state_token
  return unless defined?(super)

  kwargs.empty? ? super() : super(**kwargs)
end

#render_in(view_context, &block) ⇒ String

Render component markup wrapped in a VCR boundary when available.

Parameters:

  • view_context (ActionView::Base)

Returns:

  • (String)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/view_component_reducible/component.rb', line 51

def render_in(view_context, &block)
  ensure_vcr_state(view_context) if vcr_envelope.nil?

  path = vcr_envelope && vcr_envelope['path']
  previous_path = view_context.instance_variable_get(:@vcr_current_path)
  view_context.instance_variable_set(:@vcr_current_path, path)

  rendered = super
  return rendered if path.nil? || path.to_s.empty?

  inject_vcr_path(rendered, path)
ensure
  view_context.instance_variable_set(:@vcr_current_path, previous_path)
end

#vcr_dom_id(path:) ⇒ String

Optional DOM target id for updates.

Parameters:

  • path (String)

Returns:

  • (String)


44
45
46
# File 'lib/view_component_reducible/component.rb', line 44

def vcr_dom_id(path:)
  "vcr:#{self.class.vcr_id}:#{path}"
end

#vcr_stateHash{String=>Object}

Build state hash for rendering from the envelope.

Returns:

  • (Hash{String=>Object})


34
35
36
37
38
39
# File 'lib/view_component_reducible/component.rb', line 34

def vcr_state
  return {} if vcr_envelope.nil?

  schema = self.class.vcr_state_schema
  schema.build(vcr_envelope['data'])
end