Skip to content

Commit

Permalink
MONGOID-5808 Fix collection_options in store_in
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Sep 3, 2024
1 parent 69eaf1d commit c2d6223
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Metrics/MethodLength:
RSpec/BeforeAfterAll:
Enabled: false

RSpec/DescribeClass:
Enabled: false

RSpec/ImplicitExpect:
EnforcedStyle: is_expected

Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid/persistence_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class PersistenceContext
# @return [ Array<Symbol> ] The list of extra options besides client options
# that determine the persistence context.
EXTRA_OPTIONS = [ :client,
:collection
:collection,
:collection_options
].freeze

# The full list of valid persistence context options.
Expand Down
35 changes: 35 additions & 0 deletions spec/integration/persistence/collection_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'spec_helper'

# rubocop:disable RSpec/LeakyConstantDeclaration
# rubocop:disable Lint/ConstantDefinitionInBlock
describe 'Collection options' do
before(:all) do
class CollectionOptionsCapped
include Mongoid::Document

store_in collection_options: {
capped: true,
size: 25_600
}
end
end

after(:all) do
Mongoid.deregister_model(CollectionOptionsCapped)
Object.send(:remove_const, :CollectionOptionsCapped)
end

before do
CollectionOptionsCapped.collection.drop
# We should create the collection explicitly to apply collection options.
CollectionOptionsCapped.create_collection
end

it 'creates a document' do
expect { CollectionOptionsCapped.create! }.not_to raise_error
end
end
# rubocop:enable Lint/ConstantDefinitionInBlock
# rubocop:enable RSpec/LeakyConstantDeclaration
21 changes: 17 additions & 4 deletions spec/mongoid/persistence_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,25 @@

context 'when the options are valid extra options' do

let(:options) do
{ collection: 'other' }
context 'collection' do

let(:options) do
{ collection: 'other' }
end

it 'sets the options on the persistence context object' do
expect(persistence_context.collection_name).to eq(options[:collection].to_sym)
end
end

it 'sets the options on the persistence context object' do
expect(persistence_context.collection_name).to eq(options[:collection].to_sym)
context 'collection_options' do
let(:options) do
{ collection_options: { capped: true } }
end

it 'does not propagate to client options' do
expect(persistence_context.send(:client_options).key?(:collection_options)).to eq(false)
end
end
end

Expand Down

0 comments on commit c2d6223

Please sign in to comment.