check if clients are editable

This commit is contained in:
Stefan Tollkühn
2025-07-21 16:00:37 +02:00
parent bd0f65a323
commit 0ea5e5926f
2 changed files with 32 additions and 58 deletions

View File

@@ -5,4 +5,12 @@ class Client < ApplicationRecord
validates :company_name, :firstname, :lastname, presence: true validates :company_name, :firstname, :lastname, presence: true
# Add other validations as needed # 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 end

View File

@@ -1,3 +1,4 @@
<% if @client.editable? %>
<%= form_with(model: client) do |form| %> <%= form_with(model: client) do |form| %>
<% if client.errors.any? %> <% if client.errors.any? %>
<div style="color: red"> <div style="color: red">
@@ -11,52 +12,17 @@
</div> </div>
<% end %> <% end %>
<% [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone].each do |attrib|%>
<div> <div>
<%= form.label :company_name, style: "display: block" %> <%= form.label attrib, style: "display: block" %>
<%= form.text_field :company_name %> <%= form.text_field attrib %><br>
</div>
<div>
<%= form.label :firstname, style: "display: block" %>
<%= form.text_field :firstname %>
</div>
<div>
<%= form.label :lastname, style: "display: block" %>
<%= form.text_field :lastname %>
</div>
<div>
<%= form.label :streetname, style: "display: block" %>
<%= form.text_field :streetname %>
</div>
<div>
<%= form.label :zipcode, style: "display: block" %>
<%= form.text_field :zipcode %>
</div>
<div>
<%= form.label :city, style: "display: block" %>
<%= form.text_field :city %>
</div>
<div>
<%= form.label :country, style: "display: block" %>
<%= form.text_field :country %>
</div>
<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>
<div>
<%= form.label :phone, style: "display: block" %>
<%= form.text_field :phone %>
</div> </div>
<% end %>
<div> <div>
<%= form.submit %> <%= form.submit %>
</div> </div>
<% end %> <% end %>
<% else %>
<p>This client is used in multiple subprojects and cannot be edited.</p>
<% end %>