lib/action_cable_notifications/model.rb
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
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
Broadcast notifications when a record is updated. Only changed fields will be sent
 if they are within configured scope