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 |
# File 'app/channels/live_record/changes_channel.rb', line 6 def subscribed find_record_from_params(params) do |record| whitelisted_attributes = LiveRecord::BaseChannel::Helpers.whitelisted_attributes(record, current_user) if whitelisted_attributes.present? stream_for record, coder: ActiveSupport::JSON do || begin record.reload rescue ActiveRecord::RecordNotFound end whitelisted_attributes = LiveRecord::BaseChannel::Helpers.whitelisted_attributes(record, current_user) if whitelisted_attributes.size > 0 response = (, whitelisted_attributes) 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/channels/live_record/changes_channel.rb', line 34 def sync_record(data) params = data.symbolize_keys find_record_from_params(params) do |record| whitelisted_attributes = LiveRecord::BaseChannel::Helpers.whitelisted_attributes(record, current_user) if whitelisted_attributes.size > 0 live_record_updates = nil if params[:stale_since].present? live_record_updates = LiveRecordUpdate.where( recordable_type: record.class.name, recordable_id: record.id ).where( 'created_at >= ?', DateTime.parse(params[:stale_since]) - LiveRecord.configuration.sync_record_buffer_time ) end # if stale_since is unknown, or there is a live_record_update that has happened while disconnected, # then we update the record in the client-side if params[:stale_since].blank? || live_record_updates.exists? = { 'action' => 'update', 'attributes' => record.attributes } response = (, whitelisted_attributes) transmit response if response.present? end else respond_with_error(:forbidden) reject_subscription end end end |