aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/resource_types_controller.rb
blob: fdfb7915bfa8752c3f3c8e4ee0a79080da533ad2 (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
78
class ResourceTypesController < ApplicationController
  before_action :set_resource_type, only: %i[ show edit update destroy ]
  before_action :require_admin, only: %i[ new create update destroy ]

  # GET /resource_types or /resource_types.json
  def index
    @resource_types = ResourceType.all
  end

  # GET /resource_types/1 or /resource_types/1.json
  def show
  end

  # GET /resource_types/new
  def new
    @resource_type = ResourceType.new
  end

  # GET /resource_types/1/edit
  def edit
  end

  # POST /resource_types or /resource_types.json
  def create
    @resource_type = ResourceType.new(resource_type_params)

    respond_to do |format|
      if @resource_type.save
        format.html { redirect_to @resource_type, notice: "Resource type was successfully created." }
        format.json { render :show, status: :created, location: @resource_type }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @resource_type.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /resource_types/1 or /resource_types/1.json
  def update
    respond_to do |format|
      if @resource_type.update(resource_type_params)
        format.html { redirect_to @resource_type, notice: "Resource type was successfully updated." }
        format.json { render :show, status: :ok, location: @resource_type }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @resource_type.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /resource_types/1 or /resource_types/1.json
  def destroy
    @resource_type.destroy!

    respond_to do |format|
      format.html { redirect_to resource_types_path, status: :see_other, notice: "Resource type was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_resource_type
      @resource_type = ResourceType.find(params.expect(:id))
    end

    # Only allow a list of trusted parameters through.
    def resource_type_params
      params.expect(resource_type: [ :name ])
    end

  # must be admin!
  def require_admin
    unless Current.session.user&.is_admin
      redirect_to root_path, notice: "You must be an admin to perform this action."
    end
  end
end