summaryrefslogtreecommitdiff
path: root/app/controllers/agents/events_controller.rb
blob: ef04814dd0f2fcc8c8069ba33921f6564c72f0a6 (plain)
1
2
3
4
5
6
7
8
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
module Agents
  class EventsController < ApplicationController
    before_action :load_agent

    def index
      @events = @agent.events.order(created_at: :desc)
    end

    def new
      @event = Event.new
    end

    def create
      publish(EventMessage.new(
        agent_id: @agent.id,
        event_type: event_params[:type],
        data: event_params[:data]
      ))
      redirect_to agent_events_url, notice: 'Event was successfully created.'
    end

    def destroy
      @event = Event.find(params[:id])
      @event.destroy
      redirect_to agent_events_url(@agent), notice: 'Event was successfully destroyed.'
    end

    private

    def event_params
      params[:event]
    end

    def load_agent
      @agent = Agent.find(params[:agent_id])
    end
  end
end