Class: LiveRecord::ChangesChannel
- Inherits:
-
BaseChannel
- Object
- ActionCable::Channel::Base
- BaseChannel
- LiveRecord::ChangesChannel
- Defined in:
- app/channels/live_record/changes_channel.rb
Overview
This channel streams changes (update/destroy) from records to connected clients, through ActiveRecord callbacks This also supports syncing (old changes) when a client somehow got disconnected (i.e. network problems), through a separate cache ‘live_record_updates` table.
Instance Method Summary collapse
Instance Method Details
#subscribed ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/channels/live_record/changes_channel.rb', line 6 def subscribed find_record_from_params(params) do |record| = (record, current_user) if .present? stream_for record, coder: ActiveSupport::JSON do || begin record.reload rescue ActiveRecord::RecordNotFound end = (record, current_user) # if not just :id if .size > 1 response = (, ) transmit response if response.present? else respond_with_error(:forbidden) reject_subscription end end else respond_with_error(:forbidden) reject end end end |
#sync_record(data) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/channels/live_record/changes_channel.rb', line 35 def sync_record(data) find_record_from_params(data.symbolize_keys) do |record| = (record, current_user) # if not just :id if .size > 1 live_record_update = LiveRecordUpdate.where( recordable_type: record.class.name, recordable_id: record.id ).where( 'created_at >= ?', DateTime.parse(data['stale_since']) - LiveRecord.configuration.sync_record_buffer_time ).order(id: :asc) if live_record_update.exists? = { 'action' => 'update', 'attributes' => record.attributes } response = (, ) transmit response if response.present? end else respond_with_error(:forbidden) reject_subscription end end end |