Skip to content

Commit c90a10a

Browse files
authored
Merge pull request #1204 from pat/realtime-after-commit
Use after_commit callbacks for real-time changes.
2 parents 2c8d3e7 + 0190b61 commit c90a10a

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lib/thinking_sphinx/active_record/callbacks/delete_callbacks.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
class ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks <
44
ThinkingSphinx::Callbacks
55

6-
callbacks :after_destroy, :after_rollback
6+
callbacks :after_commit, :after_destroy, :after_rollback
7+
8+
def after_commit
9+
delete_from_sphinx
10+
end
711

812
def after_destroy
913
delete_from_sphinx

lib/thinking_sphinx/callbacks/appender.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def call
2424
attr_reader :model, :reference, :options, :block
2525

2626
def add_core_callbacks
27-
model.after_destroy ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks
27+
model.after_commit(
28+
ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks,
29+
on: :destroy
30+
)
2831
end
2932

3033
def add_delta_callbacks
@@ -40,8 +43,9 @@ def add_delta_callbacks
4043
end
4144

4245
def add_real_time_callbacks
43-
model.after_save ThinkingSphinx::RealTime.callback_for(
44-
reference, path, &block
46+
model.after_commit(
47+
ThinkingSphinx::RealTime.callback_for(reference, path, &block),
48+
on: [:create, :update]
4549
)
4650
end
4751

spec/acceptance/sql_deltas_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
sleep 0.25
1818

19-
expect(Book.search('Terry Pratchett').to_a).to eq([guards, men])
19+
expect(Book.search('Terry Pratchett').to_a).to match_array([guards, men])
2020
end
2121

2222
it "automatically indexes updated records" do

spec/thinking_sphinx/active_record/index_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
describe ThinkingSphinx::ActiveRecord::Index do
66
let(:index) { ThinkingSphinx::ActiveRecord::Index.new :user }
77
let(:config) { double('config', :settings => {},
8-
:indices_location => 'location', :next_offset => 8) }
8+
:indices_location => 'location', :next_offset => 8,
9+
:index_set_class => index_set_class) }
10+
let(:index_set_class) { double :reference_name => :user }
911

1012
before :each do
1113
allow(ThinkingSphinx::Configuration).to receive_messages :instance => config

0 commit comments

Comments
 (0)