Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use instances for index filtering from callbacks. #1171

Merged
merged 3 commits into from
Jun 22, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
8 changes: 6 additions & 2 deletions lib/thinking_sphinx/index_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 28 additions & 12 deletions spec/thinking_sphinx/index_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -50,21 +60,29 @@ 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 "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 [
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])
Expand All @@ -73,22 +91,20 @@ 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 => [])
opinion = class_double('OpinionArticle', {:column_names => []}, article)

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
Expand Down