Skip to content

Commit

Permalink
feat: Allow model/id arguments to Processor.
Browse files Browse the repository at this point in the history
An evolution of f6657c9, as per feedback from @akostadinov in #1215. If
you’re dealing with a deleted record, then you may not have an instance
(and cannot retrieve it from the database, as it doesn’t exist). So, the
Processor class now allows for either an instance, or a model and id.

The model should be the actual class, not the string name of the class.
  • Loading branch information
pat committed Jun 11, 2022
1 parent ce2d167 commit 6034c75
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def after_rollback
def delete_from_sphinx
return if ThinkingSphinx::Callbacks.suspended?

ThinkingSphinx::Processor.new(instance).delete
ThinkingSphinx::Processor.new(instance: instance).delete
end
end
20 changes: 14 additions & 6 deletions lib/thinking_sphinx/processor.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
# frozen_string_literal: true

class ThinkingSphinx::Processor
def initialize(instance)
def initialize(instance: nil, model: nil, id: nil)
raise ArgumentError if instance.nil? && (model.nil? || id.nil?)

@instance = instance
@model = model || instance.class
@id = id
end

def delete
return if instance.new_record?
return if instance&.new_record?

indices.each { |index|
ThinkingSphinx::Deletion.perform(
index, instance.public_send(index.primary_key)
index, id || instance.public_send(index.primary_key)
)
}
end

def upsert
real_time_indices.each do |index|
ThinkingSphinx::RealTime::Transcriber.new(index).copy instance
ThinkingSphinx::RealTime::Transcriber.new(index).copy loaded_instance
end
end

private

attr_reader :instance
attr_reader :instance, :model, :id

def indices
ThinkingSphinx::Configuration.instance.index_set_class.new(
:instances => [instance], :classes => [instance.class]
:instances => [instance].compact, :classes => [model]
).to_a
end

def loaded_instance
@loaded_instance ||= instance || model.find(id)
end

def real_time_indices
indices.select { |index| index.is_a? ThinkingSphinx::RealTime::Index }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/real_time_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
expect(Admin::Person.search('Pat').to_a).to be_empty

instance = Admin::Person.find_by(:name => 'Pat')
ThinkingSphinx::Processor.new(instance).upsert
ThinkingSphinx::Processor.new(instance: instance).upsert

expect(Admin::Person.search('Pat').to_a).to eq([instance])

Expand All @@ -48,7 +48,7 @@
expect(Admin::Person.search('Patrick').to_a).to be_empty

instance.reload
ThinkingSphinx::Processor.new(instance).upsert
ThinkingSphinx::Processor.new(model: Admin::Person, id: instance.id).upsert

expect(Admin::Person.search('Patrick').to_a).to eq([instance])
end
Expand Down
24 changes: 24 additions & 0 deletions spec/acceptance/remove_deleted_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@

expect(Bird.search_for_ids('duck')).to be_empty
end

it "can use a direct interface for processing records" do
pancakes = Article.create! :title => 'Pancakes'
index
expect(Article.search('pancakes')).not_to be_empty

Article.connection.execute "DELETE FROM articles WHERE id = #{pancakes.id}"
expect(Article.search_for_ids('pancakes')).not_to be_empty

ThinkingSphinx::Processor.new(instance: pancakes).delete
expect(Article.search_for_ids('pancakes')).to be_empty
end

it "can use a direct interface for processing records without an instance" do
pancakes = Article.create! :title => 'Pancakes'
index
expect(Article.search('pancakes')).not_to be_empty

Article.connection.execute "DELETE FROM articles WHERE id = #{pancakes.id}"
expect(Article.search_for_ids('pancakes')).not_to be_empty

ThinkingSphinx::Processor.new(model: Article, id: pancakes.id).delete
expect(Article.search_for_ids('pancakes')).to be_empty
end
end

0 comments on commit 6034c75

Please sign in to comment.