diff --git a/app/controllers/contract_templates_controller.rb b/app/controllers/contract_templates_controller.rb index 3ebc3ff..404ea31 100644 --- a/app/controllers/contract_templates_controller.rb +++ b/app/controllers/contract_templates_controller.rb @@ -5,8 +5,9 @@ class ContractTemplatesController < ApplicationController layout 'project' - before_action :set_project, except: [:destroy, :edit] - before_action :set_contract_template, only: [:destroy, :edit] + 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 @@ -33,7 +34,21 @@ class ContractTemplatesController < ApplicationController end def edit - @project = @contract_template.project + @release_type = @contract_template.release_type + end + + def update + unless @contract_template.editable? + redirect_to([@project, :contract_templates], alert: t('.error')) and return + end + + @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 @@ -43,6 +58,10 @@ class ContractTemplatesController < ApplicationController 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 diff --git a/app/models/contract_template.rb b/app/models/contract_template.rb index ba2ce3d..e05ed72 100644 --- a/app/models/contract_template.rb +++ b/app/models/contract_template.rb @@ -69,9 +69,13 @@ class ContractTemplate < ApplicationRecord (1..NUMBER_OF_CUSTOM_FIELDS).any? { |n| public_send("question_#{n}_text").presence } end + def editable? + releases.size.zero? + end + def attributes result = super() - result[:signature_legal_text] = signature_legal_text.as_json + result[:signature_legal_text] = signature_legal_text.as_json result end end diff --git a/app/policies/contract_template_policy.rb b/app/policies/contract_template_policy.rb index 6739e3c..3c20bfe 100644 --- a/app/policies/contract_template_policy.rb +++ b/app/policies/contract_template_policy.rb @@ -27,6 +27,10 @@ class ContractTemplatePolicy < ApplicationPolicy create? end + def update? + edit? + end + def import? if user.account_manager? record.project.account = user.account diff --git a/app/views/contract_templates/_contract_template.html.erb b/app/views/contract_templates/_contract_template.html.erb index f6d0934..203d8cc 100644 --- a/app/views/contract_templates/_contract_template.html.erb +++ b/app/views/contract_templates/_contract_template.html.erb @@ -19,7 +19,7 @@