From 2f7aea9634c6cba140db6493c41c17219e33aff8 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Thu, 18 Jun 2020 22:23:01 +1000 Subject: [PATCH 1/3] Make IndexSet spec a little more specific. --- spec/thinking_sphinx/index_set_spec.rb | 28 +++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/spec/thinking_sphinx/index_set_spec.rb b/spec/thinking_sphinx/index_set_spec.rb index 148cce3f4..b8d56e03d 100644 --- a/spec/thinking_sphinx/index_set_spec.rb +++ b/spec/thinking_sphinx/index_set_spec.rb @@ -30,6 +30,16 @@ def class_double(name, methods = {}, *superclasses) end describe '#to_a' do + let(:article_index) do + double(:reference => :article, :distributed? => false) + end + let(:opinion_article_index) do + double(:reference => :opinion_article, :distributed? => false) + end + let(:page_index) do + double(:reference => :page, :distributed? => false) + end + it "ensures the indices are loaded" do expect(configuration).to receive(:preload_indices) @@ -50,21 +60,17 @@ def class_double(name, methods = {}, *superclasses) it "uses indices for the given classes" do configuration.indices.replace [ - double(:reference => :article, :distributed? => false), - double(:reference => :opinion_article, :distributed? => false), - double(:reference => :page, :distributed? => false) + article_index, opinion_article_index, page_index ] options[:classes] = [class_double('Article', :column_names => [])] - expect(set.to_a.length).to eq(1) + expect(set.to_a).to eq([article_index]) end it "requests indices for any STI superclasses" do configuration.indices.replace [ - double(:reference => :article, :distributed? => false), - double(:reference => :opinion_article, :distributed? => false), - double(:reference => :page, :distributed? => false) + article_index, opinion_article_index, page_index ] article = class_double('Article', :column_names => [:type]) @@ -73,14 +79,12 @@ def class_double(name, methods = {}, *superclasses) options[:classes] = [opinion] - expect(set.to_a.length).to eq(2) + expect(set.to_a).to eq([article_index, opinion_article_index]) end it "does not use MTI superclasses" do configuration.indices.replace [ - double(:reference => :article, :distributed? => false), - double(:reference => :opinion_article, :distributed? => false), - double(:reference => :page, :distributed? => false) + article_index, opinion_article_index, page_index ] article = class_double('Article', :column_names => []) @@ -88,7 +92,7 @@ def class_double(name, methods = {}, *superclasses) options[:classes] = [opinion] - expect(set.to_a.length).to eq(1) + expect(set.to_a).to eq([opinion_article_index]) end it "uses named indices if names are provided" do From f9fddf018239fbe87bf2be81217ed2eb7a96b2d3 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Thu, 18 Jun 2020 22:46:45 +1000 Subject: [PATCH 2/3] Accept instances for class index filtering. --- lib/thinking_sphinx/index_set.rb | 8 ++++++-- spec/thinking_sphinx/index_set_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/thinking_sphinx/index_set.rb b/lib/thinking_sphinx/index_set.rb index f8a6a4dcb..b1403db8e 100644 --- a/lib/thinking_sphinx/index_set.rb +++ b/lib/thinking_sphinx/index_set.rb @@ -34,11 +34,11 @@ def all_indices end def classes - options[:classes] || [] + options[:classes] || instances.collect(&:class) end def classes_specified? - classes.any? || references_specified? + instances.any? || classes.any? || references_specified? end def classes_and_ancestors @@ -68,6 +68,10 @@ def indices_for_references all_indices.select { |index| references.include? index.reference } end + def instances + options[:instances] || [] + end + def mti_classes classes.reject { |klass| klass.column_names.include?(klass.inheritance_column) diff --git a/spec/thinking_sphinx/index_set_spec.rb b/spec/thinking_sphinx/index_set_spec.rb index b8d56e03d..3197009bd 100644 --- a/spec/thinking_sphinx/index_set_spec.rb +++ b/spec/thinking_sphinx/index_set_spec.rb @@ -68,6 +68,18 @@ def class_double(name, methods = {}, *superclasses) expect(set.to_a).to eq([article_index]) end + it "uses indices for the given instance's class" do + configuration.indices.replace [ + article_index, opinion_article_index, page_index + ] + + instance_class = class_double('Article', :column_names => []) + + options[:instances] = [double(:instance, :class => instance_class)] + + expect(set.to_a).to eq([article_index]) + end + it "requests indices for any STI superclasses" do configuration.indices.replace [ article_index, opinion_article_index, page_index From cdbb72e033cdee9954317ac77bc548307b9bd539 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Thu, 18 Jun 2020 22:57:29 +1000 Subject: [PATCH 3/3] Use instance-level filter for callback indices. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We’re going to still pass in the `:classes` option for the moment, for the sake of anyone who’s got a custom IndexSet that relies on that option when managing callbacks. --- .../active_record/callbacks/delete_callbacks.rb | 2 +- .../active_record/callbacks/delta_callbacks.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/thinking_sphinx/active_record/callbacks/delete_callbacks.rb b/lib/thinking_sphinx/active_record/callbacks/delete_callbacks.rb index a60cdec57..a4f8a962f 100644 --- a/lib/thinking_sphinx/active_record/callbacks/delete_callbacks.rb +++ b/lib/thinking_sphinx/active_record/callbacks/delete_callbacks.rb @@ -27,7 +27,7 @@ def delete_from_sphinx def indices ThinkingSphinx::Configuration.instance.index_set_class.new( - :classes => [instance.class] + :instances => [instance], :classes => [instance.class] ).to_a end end diff --git a/lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb b/lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb index 828d94e7e..6cdf4422f 100644 --- a/lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb +++ b/lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb @@ -43,8 +43,9 @@ def delta_indices? end def indices - @indices ||= config.index_set_class.new(:classes => [instance.class]). - select { |index| index.type == "plain" } + @indices ||= config.index_set_class.new( + :instances => [instance], :classes => [instance.class] + ).select { |index| index.type == "plain" } end def new_or_changed?