Module: ActionCableNotifications::Channel::Actions
- Included in:
- ActionCableNotifications::Channel
- Defined in:
- lib/action_cable_notifications/channel_actions.rb
Instance Method Summary collapse
-
#create(data) ⇒ Object
Creates one record in the DB.
-
#destroy(data) ⇒ Object
Remove records from the DB.
-
#fetch(data) ⇒ Object
Fetch records from the DB and send them to the client.
-
#update(data) ⇒ Object
Update one record from the DB.
Instance Method Details
#create(data) ⇒ Object
Creates one record in the DB
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 34 def create(data) # XXX: Check if the client is allowed to call the method params = data[:params] || {} fields = params[:fields].except(:id) error = nil if fields.present? begin record = data[:model].create(fields) if !record.persisted? error = true end rescue Exception => e error = e. end else error = "No fields were provided" end if error response = { collection: data[:model].model_name.collection, msg: 'error', cmd: 'create', error: error || record.errors. } # Send error notification to the client transmit response end end |
#destroy(data) ⇒ Object
Remove records from the DB
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 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 110 def destroy(data) # XXX: Check if the client is allowed to call the method params = data[:params] || {} record = data[:model].find(params[:id]) rescue nil error = nil if record.present? begin record.destroy rescue Exception => e error = e. end else error = "There is no record with id: #{params[:id]}" end if error response = { collection: data[:model].model_name.collection, msg: 'error', cmd: 'destroy', error: error || record.errors. } # Send error notification to the client transmit response end end |
#fetch(data) ⇒ Object
Fetch records from the DB and send them to the client
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 9 def fetch(data) # XXX: Check if the client is allowed to call the method params = data[:params] || {} # Get results using provided parameters and model configured scope results = data[:model]. select(params[:select]). limit(params[:limit]). where(params[:where] || {}). scoped_collection(data[:model_options][:scope]). to_a() rescue [] response = { collection: data[:model].model_name.collection, msg: 'upsert_many', data: results } # Send data to the client transmit_packet response end |
#update(data) ⇒ Object
Update one record from the DB
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 74 def update(data) # XXX: Check if the client is allowed to call the method params = data[:params] || {} record = data[:model].find(params[:id]) rescue nil error = nil if record.present? begin record.update_attributes(params[:fields]) rescue Exception => e error = e. end else error = "There is no record with id: #{params[:id]}" end if error response = { collection: data[:model].model_name.collection, msg: 'error', cmd: 'update', error: error || record.errors. } # Send error notification to the client transmit response end end |