From 442035f6d26c27b4682c8fdfe6451ed6abfadc9d Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Sat, 10 Jun 2017 22:24:52 +1000 Subject: [PATCH] =?UTF-8?q?Use=20saved=5Fchanges=20if=20it=E2=80=99s=20pre?= =?UTF-8?q?sent.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change occured in ActiveRecord/Rails 5.1. Hunting for the deprecation raised in #1059 helped me debug this one (though that issue’s not yet resolved). --- .../active_record/callbacks/update_callbacks.rb | 12 +++++++++++- .../active_record/callbacks/update_callbacks_spec.rb | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb b/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb index 61d83bd1a..b395b1935 100644 --- a/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb +++ b/lib/thinking_sphinx/active_record/callbacks/update_callbacks.rb @@ -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 @@ -15,7 +21,7 @@ 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 @@ -23,6 +29,10 @@ def attributes_hash_for(index) end end + def changed_attributes + @changed_attributes ||= CHANGED_ATTRIBUTES.call instance + end + def configuration ThinkingSphinx::Configuration.instance end diff --git a/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb b/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb index 5f7cef1c5..a56c2dd2a 100644 --- a/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb +++ b/spec/thinking_sphinx/active_record/callbacks/update_callbacks_spec.rb @@ -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