Module: LiveRecord::Channel::Implement

Included in:
LiveRecordChannel
Defined in:
lib/live_record/channel/implement.rb

Instance Method Summary collapse

Instance Method Details

#subscribedObject



4
5
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
# File 'lib/live_record/channel/implement.rb', line 4

def subscribed
  find_record_from_params(params) do |record|
    authorised_attributes = authorised_attributes(record, current_user)

    if authorised_attributes.present?
      stream_for record, coder: ActiveSupport::JSON do |message|
        begin
          record.reload
        rescue ActiveRecord::RecordNotFound
        end
        
        authorised_attributes = authorised_attributes(record, current_user)

        if authorised_attributes.present?
          response = filtered_message(message, authorised_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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/live_record/channel/implement.rb', line 32

def sync_record(data)
  find_record_from_params(data.symbolize_keys) do |record|
    authorised_attributes = authorised_attributes(record, current_user)

    if authorised_attributes.present?
      live_record_update = LiveRecordUpdate.where(
        recordable_type: record.class.name,
        recordable_id: record.id
      ).where(
        'created_at >= ?', DateTime.parse(data['stale_since']) - 1.minute
      ).order(id: :asc)

      if live_record_update.exists?
        message = { 'action' => 'update', 'attributes' => record.attributes }
        response = filtered_message(message, authorised_attributes)
        transmit response if response.present?
      end
    else
      respond_with_error(:forbidden)
      reject_subscription
    end
  end
end

#unsubscribedObject



56
57
58
# File 'lib/live_record/channel/implement.rb', line 56

def unsubscribed
  # Any cleanup needed when channel is unsubscribed
end