Skip to content
environments_controller.rb 1.63 KiB
Newer Older
Kamil Trzciński's avatar
Kamil Trzciński committed
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
  before_action :authorize_read_environment!
Kamil Trzciński's avatar
Kamil Trzciński committed
  before_action :authorize_create_environment!, only: [:new, :create]
Kamil Trzciński's avatar
Kamil Trzciński committed
  before_action :authorize_create_deployment!, only: [:stop]
  before_action :authorize_update_environment!, only: [:edit, :update]
  before_action :environment, only: [:show, :edit, :update, :stop]
Kamil Trzciński's avatar
Kamil Trzciński committed

  def index
    @scope = params[:scope]
    @all_environments = project.environments
    @environments =
      if @scope == 'stopped'
        @all_environments.stopped
      else
        @all_environments.available
      end
Kamil Trzciński's avatar
Kamil Trzciński committed
  end

  def show
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzciński's avatar
Kamil Trzciński committed
  end

  def new
    @environment = project.environments.new
  end

Kamil Trzciński's avatar
Kamil Trzciński committed
  def create
    @environment = project.environments.create(environment_params)

    if @environment.persisted?
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
      render :edit
    return render_404 unless @environment.stoppable?

    new_action = @environment.stop!(current_user)
    redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, new_action])
Kamil Trzciński's avatar
Kamil Trzciński committed
  end

  def environment_params
    params.require(:environment).permit(:name, :external_url)
  def environment
    @environment ||= project.environments.find(params[:id])
Kamil Trzciński's avatar
Kamil Trzciński committed
  end
end