Skip to content

Commit

Permalink
MONGOID-5709 Fix atomic_path calculation (#5773) (#5775)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo authored Jan 23, 2024
1 parent 56d051a commit be2dc62
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/mongoid/atomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ def atomic_position
#
# @return [ Object ] The associated path.
def atomic_paths
@atomic_paths ||= begin
if _association
_association.path(self)
else
Atomic::Paths::Root.new(self)
end
end
return @atomic_paths if @atomic_paths

paths = if _association
_association.path(self)
else
Atomic::Paths::Root.new(self)
end

paths.tap { @atomic_paths = paths unless new_record? }
end

# Get all the attributes that need to be pulled.
Expand Down
40 changes: 40 additions & 0 deletions spec/integration/associations/has_and_belongs_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

require 'spec_helper'

module HabtmSpec
class Page
include Mongoid::Document
embeds_many :blocks, class_name: 'HabtmSpec::Block'
end

class Block
include Mongoid::Document
embedded_in :page, class_name: 'HabtmSpec::Page'
end

class ImageBlock < Block
has_and_belongs_to_many :attachments, inverse_of: nil, class_name: 'HabtmSpec::Attachment'
accepts_nested_attributes_for :attachments
end

class Attachment
include Mongoid::Document
field :file, type: String
end
end

describe 'has_and_belongs_to_many associations' do

context 'when an anonymous class defines a has_and_belongs_to_many association' do
Expand All @@ -18,4 +40,22 @@
expect(klass.new.movies.build).to be_a Movie
end
end

context 'when an embedded has habtm relation' do
let(:attachment) { HabtmSpec::Attachment.create!(file: 'foo.jpg') }

let(:page) { HabtmSpec::Page.create! }

let(:image_block) do
image_block = page.blocks.build({
_type: 'HabtmSpec::ImageBlock',
attachment_ids: [ attachment.id.to_s ],
attachments_attributes: { '1234' => { file: 'bar.jpg', id: attachment.id.to_s } }
})
end

it 'does not raise on save' do
expect { image_block.save! }.not_to raise_error
end
end
end

0 comments on commit be2dc62

Please sign in to comment.