Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Please mark backwards incompatible changes with an exclamation mark at the start
## [Unreleased]

### Added
- A `#clone` method to `Elasticsearch::QueryBuilder` that properly clones the
`QueryBuilder` and its nested objects.
- ActiveSupport's `#present?`, `#presence` and `#blank?` methods can now be used
in ERB configuration files.

Expand Down
10 changes: 10 additions & 0 deletions lib/jay_api/elasticsearch/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def merge(other)
)
end

# @return [JayAPI::Elasticsearch::QueryBuilder] A copy of the receiver.
def clone
copy = super
copy.source = @source.clone # source can be an Array or a Hash
copy.sort = @sort.clone # sort is a Hash
copy.query = query.clone
copy.aggregations = aggregations.clone
copy
end

protected

attr_writer :from, :size, :source, :collapse, :sort, :query, :aggregations
Expand Down
156 changes: 156 additions & 0 deletions spec/jay_api/elasticsearch/query_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,160 @@
end
end
end

describe '#clone' do
subject(:method_call) { query_builder.clone }

let(:query_clauses_clone) { query_clauses.clone }
let(:aggregations_clone) { aggregations.clone }

before do
allow(query_clauses).to receive(:clone).and_return(query_clauses_clone)
allow(aggregations).to receive(:clone).and_return(aggregations_clone)
end

shared_examples_for '#clone' do
it "returns an instance of #{described_class}" do
expect(method_call).to be_a(described_class)
end

it 'does not return the same object' do
expect(method_call).not_to be(query_builder)
end
end

context "when the receiver has no 'from' clause" do
it_behaves_like '#clone'

it "does not have a 'from' clause either" do
expect(method_call.to_query).not_to have_key(:from)
end
end

context "when the receiver has a 'from' clause" do
before { query_builder.from(100) }

it_behaves_like '#clone'

it "has the expected 'from' clause" do
expect(method_call.to_query).to include(from: 100)
end
end

context "when the receiver has no 'size' clause" do
it_behaves_like '#clone'

it "does not have a 'size' clause either" do
expect(method_call.to_query).not_to have_key(:size)
end
end

context "when the receiver has a 'size' clause" do
before { query_builder.size(500) }

it_behaves_like '#clone'

it "has the expected 'size' clause" do
expect(method_call.to_query).to include(size: 500)
end
end

context "when the receiver has no 'source' clause" do
it_behaves_like '#clone'

it "does not have a 'source' clause either" do
expect(method_call.to_query).not_to have_key(:_source)
end
end

context "when the receiver has a 'source' clause" do
before { query_builder.source('profile.*') }

it_behaves_like '#clone'

it "has the expected 'source' clause" do
expect(method_call.to_query).to include(_source: 'profile.*')
end
end

context "when the 'source' changes after cloning" do
let(:source) { %w[profile.* permissions.*] }

before do
query_builder.source(source)
end

it_behaves_like '#clone'

it "does not change the 'source' of the clone" do
clone = method_call
expect { source << 'pictures.*' }.not_to change(clone, :to_query)
end
end

context "when the receiver has no 'collapse' clause" do
it_behaves_like '#clone'

it "does not have a 'collapse' clause either" do
expect(method_call.to_query).not_to have_key(:collapse)
end
end

context "when the receiver has a 'collapse' clause" do
before { query_builder.collapse('profile.email') }

it_behaves_like '#clone'

it "has the expected 'collapse' clause" do
expect(method_call.to_query).to include(collapse: { field: 'profile.email' })
end
end

context "when the receiver has no 'sort' clause" do
it_behaves_like '#clone'

it "does not have a 'sort' clause either" do
expect(method_call.to_query).not_to have_key(:sort)
end
end

context "when the receiver has a 'sort' clause" do
before { query_builder.sort(age: :desc) }

it_behaves_like '#clone'

it "has the expected 'sort' clause" do
expect(method_call.to_query).to include(sort: [{ age: { order: :desc } }])
end
end

context "when the 'sort' changes after cloning" do
before { query_builder.sort(age: :desc) }

it_behaves_like '#clone'

it "does not change the 'sort' clause of the clone" do
clone = method_call
expect { query_builder.sort(name: :asc) }.not_to change(clone, :to_query)
end
end

it 'clones the nested QueryClauses object' do
expect(query_clauses).to receive(:clone)
method_call
end

it 'has the cloned QueryClauses object' do
expect(method_call.query).to be(query_clauses_clone)
end

it 'clones the nested Aggregations object' do
expect(aggregations).to receive(:clone)
method_call
end

it 'has the cloned Aggregations object' do
expect(method_call.aggregations).to be(aggregations_clone)
end
end
end
Loading