Sha256: 6d2fd7826e96652676e33b736f522200ce081d9d7feafcc8002a4ab910488476

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

module LiveRecord
  module Model
    module Callbacks
      extend ActiveSupport::Concern

      included do
        before_update :__live_record_reference_changed_attributes__
        after_update_commit :__live_record_broadcast_record_update__
        after_destroy_commit :__live_record_broadcast_record_destroy__
        after_create_commit :__live_record_broadcast_record_create__

        def self.live_record_whitelisted_attributes(record, current_user)
          []
        end

        private

        def __live_record_reference_changed_attributes__
          @_live_record_changed_attributes = changed
        end

        def __live_record_broadcast_record_update__
          included_attributes = attributes.slice(*@_live_record_changed_attributes)
          @_live_record_changed_attributes = nil
          message_data = { 'action' => 'update', 'attributes' => included_attributes }
          LiveRecord::ChangesChannel.broadcast_to(self, message_data)
          LiveRecordUpdate.create!(recordable_type: self.class, recordable_id: self.id, created_at: DateTime.now)
        end

        def __live_record_broadcast_record_destroy__
          message_data = { 'action' => 'destroy' }
          LiveRecord::ChangesChannel.broadcast_to(self, message_data)
        end

        def __live_record_broadcast_record_create__
          message_data = { 'action' => 'create', 'attributes' => attributes }
          ActionCable.server.broadcast "live_record:publications:#{self.class.name.underscore}", message_data
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
live_record-0.2.8 lib/live_record/model/callbacks.rb
live_record-0.2.7 lib/live_record/model/callbacks.rb
live_record-0.2.6 lib/live_record/model/callbacks.rb
live_record-0.2.5 lib/live_record/model/callbacks.rb
live_record-0.2.4 lib/live_record/model/callbacks.rb
live_record-0.2.3 lib/live_record/model/callbacks.rb
live_record-0.2.2 lib/live_record/model/callbacks.rb
live_record-0.2.1 lib/live_record/model/callbacks.rb
live_record-0.2.0 lib/live_record/model/callbacks.rb