Files
old-holivud2/app/controllers/contract_templates_controller.rb

108 lines
3.0 KiB
Ruby

# frozen_string_literal: true
class ContractTemplatesController < ApplicationController
include ProjectContext
layout 'project'
before_action :set_project, except: [:destroy, :edit, :update]
before_action :set_contract_template, only: [:destroy, :edit, :update]
before_action :set_project_from_contract_template, only: [:edit, :update]
before_action :show_splash_screen, only: :index
def index
@contract_templates = contract_templates.non_archived.order_by_name.paginate(page: params[:page])
end
def new
@contract_template = build_contract_template
end
def create
@contract_template = build_contract_template(contract_template_params)
if params[:commit] == 'preview'
ctp = ContractTemplatePreview.new(@contract_template)
releasable_instance = ctp.build_releasable
contract = Contract.new releasable_instance, true
send_file contract.to_pdf, download_attributes
elsif @contract_template.save
redirect_to [@project, :contract_templates], notice: t('.notice')
else
@release_type = contract_template_params[:release_type]
render :new
end
end
def edit
@release_type = @contract_template.release_type
end
def update
@contract_template.attributes = contract_template_params
if @contract_template.save
redirect_to [@project, :contract_templates], notice: t('.notice')
else
render :edit
end
end
def destroy
@contract_template.archive
redirect_to [@contract_template.project, :contract_templates], alert: t('.archived_notice')
end
private
def set_project_from_contract_template
@project = @contract_template.project
end
def show_splash_screen
render :splash if contract_templates.non_archived.count.zero?
end
def set_contract_template
@contract_template = authorize contract_templates.find(params[:id])
end
def contract_templates
if @project
policy_scope(@project.contract_templates)
else
policy_scope(ContractTemplate)
end
end
def build_contract_template(attrs = {})
authorize contract_templates.build(attrs)
end
def contract_template_params
params
.require(:contract_template)
.permit(:name, :release_type, :body, :guardian_clause,
:signature_legal_text, :fee, :amendment_clause,
:applicable_medium_id, :applicable_medium_text,
:territory_id, :territory_text,
:term_id, :term_text, :accessibility,
:restriction_id, :restriction_text,
:question_1_text, :question_2_text,
:question_3_text, :question_4_text,
:question_5_text, :question_6_text,
:question_7_text, :question_8_text,
:question_9_text, :question_10_text,
:question_11_text, :question_12_text,
:question_13_text, :question_14_text,
:question_15_text)
end
def download_attributes
{
disposition: 'inline',
filename: 'contract-preview.pdf',
type: 'application/pdf'
}
end
end