Module: StimulusGridRails::Broadcastable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/stimulus_grid_rails/concerns/broadcastable.rb
Overview
Mixin for Active Record models backing a grid. Once included and wired with ‘broadcasts_grid`, every create / update / destroy AUTOMATICALLY broadcasts the right Turbo Stream action to the grid’s tenant-scoped stream — no manual broadcast calls anywhere:
create → row-insert-sorted (the full row as JSON)
update → cell (each changed registered column + any
computed column whose deps changed)
destroy → row-remove
Usage:
class Athlete < ApplicationRecord
include StimulusGridRails::Broadcastable
broadcasts_grid AthleteGrid
end
Streams are tenant-scoped via StimulusGridRails.streamables_for, so with ActsAsTenant a tenant’s changes never reach another tenant’s subscribers.
Instance Method Summary collapse
- #stimulus_grid_broadcast_changes ⇒ Object
- #stimulus_grid_broadcast_insert ⇒ Object
- #stimulus_grid_broadcast_remove ⇒ Object
- #stimulus_grid_streamables ⇒ Object
Instance Method Details
#stimulus_grid_broadcast_changes ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/stimulus_grid_rails/concerns/broadcastable.rb', line 59 def stimulus_grid_broadcast_changes grid_class = self.class.stimulus_grid_class grid = grid_class.new registry = grid_class.columns_registry || {} changed = previous_changes.keys.map(&:to_sym) direct = registry.values.select { |c| !c.computed? && !c.name.to_s.start_with?("_") && changed.include?(c.name) } computed = registry.values.select { |c| c.computed? && (c.depends_on & changed).any? } (direct + computed).each do |col| = StimulusGridRails::TurboStreams.cell( grid: grid_class.resource_name, row_id: id, column: col.name, value: grid.cell_value(self, col), optimistic_id: _sgr_optimistic_id, ) stimulus_grid_broadcast() end end |
#stimulus_grid_broadcast_insert ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/stimulus_grid_rails/concerns/broadcastable.rb', line 51 def stimulus_grid_broadcast_insert grid = self.class.stimulus_grid_class.new = StimulusGridRails::TurboStreams.row_insert_sorted( grid: grid.class.resource_name, row_id: id, payload: grid.row_to_json(self), ) stimulus_grid_broadcast() end |
#stimulus_grid_broadcast_remove ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/stimulus_grid_rails/concerns/broadcastable.rb', line 77 def stimulus_grid_broadcast_remove grid_class = self.class.stimulus_grid_class = StimulusGridRails::TurboStreams.row_remove( grid: grid_class.resource_name, row_id: id, ) stimulus_grid_broadcast() end |
#stimulus_grid_streamables ⇒ Object
47 48 49 |
# File 'lib/stimulus_grid_rails/concerns/broadcastable.rb', line 47 def stimulus_grid_streamables StimulusGridRails.streamables_for(self.class.stimulus_grid_class.resource_name) end |