Skip to content

Commit

Permalink
Allow use of after_commit for real-time callback.
Browse files Browse the repository at this point in the history
As discussed in #1021.
  • Loading branch information
pat committed Nov 19, 2016
1 parent 6b0923c commit dc16d1c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
Expand Up @@ -3,14 +3,12 @@ def initialize(reference, path = [], &block)
@reference, @path, @block = reference, path, block
end

def after_save(instance)
return unless real_time_indices? && callbacks_enabled?
def after_commit(instance)
persist_changes instance
end

real_time_indices.each do |index|
objects_for(instance).each do |object|
ThinkingSphinx::RealTime::Transcriber.new(index).copy object
end
end
def after_save(instance)
persist_changes instance
end

private
Expand Down Expand Up @@ -40,6 +38,16 @@ def objects_for(instance)
Array results
end

def persist_changes(instance)
return unless real_time_indices? && callbacks_enabled?

real_time_indices.each do |index|
objects_for(instance).each do |object|
ThinkingSphinx::RealTime::Transcriber.new(index).copy object
end
end
end

def real_time_indices?
real_time_indices.any?
end
Expand Down
Expand Up @@ -17,7 +17,7 @@
allow(ThinkingSphinx::Connection).to receive_message_chain(:pool, :take).and_yield connection
end

describe '#after_save' do
describe '#after_save, #after_commit' do
let(:insert) { double('insert', :to_sql => 'REPLACE INTO my_index') }
let(:time) { 1.day.ago }
let(:field) { double('field', :name => 'name', :translate => 'Foo') }
Expand Down Expand Up @@ -51,6 +51,26 @@
callbacks.after_save instance
end

it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)

callbacks.after_commit instance
end

it "switches the insert to a replace statement" do
expect(insert).to receive(:replace!).and_return(insert)

callbacks.after_commit instance
end

it "sends the insert through to the server" do
expect(connection).to receive(:execute).with('REPLACE INTO my_index')

callbacks.after_commit instance
end

context 'with a given path' do
let(:callbacks) {
ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new(
Expand Down Expand Up @@ -79,6 +99,26 @@

callbacks.after_save instance
end

it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)

callbacks.after_commit instance
end

it "gets the document id for the user object" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)

callbacks.after_commit instance
end

it "translates values for the user object" do
expect(field).to receive(:translate).with(user).and_return('Foo')

callbacks.after_commit instance
end
end

context 'with a path returning multiple objects' do
Expand Down Expand Up @@ -113,6 +153,28 @@

callbacks.after_save instance
end

it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)

callbacks.after_commit instance
end

it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)

callbacks.after_commit instance
end

it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')

callbacks.after_commit instance
end
end

context 'with a block instead of a path' do
Expand Down Expand Up @@ -147,6 +209,28 @@

callbacks.after_save instance
end

it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)

callbacks.after_commit instance
end

it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)

callbacks.after_commit instance
end

it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')

callbacks.after_commit instance
end
end
end
end

0 comments on commit dc16d1c

Please sign in to comment.