Skip to content

Commit

Permalink
Only toggle deltas if there have been changes.
Browse files Browse the repository at this point in the history
Or if the record’s a new one. Prompted by #1027.
  • Loading branch information
pat committed Nov 18, 2016
1 parent f5f9663 commit 6b0923c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb
Expand Up @@ -4,9 +4,7 @@ class ThinkingSphinx::ActiveRecord::Callbacks::DeltaCallbacks <
callbacks :after_commit, :before_save

def after_commit
return unless !suspended? && delta_indices? && processors.any? { |processor|
processor.toggled?(instance)
}
return unless !suspended? && delta_indices? && toggled?

delta_indices.each do |index|
index.delta_processor.index index
Expand All @@ -18,7 +16,9 @@ def after_commit
end

def before_save
return unless !ThinkingSphinx::Callbacks.suspended? && delta_indices?
return unless new_or_changed? &&
!ThinkingSphinx::Callbacks.suspended? &&
delta_indices?

processors.each { |processor| processor.toggle instance }
end
Expand All @@ -45,11 +45,19 @@ def indices
@indices ||= config.index_set_class.new :classes => [instance.class]
end

def new_or_changed?
instance.new_record? || instance.changed?
end

def processors
delta_indices.collect &:delta_processor
end

def suspended?
ThinkingSphinx::Callbacks.suspended? || ThinkingSphinx::Deltas.suspended?
end

def toggled?
processors.any? { |processor| processor.toggled?(instance) }
end
end
Expand Up @@ -130,6 +130,10 @@

before :each do
allow(config).to receive_messages :index_set_class => double(:new => [index])
allow(instance).to receive_messages(
:changed? => true,
:new_record? => false
)
end

it "sets delta to true if there are delta indices" do
Expand All @@ -145,5 +149,24 @@

callbacks.before_save
end

it "does not try to set delta to true if the instance is unchanged" do
allow(instance).to receive_messages :changed? => false

expect(processor).not_to receive(:toggle)

callbacks.before_save
end

it "does set delta to true if the instance is unchanged but new" do
allow(instance).to receive_messages(
:changed? => false,
:new_record? => true
)

expect(processor).to receive(:toggle)

callbacks.before_save
end
end
end

0 comments on commit 6b0923c

Please sign in to comment.