Module: ActiveSync::ActiveRecordExtension

Extended by:
ActiveSupport::Concern
Defined in:
app/models/active_sync/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#sync_changeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/active_sync/active_record_extension.rb', line 16

def sync_change
  if ActiveSync::Sync.is_sync_model? self.class
    #TODO properly accommodate multi process environment, since sync sync_subscriptions
    # exists only in one process, if one process has a filter sub but another does not
    # not all users will necessarily get broadcast to.
    ActionCable.server.broadcast("#{self.class}_All", ActiveSync::Sync.sync_record( self ) )
    self.class.sync_record_subscriptions.each do | stream, filter |
      unless filter[:IsReference]

        match = true
        filter.each do | key, value |
          unless self.send( key ) == value
            match = false
            break
          end
        end

        ActionCable.server.broadcast( stream, ActiveSync::Sync.sync_record( self ) ) if match

      else

        model_association = ActiveSync::Sync.get_model_association( filter[:subscribed_model], filter[:association_name] )

        record = filter[:subscribed_model].find( filter[:record_id] )

        if defined? record.send( model_association[:name] ).pluck

          referenced = record.send( model_association[:name] ).pluck(:id).include? id
        else
          referenced = record.send( model_association[:name] ).id == id
        end

        if referenced
          ActionCable.server.broadcast( stream, ActiveSync::Sync.association_record( model_association, record ))
        end
      end
    end
  end
end

#sync_updateObject



12
13
14
# File 'app/models/active_sync/active_record_extension.rb', line 12

def sync_update
  sync_change if saved_changes?
end