From c2d6223cd7da975dd3133a001374220684c0381f Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Tue, 3 Sep 2024 17:19:34 +0200 Subject: [PATCH 1/3] MONGOID-5808 Fix collection_options in store_in --- .rubocop.yml | 3 ++ lib/mongoid/persistence_context.rb | 3 +- .../persistence/collection_options_spec.rb | 35 +++++++++++++++++++ spec/mongoid/persistence_context_spec.rb | 21 ++++++++--- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 spec/integration/persistence/collection_options_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index bdb558ac7c..2a0126082c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -75,6 +75,9 @@ Metrics/MethodLength: RSpec/BeforeAfterAll: Enabled: false +RSpec/DescribeClass: + Enabled: false + RSpec/ImplicitExpect: EnforcedStyle: is_expected diff --git a/lib/mongoid/persistence_context.rb b/lib/mongoid/persistence_context.rb index ad0a7969a0..1f7ea96d6e 100644 --- a/lib/mongoid/persistence_context.rb +++ b/lib/mongoid/persistence_context.rb @@ -25,7 +25,8 @@ class PersistenceContext # @return [ Array ] 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. diff --git a/spec/integration/persistence/collection_options_spec.rb b/spec/integration/persistence/collection_options_spec.rb new file mode 100644 index 0000000000..17541bde65 --- /dev/null +++ b/spec/integration/persistence/collection_options_spec.rb @@ -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 diff --git a/spec/mongoid/persistence_context_spec.rb b/spec/mongoid/persistence_context_spec.rb index 9c2bb0fbf6..93676dfabf 100644 --- a/spec/mongoid/persistence_context_spec.rb +++ b/spec/mongoid/persistence_context_spec.rb @@ -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 From 7c83eacacb5701fd7a8785a8ab9ae57764ca6b4d Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Tue, 3 Sep 2024 18:06:26 +0200 Subject: [PATCH 2/3] Fix rubocop offences --- spec/mongoid/monkey_patches_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/mongoid/monkey_patches_spec.rb b/spec/mongoid/monkey_patches_spec.rb index 90086d4d9c..a6c92081d8 100644 --- a/spec/mongoid/monkey_patches_spec.rb +++ b/spec/mongoid/monkey_patches_spec.rb @@ -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, From 28ee83699864bd6d2092b1f1f8ae07c3b2042c1f Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Tue, 3 Sep 2024 18:39:25 +0200 Subject: [PATCH 3/3] Fix specs --- spec/integration/persistence/collection_options_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/integration/persistence/collection_options_spec.rb b/spec/integration/persistence/collection_options_spec.rb index 17541bde65..13109770ef 100644 --- a/spec/integration/persistence/collection_options_spec.rb +++ b/spec/integration/persistence/collection_options_spec.rb @@ -17,6 +17,7 @@ class CollectionOptionsCapped end after(:all) do + CollectionOptionsCapped.collection.drop Mongoid.deregister_model(CollectionOptionsCapped) Object.send(:remove_const, :CollectionOptionsCapped) end