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

Don't alias subspecs when merge!ing #14

Merged
merged 1 commit into from
Mar 22, 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
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
Loading