check if clients are editable
This commit is contained in:
@@ -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
|
||||
@@ -1,62 +1,28 @@
|
||||
<%= form_with(model: client) do |form| %>
|
||||
<% if client.errors.any? %>
|
||||
<div style="color: red">
|
||||
<h2><%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:</h2>
|
||||
<% if @client.editable? %>
|
||||
<%= form_with(model: client) do |form| %>
|
||||
<% if client.errors.any? %>
|
||||
<div style="color: red">
|
||||
<h2><%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% client.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<ul>
|
||||
<% client.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone].each do |attrib|%>
|
||||
<div>
|
||||
<%= form.label attrib, style: "display: block" %>
|
||||
<%= form.text_field attrib %><br>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.label :company_name, style: "display: block" %>
|
||||
<%= form.text_field :company_name %>
|
||||
</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>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% else %>
|
||||
<p>This client is used in multiple subprojects and cannot be edited.</p>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user