Skip to content

Commit

Permalink
MONGOID-5808 Fix collection_options in store_in (#5859)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Sep 4, 2024
1 parent f85ff96 commit a79e90c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 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
36 changes: 36 additions & 0 deletions spec/integration/persistence/collection_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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
CollectionOptionsCapped.collection.drop
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
2 changes: 1 addition & 1 deletion spec/mongoid/monkey_patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# @note This test ensures that we do not inadvertently introduce new monkey patches
# to Mongoid. Existing monkey patch methods which are marked with +Mongoid.deprecated+
# are excluded from this test.
RSpec.describe('Do not add monkey patches') do # rubocop:disable RSpec/DescribeClass
RSpec.describe('Do not add monkey patches') do
classes = [
Object,
Array,
Expand Down
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 a79e90c

Please sign in to comment.