Method: LiveRecord::ChangesChannel#sync_record

Defined in:
app/channels/live_record/changes_channel.rb

#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?
        message = { 'action' => 'update', 'attributes' => record.attributes }
        response = filtered_message(message, whitelisted_attributes)
        transmit response if response.present?
      end
    else
      respond_with_error(:forbidden)
      reject_subscription
    end
  end
end