about summary refs log tree commit diff
path: root/app/controllers/admin/webhooks_controller.rb
blob: d6fb1a4eaf3009833a0c9bb9c32b5419a4c6842c (plain) (blame)
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
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
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

module Admin
  class WebhooksController < BaseController
    before_action :set_webhook, except: [:index, :new, :create]

    def index
      authorize :webhook, :index?

      @webhooks = Webhook.page(params[:page])
    end

    def new
      authorize :webhook, :create?

      @webhook = Webhook.new
    end

    def create
      authorize :webhook, :create?

      @webhook = Webhook.new(resource_params)

      if @webhook.save
        redirect_to admin_webhook_path(@webhook)
      else
        render :new
      end
    end

    def show
      authorize @webhook, :show?
    end

    def edit
      authorize @webhook, :update?
    end

    def update
      authorize @webhook, :update?

      if @webhook.update(resource_params)
        redirect_to admin_webhook_path(@webhook)
      else
        render :show
      end
    end

    def enable
      authorize @webhook, :enable?
      @webhook.enable!
      redirect_to admin_webhook_path(@webhook)
    end

    def disable
      authorize @webhook, :disable?
      @webhook.disable!
      redirect_to admin_webhook_path(@webhook)
    end

    def destroy
      authorize @webhook, :destroy?
      @webhook.destroy!
      redirect_to admin_webhooks_path
    end

    private

    def set_webhook
      @webhook = Webhook.find(params[:id])
    end

    def resource_params
      params.require(:webhook).permit(:url, events: [])
    end
  end
end