about summary refs log tree commit diff
path: root/app/controllers/admin/rules_controller.rb
blob: f3bed3ad8e79f19f9a428d113af365227e30cf48 (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
# frozen_string_literal: true

module Admin
  class RulesController < BaseController
    before_action :set_rule, except: [:index, :create]

    def index
      authorize :rule, :index?

      @rules = Rule.ordered
      @rule  = Rule.new
    end

    def create
      authorize :rule, :create?

      @rule = Rule.new(resource_params)

      if @rule.save
        redirect_to admin_rules_path
      else
        @rules = Rule.ordered
        render :index
      end
    end

    def edit
      authorize @rule, :update?
    end

    def update
      authorize @rule, :update?

      if @rule.update(resource_params)
        redirect_to admin_rules_path
      else
        render :edit
      end
    end

    def destroy
      authorize @rule, :destroy?

      @rule.discard

      redirect_to admin_rules_path
    end

    private

    def set_rule
      @rule = Rule.find(params[:id])
    end

    def resource_params
      params.require(:rule).permit(:text, :priority)
    end
  end
end