Module: ActionCableNotifications::Callbacks

Extended by:
ActiveSupport::Concern
Defined in:
lib/action_cable_notifications/callbacks.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#notify_createObject

Broadcast notifications when a new record is created



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/action_cable_notifications/callbacks.rb', line 76

def notify_create
  self.ActionCableNotificationsOptions.each do |broadcasting, options|
    if options[:actions].include? :create
      # Checks if record is within scope before broadcasting
      if self.class.scoped_collection(options[:scope]).where(id: self.id)
        ActionCable.server.broadcast broadcasting,
          collection: self.model_name.collection,
          msg: 'added',
          id: self.id,
          data: self
      end
    end
  end
end

#notify_destroyObject

Broadcast notifications when a record is destroyed.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/action_cable_notifications/callbacks.rb', line 124

def notify_destroy
  self.ActionCableNotificationsOptions.each do |broadcasting, options|
    if options[:actions].include? :destroy
      # Checks if record is within scope before broadcasting
      if self.class.scoped_collection(options[:scope]).where(id: self.id)
        ActionCable.server.broadcast broadcasting,
          collection: self.model_name.collection,
          msg: 'removed',
          id: self.id
      end
    end
  end
end

#notify_updateObject

Broadcast notifications when a record is updated. Only changed field will be sent.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/action_cable_notifications/callbacks.rb', line 95

def notify_update
  changes = {}
  self.changes.each do |k,v|
    changes[k] = v[1]
  end

  self.ActionCableNotificationsOptions.each do |broadcasting, options|
    if options[:actions].include? :update
      # Checks if record is within scope before broadcasting
      if self.class.scoped_collection(options[:scope]).where(id: self.id)
        # XXX: Performance required. For small data sets this should be
        # fast enough, but for large data sets this could be slow. As
        # clients should have a limited subset of the dataset loaded at a
        # time, caching the results already sent to clients in server memory
        # should not have a big impact in memory usage but can improve
        # performance for large data sets where only a sub
        ActionCable.server.broadcast broadcasting,
          collection: self.model_name.collection,
          msg: 'changed',
          id: self.id,
          data: changes
      end
    end
  end
end