Skip to content

Commit

Permalink
Direct reference_name calls to index_set_class.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pat committed Jun 24, 2020
1 parent ea10107 commit 16f2e66
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/index_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/middlewares/sphinxql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion spec/thinking_sphinx/middlewares/sphinxql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 16f2e66

Please sign in to comment.