Skip to content

Commit

Permalink
MONGOID-5819 Do not pass the :database option when creating a client (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jamis committed Oct 21, 2024
1 parent 560ce66 commit e32d5c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/mongoid/persistence_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ def database_name
def client
@client ||= begin
client = Clients.with_name(client_name)
options = client_options

if database_name_option
client = client.use(database_name)
options = options.except(:database, 'database')
end
unless client_options.empty?
client = client.with(client_options)
end

client = client.with(options) unless options.empty?

client
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/clients/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
let(:options) { { database: 'other' } }

it 'sets the options on the client' do
expect(persistence_context.client.options['database']).to eq(options[:database])
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
end

it 'does not set the options on class level' do
Expand Down Expand Up @@ -319,7 +319,7 @@
end

it 'sets the options on the client' do
expect(persistence_context.client.options['database']).to eq(options[:database])
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
end

it 'does not set the options on instance level' do
Expand Down
31 changes: 31 additions & 0 deletions spec/mongoid/persistence_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@
end
end
end

context 'when the database is specified as a proc' do
let(:options) { { database: ->{ 'other' } } }

after { persistence_context.client.close }

it 'evaluates the proc' do
expect(persistence_context.database_name).to eq(:other)
end

it 'does not pass the proc to the client' do
expect(persistence_context.client.database.name).to eq('other')
end
end
end

describe '#client' do
Expand Down Expand Up @@ -608,6 +622,23 @@
end
end

context 'when the client is set as a proc in the storage options' do
let(:options) { {} }

before do
Band.store_in client: ->{ :alternative }
end

after do
persistence_context.client.close
Band.store_in client: nil
end

it 'uses the client option' do
expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
end
end

context 'when there is no client option set' do

let(:options) do
Expand Down

0 comments on commit e32d5c7

Please sign in to comment.