Module: ActionCableNotifications::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/action_cable_notifications/model.rb
Instance Method Summary collapse
-
#notify_create ⇒ Object
Broadcast notifications when a new record is created.
-
#notify_destroy ⇒ Object
Broadcast notifications when a record is destroyed.
-
#notify_update ⇒ Object
Broadcast notifications when a record is updated.
- #prepare_update ⇒ Object
Instance Method Details
#notify_create ⇒ Object
Broadcast notifications when a new record is created
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/action_cable_notifications/model.rb', line 74 def notify_create self.ChannelPublications.each do |publication, | if [:actions].include? :create # Checks if record is within scope before broadcasting if [:scope]==:all or self.class.scoped_collection([:scope]).where(id: self.id).present? ActionCable.server.broadcast publication, msg: 'create', id: self.id, data: self end end end end |
#notify_destroy ⇒ Object
Broadcast notifications when a record is destroyed.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/action_cable_notifications/model.rb', line 172 def notify_destroy self.ChannelPublications.each do |publication, | if [:scope]==:all or [:actions].include? :destroy # Checks if record is within scope before broadcasting if [:scope]==:all or self.class.scoped_collection([:scope]).where(id: self.id).present? ActionCable.server.broadcast publication, msg: 'destroy', id: self.id end end end end |
#notify_update ⇒ Object
Broadcast notifications when a record is updated. Only changed fields will be sent if they are within configured scope
107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 |
# File 'lib/action_cable_notifications/model.rb', line 107 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, | if [:actions].include? :update # Checks if previous record was within scope record = [:records].detect{|r| r.id==self.id} was_in_scope = record.present? [:records].delete(record) if was_in_scope # Checks if current record is within scope if [:track_scope_changes]==true is_in_scope = false if [:scope]==:all record = self is_in_scope = true else record = self.class.scoped_collection([:scope]).where(id: self.id) if record.present? record = record.first 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_update ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/action_cable_notifications/model.rb', line 88 def prepare_update self.ChannelPublications.each do |publication, | if [:actions].include? :update if [:scope]==:all [:records].push self else record = self.class.scoped_collection([:scope]).where(id: self.id) if record.present? [:records].push record.first end end end end end |