Skip to content

Commit

Permalink
Merge pull request #14 from iknow/fix-merge-mutation
Browse files Browse the repository at this point in the history
Don't alias subspecs when merge!ing
  • Loading branch information
chrisandreae authored Mar 22, 2024
2 parents a590997 + b3dfc77 commit cce164f
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.parse(a: :b)
b = DeepPreloader::Spec.parse(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.parse(a: [:b, :c]))
expect(a).to eq(DeepPreloader::Spec.parse(a: :b))
expect(b).to eq(DeepPreloader::Spec.parse(a: :c))
end

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

Expand Down

0 comments on commit cce164f

Please sign in to comment.