From 16f2e66e8611f29a824ac8555a16ecdc1d42116d Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Wed, 24 Jun 2020 22:34:31 +1000 Subject: [PATCH] Direct `reference_name` calls to index_set_class. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This ensures that if IndexSet is customised, that customised class gets this method called as well, rather than the original implementation. Of course, if `reference_name` isn’t customised, then it’s the original implementation that does get invoked, and that’s fine. This just keeps things consistent - always use whatever configuration’s index_set_class is set to. This was prompted by thinking through how to support anonymous models as discussed in #1172 by @kalsan. --- .../active_record/association_proxy/attribute_finder.rb | 2 +- .../active_record/callbacks/update_callbacks.rb | 2 +- lib/thinking_sphinx/index_set.rb | 2 +- lib/thinking_sphinx/middlewares/sphinxql.rb | 2 +- .../active_record/callbacks/update_callbacks_spec.rb | 3 ++- spec/thinking_sphinx/middlewares/sphinxql_spec.rb | 3 ++- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/thinking_sphinx/active_record/association_proxy/attribute_finder.rb b/lib/thinking_sphinx/active_record/association_proxy/attribute_finder.rb index ebbfecfcf..158549706 100644 --- a/lib/thinking_sphinx/active_record/association_proxy/attribute_finder.rb +++ b/lib/thinking_sphinx/active_record/association_proxy/attribute_finder.rb @@ -31,7 +31,7 @@ def indices @indices ||= begin configuration.preload_indices configuration.indices_for_references( - *ThinkingSphinx::IndexSet.reference_name(@association.klass) + *configuration.index_set_class.reference_name(@association.klass) ).reject &:distributed? end end diff --git a/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb b/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb index 042632242..ee9add052 100644 --- a/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb +++ b/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb @@ -47,7 +47,7 @@ def indices end def reference - ThinkingSphinx::IndexSet.reference_name(instance.class) + configuration.index_set_class.reference_name(instance.class) end def update(index) diff --git a/lib/thinking_sphinx/index_set.rb b/lib/thinking_sphinx/index_set.rb index b1403db8e..2e87a1643 100644 --- a/lib/thinking_sphinx/index_set.rb +++ b/lib/thinking_sphinx/index_set.rb @@ -80,7 +80,7 @@ def mti_classes def references options[:references] || classes_and_ancestors.collect { |klass| - ThinkingSphinx::IndexSet.reference_name(klass) + self.class.reference_name(klass) } end diff --git a/lib/thinking_sphinx/middlewares/sphinxql.rb b/lib/thinking_sphinx/middlewares/sphinxql.rb index 1c65505c5..69e01fd96 100644 --- a/lib/thinking_sphinx/middlewares/sphinxql.rb +++ b/lib/thinking_sphinx/middlewares/sphinxql.rb @@ -133,7 +133,7 @@ def indices def indices_match_classes? indices.collect(&:reference).uniq.sort == classes.collect { |klass| - ThinkingSphinx::IndexSet.reference_name(klass) + configuration.index_set_class.reference_name(klass) }.sort end diff --git a/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb b/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb index 0a8885960..d13a40e50 100644 --- a/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb +++ b/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb @@ -19,12 +19,13 @@ module Callbacks; end let(:klass) { double(:name => 'Article') } let(:configuration) { double('configuration', :settings => {'attribute_updates' => true}, - :indices_for_references => [index]) } + :indices_for_references => [index], :index_set_class => set_class) } let(:connection) { double('connection', :execute => '') } let(:index) { double 'index', :name => 'article_core', :sources => [source], :document_id_for_key => 3, :distributed? => false, :type => 'plain', :primary_key => :id} let(:source) { double('source', :attributes => []) } + let(:set_class) { double(:reference_name => :article) } before :each do stub_const 'ThinkingSphinx::Configuration', diff --git a/spec/thinking_sphinx/middlewares/sphinxql_spec.rb b/spec/thinking_sphinx/middlewares/sphinxql_spec.rb index bd8c34f9b..ff966d51c 100644 --- a/spec/thinking_sphinx/middlewares/sphinxql_spec.rb +++ b/spec/thinking_sphinx/middlewares/sphinxql_spec.rb @@ -31,7 +31,7 @@ class SphinxQLSubclass let(:query) { double('query') } let(:configuration) { double('configuration', :settings => {}, index_set_class: set_class) } - let(:set_class) { double(:new => index_set) } + let(:set_class) { double(:new => index_set, :reference_name => :article) } before :each do stub_const 'Riddle::Query::Select', double(:new => sphinx_sql) @@ -115,6 +115,7 @@ class SphinxQLSubclass model = double('model', :connection => double, :ancestors => [ActiveRecord::Base], :name => 'Animal') allow(index_set.first).to receive_messages :reference => :animal + allow(set_class).to receive_messages(:reference_name => :animal) search.options[:classes] = [model]