Skip to content

Commit

Permalink
add tests to ensure an explicit context overrides a remembered one
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Nov 22, 2023
1 parent c570d4b commit 8a5491a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/mongoid/clients/storage_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ module StorageOptions
class_attribute :storage_options, instance_accessor: false, default: storage_options_defaults
end

# Remembers the storage options that were active when the current object
# was instantiated/created.
#
# @return [ Hash | nil ] the storage options that have been cached for
# this object instance (or nil if no storage options have been
# cached).
#
# @api private
attr_accessor :remembered_storage_options

# The storage options that apply to this record, consisting of both
Expand Down
12 changes: 12 additions & 0 deletions spec/mongoid/document_persistence_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
person.update username: 'zyg15'
expect(Person.with(options) { Person.first.username }).to be == 'zyg15'
end

it 'an explicit context takes precedence over a remembered context when persisting' do
person.username = 'bob'
# should not actually save -- the person does not exist in the
# `other` collection and so cannot be updated.
Person.with(collection: 'other') { person.save! }
expect(person.reload.username).to eq 'zyg14'
end

it 'an explicit context takes precedence over a remembered context when reloading' do
expect { Person.with(collection: 'other') { person.reload } }.to raise_error(Mongoid::Errors::DocumentNotFound)
end
end

# pre-9.0 default persistence behavior
Expand Down

0 comments on commit 8a5491a

Please sign in to comment.