From 6cd4bec9247506fb3e15f4bdbefff8df51beafee Mon Sep 17 00:00:00 2001 From: TamarinEA Date: Tue, 26 Jan 2021 00:04:05 -0800 Subject: [PATCH] feature: ability to rename cte https://jira.railsc.ru/browse/GOODS-2607 --- .../thinking_sphinx/index/builder.rb | 14 ++++++++ .../thinking_sphinx/index/builder_spec.rb | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/sphinx/integration/extensions/thinking_sphinx/index/builder.rb b/lib/sphinx/integration/extensions/thinking_sphinx/index/builder.rb index c0007e8..b1e2e50 100644 --- a/lib/sphinx/integration/extensions/thinking_sphinx/index/builder.rb +++ b/lib/sphinx/integration/extensions/thinking_sphinx/index/builder.rb @@ -57,6 +57,20 @@ def delete_withs(*names) end end + # Переименовывает CTE + # + # old_name - String, название сохранённого CTE + # new_name - String, новое название CTE + # + # При перемещении новое CTE (если оно не было сохранено заранее) попадёт в конец списка CTE. + # Можно указать то же название CTE, что переместит блок в конец списка CTE. + # + # Returns nothing + def rename_with(old_name, new_name) + with_block = @index.local_options[:source_cte].delete(old_name) + @index.local_options[:source_cte][new_name] = with_block + end + # Формирует LEFT JOIN # # name - Symbol or Hash diff --git a/spec/sphinx/integration/extensions/thinking_sphinx/index/builder_spec.rb b/spec/sphinx/integration/extensions/thinking_sphinx/index/builder_spec.rb index 26bcc50..c3b7863 100644 --- a/spec/sphinx/integration/extensions/thinking_sphinx/index/builder_spec.rb +++ b/spec/sphinx/integration/extensions/thinking_sphinx/index/builder_spec.rb @@ -144,6 +144,42 @@ it { expect(index.local_options[:source_cte][:_rubrics2]).to eq "select id from rubrics2" } end + describe 'rename with' do + context 'when different new name' do + let(:index) do + ThinkingSphinx::Index::Builder.generate(ModelWithDisk) do + with(:_rubrics1) { "select id from rubrics1" } + with(:_rubrics2) { "select id from rubrics2" } + + rename_with(:_rubrics1, :_rubrics3) + end + end + + it do + expect(index.local_options[:source_cte].size).to eq 2 + expect(index.local_options[:source_cte][:_rubrics3]).to eq "select id from rubrics1" + expect(index.local_options[:source_cte].keys).to eq [:_rubrics2, :_rubrics3] + end + end + + context 'when same new name' do + let(:index) do + ThinkingSphinx::Index::Builder.generate(ModelWithDisk) do + with(:_rubrics1) { "select id from rubrics1" } + with(:_rubrics2) { "select id from rubrics2" } + + rename_with(:_rubrics1, :_rubrics1) + end + end + + it do + expect(index.local_options[:source_cte].size).to eq 2 + expect(index.local_options[:source_cte][:_rubrics1]).to eq "select id from rubrics1" + expect(index.local_options[:source_cte].keys).to eq [:_rubrics2, :_rubrics1] + end + end + end + describe 'composite_index' do let(:index) do ThinkingSphinx::Index::Builder.generate(ModelWithDisk, nil) do