prevent update if template is in use

This commit is contained in:
Bilal
2020-07-13 20:01:06 +02:00
parent cefa30b331
commit f44eca5328
7 changed files with 39 additions and 6 deletions

View File

@@ -5,8 +5,9 @@ class ContractTemplatesController < ApplicationController
layout 'project' layout 'project'
before_action :set_project, except: [:destroy, :edit] before_action :set_project, except: [:destroy, :edit, :update]
before_action :set_contract_template, only: [:destroy, :edit] 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 before_action :show_splash_screen, only: :index
def index def index
@@ -33,7 +34,21 @@ class ContractTemplatesController < ApplicationController
end end
def edit 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 end
def destroy def destroy
@@ -43,6 +58,10 @@ class ContractTemplatesController < ApplicationController
private private
def set_project_from_contract_template
@project = @contract_template.project
end
def show_splash_screen def show_splash_screen
render :splash if contract_templates.non_archived.count.zero? render :splash if contract_templates.non_archived.count.zero?
end end

View File

@@ -69,9 +69,13 @@ class ContractTemplate < ApplicationRecord
(1..NUMBER_OF_CUSTOM_FIELDS).any? { |n| public_send("question_#{n}_text").presence } (1..NUMBER_OF_CUSTOM_FIELDS).any? { |n| public_send("question_#{n}_text").presence }
end end
def editable?
releases.size.zero?
end
def attributes def attributes
result = super() result = super()
result[:signature_legal_text] = signature_legal_text.as_json result[:signature_legal_text] = signature_legal_text.as_json
result result
end end
end end

View File

@@ -27,6 +27,10 @@ class ContractTemplatePolicy < ApplicationPolicy
create? create?
end end
def update?
edit?
end
def import? def import?
if user.account_manager? if user.account_manager?
record.project.account = user.account record.project.account = user.account

View File

@@ -19,7 +19,7 @@
<div class="btn-group"> <div class="btn-group">
<%= button_tag t(".actions.manage"), class: "btn btn-light btn-sm dropdown-toggle border", data: { toggle: "dropdown", boundary: "window" }, aria: { haspopup: true, expanded: false } %> <%= button_tag t(".actions.manage"), class: "btn btn-light btn-sm dropdown-toggle border", data: { toggle: "dropdown", boundary: "window" }, aria: { haspopup: true, expanded: false } %>
<div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-menu dropdown-menu-right">
<% if policy(ContractTemplate).edit? %> <% if policy(ContractTemplate).edit? && contract_template.editable? %>
<%= link_to fa_icon("pencil fw", text: t(".actions.edit")), [:edit, contract_template], class: "dropdown-item" %> <%= link_to fa_icon("pencil fw", text: t(".actions.edit")), [:edit, contract_template], class: "dropdown-item" %>
<% end %> <% end %>
<% if policy(QrCode).show? %> <% if policy(QrCode).show? %>

View File

@@ -1,4 +1,4 @@
<%= bootstrap_form_with model: [project, contract_template], local: true do |form| %> <%= bootstrap_form_with model: contract_template, local: true do |form| %>
<%= field_set_tag content_tag(:span, t(".release_info.heading"), class: "h6 text-muted text-uppercase") do %> <%= field_set_tag content_tag(:span, t(".release_info.heading"), class: "h6 text-muted text-uppercase") do %>
<div class="form-row"> <div class="form-row">
<%= form.text_field :name, wrapper_class: "col-sm-6" %> <%= form.text_field :name, wrapper_class: "col-sm-6" %>

View File

@@ -300,6 +300,9 @@ en:
manage_large_audience: Easily manage large audiences with the crowd control feature manage_large_audience: Easily manage large audiences with the crowd control feature
print_QR_code: Print out release QR codes print_QR_code: Print out release QR codes
releases_automatically_organized: Releases are automatically organized as theyre submitted releases_automatically_organized: Releases are automatically organized as theyre submitted
update:
notice: The release template has been updated
error: The release template cannot be updated
contracts: contracts:
for_office_use_only: for_office_use_only:
description_labels: description_labels:

View File

@@ -134,6 +134,9 @@ es:
manage_large_audience: Easily manage large audiences with the crowd control feature (ES) manage_large_audience: Easily manage large audiences with the crowd control feature (ES)
print_QR_code: Print out release QR codes (ES) print_QR_code: Print out release QR codes (ES)
releases_automatically_organized: Releases are automatically organized as theyre submitted (ES) releases_automatically_organized: Releases are automatically organized as theyre submitted (ES)
update:
notice: The release template has been updated (ES)
error: The release template cannot be updated (ES)
contracts: contracts:
for_office_use_only: for_office_use_only:
description_labels: description_labels: