Class: ViewComponent::Live::LiveUpdater

Inherits:
Object
  • Object
show all
Defined in:
app/models/view_component/live/live_updater.rb

Class Method Summary collapse

Class Method Details

.subscribe(model, component_name) ⇒ Object



16
17
18
19
# File 'app/models/view_component/live/live_updater.rb', line 16

def subscribe(model, component_name)
  subscriptions[model] ||= []
  subscriptions[model] << component_name
end

.subscriptionsObject



21
22
23
# File 'app/models/view_component/live/live_updater.rb', line 21

def subscriptions
  @subscriptions ||= {}
end

.update(model) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'app/models/view_component/live/live_updater.rb', line 5

def update(model)
  model_name = model.class.name
  return unless subscriptions[model_name].present?

  subscriptions[model_name].each do |component_name|
    locals = {model_name.underscore.to_sym => model}
    body = ApplicationController.render template: 'live', layout: false, locals: {component: component_name, locals: locals}
    LiveChannel.broadcast_to "#{component_name}:#{model.id}", body: body
  end
end