From 0ea5e5926fb04d1714ec27016c852f875f10b3f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Tollk=C3=BChn?= Date: Mon, 21 Jul 2025 16:00:37 +0200 Subject: [PATCH] check if clients are editable --- app/models/client.rb | 8 ++++ app/views/clients/_form.html.erb | 82 ++++++++++---------------------- 2 files changed, 32 insertions(+), 58 deletions(-) diff --git a/app/models/client.rb b/app/models/client.rb index 87d2f30..8bb8b75 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -5,4 +5,12 @@ class Client < ApplicationRecord validates :company_name, :firstname, :lastname, presence: true # Add other validations as needed + + def referenced_count + owner_subprojects.count + builder_subprojects.count + client_subprojects.count + end + + def editable? + referenced_count <= 1 + end end \ No newline at end of file diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb index e5f532f..ada555a 100644 --- a/app/views/clients/_form.html.erb +++ b/app/views/clients/_form.html.erb @@ -1,62 +1,28 @@ -<%= form_with(model: client) do |form| %> - <% if client.errors.any? %> -
-

<%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:

+<% if @client.editable? %> + <%= form_with(model: client) do |form| %> + <% if client.errors.any? %> +
+

<%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:

-
    - <% client.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
+
    + <% client.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + + <% [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone].each do |attrib|%> +
+ <%= form.label attrib, style: "display: block" %> + <%= form.text_field attrib %>
+
+ <% end %> + +
+ <%= form.submit %>
<% end %> - -
- <%= form.label :company_name, style: "display: block" %> - <%= form.text_field :company_name %> -
- -
- <%= form.label :firstname, style: "display: block" %> - <%= form.text_field :firstname %> -
- -
- <%= form.label :lastname, style: "display: block" %> - <%= form.text_field :lastname %> -
- -
- <%= form.label :streetname, style: "display: block" %> - <%= form.text_field :streetname %> -
- -
- <%= form.label :zipcode, style: "display: block" %> - <%= form.text_field :zipcode %> -
- -
- <%= form.label :city, style: "display: block" %> - <%= form.text_field :city %> -
- -
- <%= form.label :country, style: "display: block" %> - <%= form.text_field :country %> -
- -
- <%= form.label :email, style: "display: block" %> - <%= form.text_field :email %> -
- -
- <%= form.label :phone, style: "display: block" %> - <%= form.text_field :phone %> -
- -
- <%= form.submit %> -
+<% else %> +

This client is used in multiple subprojects and cannot be edited.

<% end %>