Module: ActionCableNotifications::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/action_cable_notifications/model.rb
Defined Under Namespace
Modules: ClassMethods
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.
Instance Method Details
#notify_create ⇒ Object
Broadcast notifications when a new record is created
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/action_cable_notifications/model.rb', line 73 def notify_create self.ActionCableNotificationsOptions.each do |broadcasting, | if [:actions].include? :create # Checks if record is within scope before broadcasting if self.class.scoped_collection([:scope]).where(id: self.id) ActionCable.server.broadcast broadcasting, collection: self.model_name.collection, msg: 'create', id: self.id, data: self end end end end |
#notify_destroy ⇒ Object
Broadcast notifications when a record is destroyed.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/action_cable_notifications/model.rb', line 123 def notify_destroy self.ActionCableNotificationsOptions.each do |broadcasting, | if [:actions].include? :destroy # Checks if record is within scope before broadcasting if self.class.scoped_collection([:scope]).where(id: self.id) ActionCable.server.broadcast broadcasting, collection: self.model_name.collection, msg: 'destroy', id: self.id end end end end |
#notify_update ⇒ Object
Broadcast notifications when a record is updated. Only changed field will be sent.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/action_cable_notifications/model.rb', line 92 def notify_update changes = {} self.changes.each do |k,v| changes[k] = v[1] end if !changes.empty? self.ActionCableNotificationsOptions.each do |broadcasting, | if [:actions].include? :update # Checks if record is within scope before broadcasting if self.class.scoped_collection([: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: 'update', id: self.id, data: changes end end end end end |