Skip to content

Commit 342014b

Browse files
committed
Fix backend_apis associations
1 parent dca6ac8 commit 342014b

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

app/lib/backend_api_logic/service_extension.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def backend_api_config
3333
end
3434

3535
def backend_api
36-
@backend_api ||= backend_api_configs.first&.backend_api || account.backend_apis.build(system_name: service_system_name, name: "#{service_name} Backend", description: "Backend of #{service_name}")
36+
return @backend_api if @backend_api&.persisted?
37+
38+
@backend_api = backend_api_configs.first&.backend_api ||
39+
@backend_api
40+
@backend_api ||= account.backend_apis.build(system_name: service_system_name, name: "#{service_name} Backend", description: "Backend of #{service_name}")
3741
end
3842

3943
def update!(attrs = {})

test/factories/other.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
association(:account, :factory => :provider_account)
4747
after(:create) do |record|
4848
record.service_tokens.first_or_create!(value: 'token')
49+
# Proxy validation during service creation calls account.backend_apis.build(...),
50+
# which adds an unpersisted BackendApi to the association. Remove it to prevent
51+
# validation failures in later operations on the account.
52+
record.account.backend_apis.target.delete_if { |ba| !ba.persisted? }
4953
end
5054
end
5155

test/factories/simple.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
association(:account, :factory => :simple_provider)
8080
after(:create) do |record|
8181
record.service_tokens.first_or_create!(value: 'token')
82+
# Proxy validation during service creation calls account.backend_apis.build(...),
83+
# which adds an unpersisted BackendApi to the association. Remove it to prevent
84+
# validation failures in later operations on the account.
85+
record.account.backend_apis.target.delete_if { |ba| !ba.persisted? }
8286
end
8387

8488
trait :with_default_backend_api do

0 commit comments

Comments
 (0)