BigW Consortium Gitlab

environments_controller.rb 1.2 KB
Newer Older
Kamil Trzcinski committed
1 2
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
3
  before_action :authorize_read_environment!
4 5
  before_action :authorize_create_environment!, only: [:new, :create]
  before_action :authorize_update_environment!, only: [:destroy]
Kamil Trzcinski committed
6
  before_action :environment, only: [:show, :destroy]
Kamil Trzcinski committed
7 8

  def index
9
    @environments = project.environments
Kamil Trzcinski committed
10 11 12
  end

  def show
13
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzcinski committed
14 15 16 17 18 19 20 21
  end

  def new
    @environment = project.environments.new
  end

  def create
    @environment = project.environments.create(create_params)
22 23 24 25

    if @environment.persisted?
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
Kamil Trzcinski committed
26 27 28 29 30 31
      render 'new'
    end
  end

  def destroy
    if @environment.destroy
32
      flash[:notice] = 'Environment was successfully removed.'
Kamil Trzcinski committed
33
    else
34
      flash[:alert] = 'Failed to remove environment.'
Kamil Trzcinski committed
35
    end
36 37

    redirect_to namespace_project_environments_path(project.namespace, project)
Kamil Trzcinski committed
38 39
  end

40 41
  private

Kamil Trzcinski committed
42 43 44 45
  def create_params
    params.require(:environment).permit(:name)
  end

46
  def environment
47
    @environment ||= project.environments.find(params[:id])
Kamil Trzcinski committed
48 49
  end
end