Skip to content

Commit

Permalink
Don't alias subspecs when merge!ing
Browse files Browse the repository at this point in the history
Otherwise they'll be subject to being clobbered in subsequent merges
  • Loading branch information
chrisandreae committed Mar 22, 2024
1 parent a072f85 commit 87f036c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/deep_preloader/polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def merge!(other)
if specs_by_type[k]
specs_by_type[k].merge!(v)
else
specs_by_type[k] = v
specs_by_type[k] = v.deep_dup
end
end
else
Expand All @@ -54,6 +54,10 @@ def ==(other)

alias eql? ==

def deep_dup
self.class.new(specs_by_type.deep_dup)
end

def inspect
"PolySpec#{specs_by_type.inspect}"
end
Expand Down
6 changes: 5 additions & 1 deletion lib/deep_preloader/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def merge!(other)
if association_specs[k]
association_specs[k].merge!(v)
else
association_specs[k] = v
association_specs[k] = v.deep_dup
end
end
else
Expand All @@ -57,6 +57,10 @@ def ==(other)

alias eql? ==

def deep_dup
self.class.new(association_specs.deep_dup)
end

def inspect
"Spec#{association_specs.inspect}"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/deep_preloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
expect(DeepPreloader::Spec.parse(spec)).to eq(expected_spec)
end

it 'can merge specs' do
a = DeepPreloader::Spec.new(a: :b)
b = DeepPreloader::Spec.new(a: :c)
result = DeepPreloader::Spec.new.merge!(a).merge!(b)

# The result should contain each of the merged specs and the merged specs
# themselves should not be altered
expect(result).to eq(DeepPreloader::Spec.new(a: [:b, :c]))
expect(a).to eq(DeepPreloader::Spec.new(a: :b))
expect(b).to eq(DeepPreloader::Spec.new(a: :d))
end

context 'with a test model' do
with_temporary_table(:model)

Expand Down

0 comments on commit 87f036c

Please sign in to comment.