Module: LiveCableHelper

Defined in:
app/helpers/live_cable_helper.rb

Instance Method Summary collapse

Instance Method Details

#live(component, id:, **defaults) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/live_cable_helper.rb', line 32

def live(component, id:, **defaults)
  unless component.is_a?(String)
    raise LiveCable::Error, '`live` helper only accepts string component names. Use render(component) directly if ' \
                            'you have a component instance.'
  end

  id = LiveCable::Component.resolve_id(id)

  live_id = "#{component}/#{id}"

  component = render_context&.get_component(live_id) || LiveCable.instance_from_string(component, id)
  component.defaults = defaults

  render(component)
end

#render_part(index) ⇒ Object



25
26
27
28
29
30
# File 'app/helpers/live_cable_helper.rb', line 25

def render_part(index, &)
  ctx = render_context
  return yield unless ctx

  ctx.render_part(index, &)
end

#with_render_context(component) ⇒ Object

Parameters:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/live_cable_helper.rb', line 5

def with_render_context(component, &)
  # Add the current component to the parent context before making a new context
  render_context&.add_component(component)

  # If we had a parent with a live connection, we're connected, so apply defaults now, if not
  # then we apply them to the pre-render container
  component.apply_defaults

  context = LiveCable::RenderContext.new(component, root: context_stack.first)
  context_stack.push(context)

  begin
    value = yield
  ensure
    context_stack.pop
  end

  [value, context]
end