pages_domains_controller.rb 1.67 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
class Projects::PagesDomainsController < Projects::ApplicationController
  layout 'project_settings'

6
  before_action :require_pages_enabled!
7
  before_action :authorize_update_pages!
8
  before_action :domain, except: [:new, :create]
9 10 11 12 13 14 15 16

  def show
  end

  def new
    @domain = @project.pages_domains.new
  end

17 18 19 20 21 22 23 24 25 26 27 28
  def verify
    result = VerifyPagesDomainService.new(@domain).execute

    if result[:status] == :success
      flash[:notice] = 'Successfully verified domain ownership'
    else
      flash[:alert] = 'Failed to verify domain ownership'
    end

    redirect_to project_pages_domain_path(@project, @domain)
  end

29 30 31
  def edit
  end

32
  def create
33
    @domain = @project.pages_domains.create(create_params)
34 35

    if @domain.valid?
36
      redirect_to project_pages_domain_path(@project, @domain)
37 38 39 40 41
    else
      render 'new'
    end
  end

42 43 44 45 46 47 48 49 50 51
  def update
    if @domain.update(update_params)
      redirect_to project_pages_path(@project),
        status: 302,
        notice: 'Domain was updated'
    else
      render 'edit'
    end
  end

52 53 54 55 56
  def destroy
    @domain.destroy

    respond_to do |format|
      format.html do
57
        redirect_to project_pages_path(@project),
58 59
                    status: 302,
                    notice: 'Domain was removed'
60 61 62 63 64 65 66
      end
      format.js
    end
  end

  private

67 68 69 70 71 72
  def create_params
    params.require(:pages_domain).permit(:key, :certificate, :domain)
  end

  def update_params
    params.require(:pages_domain).permit(:key, :certificate)
73 74
  end

75
  # rubocop: disable CodeReuse/ActiveRecord
76
  def domain
77
    @domain ||= @project.pages_domains.find_by!(domain: params[:id].to_s)
78
  end
79
  # rubocop: enable CodeReuse/ActiveRecord
80
end