Skip to content

Commit

Permalink
decide per index to reconcile class name field
Browse files Browse the repository at this point in the history
Presently we only remove the class name field
if there's no index with inheritance at all
among the ones defined. This commit reconciles
all possible indices, skipping only the ones
where this field is needed.
  • Loading branch information
akostadinov authored and pat committed Jun 12, 2022
1 parent 08b9f7b commit 5ccb900
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions lib/thinking_sphinx/configuration/minimum_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ def initialize(indices)
end

def reconcile
return unless no_inheritance_columns?

field_collections.each do |collection|
collection.fields.delete_if do |field|
field.name == 'sphinx_internal_class_name'
Expand All @@ -20,18 +18,19 @@ def reconcile
attr_reader :indices

def field_collections
indices_of_type('plain').collect(&:sources).flatten +
plain_indices_without_inheritance.collect(&:sources).flatten +
indices_of_type('rt')
end

def indices_of_type(type)
indices.select { |index| index.type == type }
end

def no_inheritance_columns?
indices.select { |index|
index.model.table_exists? &&
index.model.column_names.include?(index.model.inheritance_column)
}.empty?
def inheritance_columns?(index)
index.model.table_exists? && index.model.column_names.include?(index.model.inheritance_column)
end

def plain_indices_without_inheritance
indices_of_type('plain').reject(&method(:inheritance_columns?))
end
end
4 changes: 2 additions & 2 deletions spec/thinking_sphinx/configuration/minimum_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
expect(index_b.fields).to eq([field_b2])
end

it 'keeps the class name fields when one index model has a type column' do
it 'removes the class name fields only for the indices without type column' do
allow(model_a).to receive(:column_names).and_return(['id', 'name', 'type'])
allow(model_b).to receive(:column_names).and_return(['id', 'name'])

subject.reconcile

expect(index_a.sources.first.fields).to eq([field_a1, field_a2])
expect(index_b.fields).to eq([field_b1, field_b2])
expect(index_b.fields).to eq([field_b2])
end
end

0 comments on commit 5ccb900

Please sign in to comment.