Skip to content

Commit 3c8f5db

Browse files
authored
MONGOID-5819 Do not pass the :database option when creating a client (#5886)
* MONGOID-5819 Do not pass the :database option when creating a client The option is already accounted for via the `#use` method. * fix failing specs
1 parent cd07a18 commit 3c8f5db

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

lib/mongoid/persistence_context.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,15 @@ def database_name
117117
def client
118118
@client ||= begin
119119
client = Clients.with_name(client_name)
120+
options = client_options
121+
120122
if database_name_option
121123
client = client.use(database_name)
124+
options = options.except(:database, 'database')
122125
end
123-
unless client_options.empty?
124-
client = client.with(client_options)
125-
end
126+
127+
client = client.with(options) unless options.empty?
128+
126129
client
127130
end
128131
end

spec/mongoid/clients/options_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
let(:options) { { database: 'other' } }
2828

2929
it 'sets the options on the client' do
30-
expect(persistence_context.client.options['database']).to eq(options[:database])
30+
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
3131
end
3232

3333
it 'does not set the options on class level' do
@@ -319,7 +319,7 @@
319319
end
320320

321321
it 'sets the options on the client' do
322-
expect(persistence_context.client.options['database']).to eq(options[:database])
322+
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
323323
end
324324

325325
it 'does not set the options on instance level' do

spec/mongoid/persistence_context_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,20 @@
536536
end
537537
end
538538
end
539+
540+
context 'when the database is specified as a proc' do
541+
let(:options) { { database: ->{ 'other' } } }
542+
543+
after { persistence_context.client.close }
544+
545+
it 'evaluates the proc' do
546+
expect(persistence_context.database_name).to eq(:other)
547+
end
548+
549+
it 'does not pass the proc to the client' do
550+
expect(persistence_context.client.database.name).to eq('other')
551+
end
552+
end
539553
end
540554

541555
describe '#client' do
@@ -608,6 +622,23 @@
608622
end
609623
end
610624

625+
context 'when the client is set as a proc in the storage options' do
626+
let(:options) { {} }
627+
628+
before do
629+
Band.store_in client: ->{ :alternative }
630+
end
631+
632+
after do
633+
persistence_context.client.close
634+
Band.store_in client: nil
635+
end
636+
637+
it 'uses the client option' do
638+
expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
639+
end
640+
end
641+
611642
context 'when there is no client option set' do
612643

613644
let(:options) do

0 commit comments

Comments
 (0)