diff --git a/lib/algoliasearch-rails.rb b/lib/algoliasearch-rails.rb index 8a01df2..3e41571 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] @@ -624,7 +625,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 13081ba..21eb8b0 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 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index cf67154..38fbfe8 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -1561,3 +1561,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