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:

- + +
+ <% 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 %>