Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Use saved_changes if it’s present.
Browse files Browse the repository at this point in the history
This change occured in ActiveRecord/Rails 5.1. Hunting for the deprecation raised in pat#1059 helped me debug this one (though that issue’s not yet resolved).
  • Loading branch information
pat authored and h3nnn4n committed May 14, 2019
1 parent 049ee6c commit 442035f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks <
ThinkingSphinx::Callbacks

if ActiveRecord::Base.instance_methods.grep(/saved_changes/).any?
CHANGED_ATTRIBUTES = lambda { |instance| instance.saved_changes.keys }
else
CHANGED_ATTRIBUTES = lambda { |instance| instance.changed }
end

callbacks :after_update

def after_update
Expand All @@ -15,14 +21,18 @@ def after_update

def attributes_hash_for(index)
updateable_attributes_for(index).inject({}) do |hash, attribute|
if instance.changed.include?(attribute.columns.first.__name.to_s)
if changed_attributes.include?(attribute.columns.first.__name.to_s)
hash[attribute.name] = attribute.value_for(instance)
end

hash
end
end

def changed_attributes
@changed_attributes ||= CHANGED_ATTRIBUTES.call instance
end

def configuration
ThinkingSphinx::Configuration.instance
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ module Callbacks; end
double(:name => 'baz', :updateable? => false)
])

allow(instance).to receive_messages :changed => ['bar_column', 'baz'], :bar_column => 7
allow(instance).to receive_messages(
:changed => ['bar_column', 'baz'],
:bar_column => 7,
:saved_changes => {'bar_column' => [1, 2], 'baz' => [3, 4]}
)
end

it "does not send any updates to Sphinx if updates are disabled" do
Expand Down

0 comments on commit 442035f

Please sign in to comment.