From cd5257635a2e09c920520b6facf37fd0c1f1f929 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Wed, 27 Nov 2024 07:57:01 -0700 Subject: [PATCH] make sure to evaluate client name since it might be a proc (#5906) --- lib/mongoid/persistence_context.rb | 2 +- spec/mongoid/persistence_context_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/mongoid/persistence_context.rb b/lib/mongoid/persistence_context.rb index 3f110fc970..89d7f49e64 100644 --- a/lib/mongoid/persistence_context.rb +++ b/lib/mongoid/persistence_context.rb @@ -138,7 +138,7 @@ def client # @return [ Symbol ] The client name for this persistence # context. def client_name - @client_name ||= options[:client] || + @client_name ||= __evaluate__(options[:client]) || Threaded.client_override || __evaluate__(storage_options[:client]) end diff --git a/spec/mongoid/persistence_context_spec.rb b/spec/mongoid/persistence_context_spec.rb index ecbc95bd80..85ec4f7b4f 100644 --- a/spec/mongoid/persistence_context_spec.rb +++ b/spec/mongoid/persistence_context_spec.rb @@ -584,6 +584,14 @@ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative)) end + context 'when the client option is a proc' do + let(:options) { { client: -> { :alternative } } } + + it 'evaluates the proc' do + expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative)) + end + end + context 'when there is a client override' do persistence_context_override :client, :other