Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5709 Fix atomic_path calculation #5773

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/mongoid/atomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ def atomic_position
#
# @return [ Object ] The associated path.
def atomic_paths
@atomic_paths ||= if _association
_association.path(self)
else
Atomic::Paths::Root.new(self)
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 @@ -3,6 +3,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 @@ -19,4 +41,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
Loading