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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 50 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', command: data[:command], error: error || record.errors. } # Send error notification to the client transmit_packet response end end |
#destroy(data) ⇒ Object
Remove records from the DB
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 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 126 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', command: data[:command], error: error || record.errors. } # Send error notification to the client transmit_packet 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 begin temp_scope = data[:model_options][:scope].deep_dup || {} if (data[:model_options][:scope].is_a? Hash) && (data[:model_options][:scope][:where].is_a? Hash) #temp_scope = temp_scope.merge(params) temp_scope[:where].each{|k,v| v.is_a?(Proc) ? temp_scope[:where][k]=v.call() : nil } end results = data[:model]. select(params[:select] || []). limit(params[:limit]). where(params[:where] || {}). scoped_collection(temp_scope). to_a() rescue [] response = { publication: data[:publication], msg: 'upsert_many', data: results } rescue Exception => e response = { publication: data[:publication], collection: data[:model].model_name.collection, msg: 'error', command: data[:command], error: e. } end # Send data to the client transmit_packet response, data[:options] end |
#update(data) ⇒ Object
Update one record from the DB
90 91 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 119 120 121 |
# File 'lib/action_cable_notifications/channel_actions.rb', line 90 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', command: data[:command], error: error || record.errors. } # Send error notification to the client transmit_packet response end end |