Module: ActionCableNotifications::Model

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

Instance Method Summary collapse

Instance Method Details

#notify_createObject

Broadcast notifications when a new record is created



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/action_cable_notifications/model.rb', line 84

def notify_create
  self.ChannelPublications.each do |publication, options|
    if options[:actions].include? :create
      # Checks if records is within scope before broadcasting
      records = self.class.scoped_collection(options[:scope])

      if options[:scope]==:all or record_within_scope(records)
        ActionCable.server.broadcast publication,
          {
            msg: 'create',
            id: self.id,
            data: self
          }
      end
    end
  end
end

#notify_destroyObject

Broadcast notifications when a record is destroyed.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/action_cable_notifications/model.rb', line 192

def notify_destroy
  self.ChannelPublications.each do |publication, options|
    if options[:scope]==:all or options[:actions].include? :destroy
      # Checks if record is within scope before broadcasting
      if options[:scope]==:all or record_within_scope(self.class.scoped_collection(options[:scope])).present?
        ActionCable.server.broadcast publication,
          {
            msg: 'destroy',
            id: self.id
          }
      end
    end
  end
end

#notify_updateObject

Broadcast notifications when a record is updated. Only changed fields will be sent if they are within configured scope



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/action_cable_notifications/model.rb', line 121

def notify_update
  # Get model changes
  if self.respond_to?(:saved_changes) # For Rails >= 5.1
    changes = self.saved_changes.transform_values(&:second)
  else # For Rails < 5.1
    changes = self.changes.transform_values(&:second)
  end

  # Checks if there are changes in the model
  if !changes.empty?
    self.ChannelPublications.each do |publication, options|
      if options[:actions].include? :update
        # Checks if previous record was within scope
        record = record_within_scope(options[:records])
        was_in_scope = record.present?

        options[:records].delete(record) if was_in_scope

        # Checks if current record is within scope
        if options[:track_scope_changes]==true
          is_in_scope = false
          if options[:scope]==:all
            record = self
            is_in_scope = true
          else
            record = record_within_scope(self.class.scoped_collection(options[:scope]))
            if record.present?
              is_in_scope = true
            end
          end
        else
          is_in_scope = was_in_scope
        end

        # Broadcasts notifications about model changes
        if is_in_scope
          if was_in_scope
            # Get model changes and applies them to the scoped collection record
            changes.select!{|k,v| record.respond_to?(k)}

            if !changes.empty?
              ActionCable.server.broadcast publication,
                {
                  msg: 'update',
                  id: self.id,
                  data: changes
                }
            end
          else
            ActionCable.server.broadcast publication,
              {
                msg: 'create',
                id: record.id,
                data: record
              }
          end
        elsif was_in_scope # checks if needs to delete the record if its no longer in scope
          ActionCable.server.broadcast publication,
            {
              msg: 'destroy',
              id: self.id
            }
        end
      end
    end
  end
end

#prepare_updateObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/action_cable_notifications/model.rb', line 102

def prepare_update
  self.ChannelPublications.each do |publication, options|
    if options[:actions].include? :update
      if options[:scope]==:all
        options[:records].push self
      else
        record = record_within_scope(self.class.scoped_collection(options[:scope]))
        if record.present?
          options[:records].push record
        end
      end
    end
  end
end