From 3bc24daf288711d13634c619349e3a99c8d971af Mon Sep 17 00:00:00 2001 From: Duarte Henriques Date: Tue, 8 Mar 2022 13:09:11 +0000 Subject: [PATCH 1/2] Fix ActiveJob::DeserializationError issue --- lib/algoliasearch-rails.rb | 5 +++-- lib/algoliasearch/algolia_job.rb | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/algoliasearch-rails.rb b/lib/algoliasearch-rails.rb index 57fe9533..808c7bd5 100644 --- a/lib/algoliasearch-rails.rb +++ b/lib/algoliasearch-rails.rb @@ -399,7 +399,8 @@ def algoliasearch(options = {}, &block) raise ArgumentError.new("Cannot use a enqueue if the `synchronous` option if set") if options[:synchronous] proc = if options[:enqueue] == true Proc.new do |record, remove| - AlgoliaJob.perform_later(record, remove ? 'algolia_remove_from_index!' : 'algolia_index!') + record_or_object_id = remove ? algolia_object_id_of(record) : record + AlgoliaJob.perform_later(record.class.to_s, record_or_object_id, remove) end elsif options[:enqueue].respond_to?(:call) options[:enqueue] @@ -623,7 +624,7 @@ def algolia_index!(object, synchronous = false) def algolia_remove_from_index!(object, synchronous = false) return if algolia_without_auto_index_scope - object_id = algolia_object_id_of(object) + object_id = (object.is_a?(String) || object.is_a?(Numeric)) ? object : algolia_object_id_of(object) raise ArgumentError.new("Cannot index a record with a blank objectID") if object_id.blank? algolia_configurations.each do |options, settings| next if algolia_indexing_disabled?(options) diff --git a/lib/algoliasearch/algolia_job.rb b/lib/algoliasearch/algolia_job.rb index 13081ba2..21eb8b00 100644 --- a/lib/algoliasearch/algolia_job.rb +++ b/lib/algoliasearch/algolia_job.rb @@ -2,8 +2,14 @@ module AlgoliaSearch class AlgoliaJob < ::ActiveJob::Base queue_as :algoliasearch - def perform(record, method) - record.send(method) + def perform(record_class, record_or_object_id, remove) + if remove + object_id = record_or_object_id + record_class.constantize.algolia_remove_from_index!(object_id) + else + record = record_or_object_id + record.algolia_index! + end end end end From 2c547e29bc8c94917fa4cd4649a2a11186da32ef Mon Sep 17 00:00:00 2001 From: Duarte Henriques Date: Mon, 14 Mar 2022 10:45:46 +0000 Subject: [PATCH 2/2] Add spec for passing in id --- spec/integration_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 8373ad60..6b6e3504 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -1440,3 +1440,19 @@ class ForwardToReplicasTwo < ActiveRecord::Base }.to raise_error(ArgumentError) end end + +describe 'algolia_remove_from_index!' do + it "allows passing in the id as well" do + # index a Product + nokia = Product.create!(:name => 'nokia', :href => "google", :tags => ['decent']) + Product.reindex(AlgoliaSearch::IndexSettings::DEFAULT_BATCH_SIZE, true) + sleep 5 + + # verify it was indexed + expect(Product.search('nokia').size).to eq(1) + + # remove from index by passing in id rather than the record + Product.algolia_remove_from_index!(nokia.id, true) + expect(Product.search('nokia').size).to eq(0) + end +end