variables_controller.rb 1.43 KB
Newer Older
1
class Projects::VariablesController < Projects::ApplicationController
Shinya Maeda's avatar
Shinya Maeda committed
2
  before_action :variable, only: [:show, :update, :destroy]
3
  before_action :authorize_admin_build!
4 5 6

  layout 'project_settings'

7
  def index
8
    redirect_to project_settings_ci_cd_path(@project)
9 10
  end

11 12 13 14
  def show
  end

  def update
15
    if variable.update(variable_params)
16
      redirect_to project_variables_path(project),
17
                  notice: 'Variable was successfully updated.'
Phil Hughes's avatar
Phil Hughes committed
18
    else
Shinya Maeda's avatar
Shinya Maeda committed
19
      render "show"
Phil Hughes's avatar
Phil Hughes committed
20 21 22 23
    end
  end

  def create
24
    @variable = project.variables.create(variable_params)
Shinya Maeda's avatar
Shinya Maeda committed
25
      .present(current_user: current_user)
Phil Hughes's avatar
Phil Hughes committed
26

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

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

47 48
  private

49 50 51 52 53 54
  def variable_params
    params.require(:variable).permit(*variable_params_attributes)
  end

  def variable_params_attributes
    %i[id key value protected _destroy]
Lin Jen-Shin's avatar
Lin Jen-Shin committed
55 56
  end

Shinya Maeda's avatar
Shinya Maeda committed
57 58
  def variable
    @variable ||= project.variables.find(params[:id]).present(current_user: current_user)
59 60
  end
end