BigW Consortium Gitlab

variables_controller.rb 1.58 KB
Newer Older
Shinya Maeda committed
1 2 3 4 5 6 7 8 9 10 11 12 13
module Groups
  class VariablesController < Groups::ApplicationController
    before_action :variable, only: [:show, :update, :destroy]
    before_action :authorize_admin_build!

    def index
      redirect_to group_settings_ci_cd_path(group)
    end

    def show
    end

    def update
14
      if variable.update(variable_params)
Shinya Maeda committed
15 16 17 18 19 20 21 22
        redirect_to group_variables_path(group),
                    notice: 'Variable was successfully updated.'
      else
        render "show"
      end
    end

    def create
23
      @variable = group.variables.create(variable_params)
Shinya Maeda committed
24
        .present(current_user: current_user)
Shinya Maeda committed
25

Shinya Maeda committed
26
      if @variable.persisted?
Shinya Maeda committed
27
        redirect_to group_settings_ci_cd_path(group),
Shinya Maeda committed
28
                    notice: 'Variable was successfully created.'
Shinya Maeda committed
29 30 31 32 33 34
      else
        render "show"
      end
    end

    def destroy
Shinya Maeda committed
35 36 37 38 39 40 41
      if variable.destroy
        redirect_to group_settings_ci_cd_path(group),
                    status: 302,
                    notice: 'Variable was successfully removed.'
      else
        redirect_to group_settings_ci_cd_path(group),
                    status: 302,
Shinya Maeda committed
42
                    notice: 'Failed to remove the variable.'
Shinya Maeda committed
43
      end
Shinya Maeda committed
44 45 46 47
    end

    private

48 49
    def variable_params
      params.require(:variable).permit(*variable_params_attributes)
Shinya Maeda committed
50 51
    end

52 53
    def variable_params_attributes
      %i[key value protected]
Shinya Maeda committed
54 55 56 57 58
    end

    def variable
      @variable ||= group.variables.find(params[:id]).present(current_user: current_user)
    end
59 60 61 62

    def authorize_admin_build!
      return render_404 unless can?(current_user, :admin_build, group)
    end
Shinya Maeda committed
63 64
  end
end