From bae8b9318d6d68ee74bf3de41b51351eceb7d727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Tollk=C3=BChn?= Date: Tue, 22 Jul 2025 16:01:46 +0200 Subject: [PATCH] allow saving of subproject when using existing client --- app/models/subproject.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/subproject.rb b/app/models/subproject.rb index 8b17504..fb251f3 100644 --- a/app/models/subproject.rb +++ b/app/models/subproject.rb @@ -5,9 +5,9 @@ class Subproject < ApplicationRecord belongs_to :owner, class_name: 'Client', optional: true belongs_to :builder, class_name: 'Client', optional: true - accepts_nested_attributes_for :client, reject_if: :all_blank - accepts_nested_attributes_for :owner, reject_if: :all_blank - accepts_nested_attributes_for :builder, reject_if: :all_blank + accepts_nested_attributes_for :client, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } + accepts_nested_attributes_for :owner, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } + accepts_nested_attributes_for :builder, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } validates :subproject_name, presence: true validate :client_presence_check @@ -17,16 +17,19 @@ class Subproject < ApplicationRecord validates_associated :client, :owner, :builder def client_attributes=(attributes) + return if self.client_id.present? self.client = Client.find_or_initialize_by(email: attributes[:email]) self.client.assign_attributes(attributes) end def owner_attributes=(attributes) + return if self.owner_id.present? self.owner = Client.find_or_initialize_by(email: attributes[:email]) self.owner.assign_attributes(attributes) end def builder_attributes=(attributes) + return if self.builder_id.present? self.builder = Client.find_or_initialize_by(email: attributes[:email]) self.builder.assign_attributes(attributes) end @@ -57,12 +60,12 @@ class Subproject < ApplicationRecord end def owner_attributes_blank? - return true if client.nil? + return true if owner.nil? owner.attributes.except("id", "created_at", "updated_at").values.all?(&:blank?) end def builder_attributes_blank? - return true if client.nil? + return true if builder.nil? builder.attributes.except("id", "created_at", "updated_at").values.all?(&:blank?) end