module ActionCableNotifications::Channel::Actions

Public Instance Methods

create(data) click to toggle source

Creates one record in the DB

# 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) click to toggle source

Remove records from the DB

# 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) click to toggle source

Fetch records from the DB and send them to the client

@param [Hash] selector Specifies conditions that the registers should match

# 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) click to toggle source

Update one record from the DB

# 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