Module: ActionCableNotifications::Channel::Actions

Included in:
ActionCableNotifications::Channel
Defined in:
lib/action_cable_notifications/channel_actions.rb

Instance Method Summary collapse

Instance Method Details

#create(data) ⇒ Object

Creates one record in the DB



45
46
47
48
49
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
# File 'lib/action_cable_notifications/channel_actions.rb', line 45

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.message
    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.full_messages
    }

    # Send error notification to the client
    transmit_packet response
  end

end

#destroy(data) ⇒ Object

Remove records from the DB



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
# File 'lib/action_cable_notifications/channel_actions.rb', line 121

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.message
    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.full_messages
    }

    # 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

Parameters:

  • selector (Hash)

    Specifies conditions that the registers should match



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
# 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
    results = data[:model].
              select(params[:select] || []).
              limit(params[:limit]).
              where(params[:where] || {}).
              scoped_collection(data[:model_options][: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.message
    }
  end

  # Send data to the client
  transmit_packet response, data[:options]
end

#update(data) ⇒ Object

Update one record from the DB



85
86
87
88
89
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
# File 'lib/action_cable_notifications/channel_actions.rb', line 85

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.message
    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.full_messages
    }

    # Send error notification to the client
    transmit_packet response
  end

end