From 3ffa7e35c669119f5ddfa134ce32fcc820ccaf4b Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Mon, 16 Sep 2024 22:46:29 -0300 Subject: [PATCH] fix: when defining custom task on index level, keep the ones from global --- Gemfile.lock | 2 +- lib/esse/async_indexing/tasks.rb | 6 ++++++ lib/esse/async_indexing/version.rb | 2 +- lib/esse/plugins/async_indexing.rb | 2 +- spec/esse/async_indexing/tasks_spec.rb | 9 +++++++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 99f248d..2871075 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,7 +10,7 @@ GIT PATH remote: . specs: - esse-async_indexing (0.1.0.rc3) + esse-async_indexing (0.1.0.rc4) background_job esse (>= 0.4.0.rc1) multi_json diff --git a/lib/esse/async_indexing/tasks.rb b/lib/esse/async_indexing/tasks.rb index 041ebd5..9405342 100644 --- a/lib/esse/async_indexing/tasks.rb +++ b/lib/esse/async_indexing/tasks.rb @@ -64,6 +64,12 @@ def fetch(name) end alias_method :[], :fetch + def dup + new_task = self.class.new + new_task.instance_variable_set(:@tasks, @tasks.dup) + new_task + end + private def validate!(names, block) diff --git a/lib/esse/async_indexing/version.rb b/lib/esse/async_indexing/version.rb index 0c03bdc..95be3e0 100755 --- a/lib/esse/async_indexing/version.rb +++ b/lib/esse/async_indexing/version.rb @@ -2,6 +2,6 @@ module Esse module AsyncIndexing - VERSION = "0.1.0.rc3" + VERSION = "0.1.0.rc4" end end diff --git a/lib/esse/plugins/async_indexing.rb b/lib/esse/plugins/async_indexing.rb index 81fd696..b2c2022 100644 --- a/lib/esse/plugins/async_indexing.rb +++ b/lib/esse/plugins/async_indexing.rb @@ -45,7 +45,7 @@ def implement_batch_ids? # MyCustomJob.perform_later(repo.index.name, [id], **kwargs) # end def async_indexing_job(*operations, &block) - definer = @async_indexing_tasks || Esse::AsyncIndexing::Tasks.new + definer = @async_indexing_tasks || Esse.config.async_indexing.tasks.dup definer.define(*operations, &block) @async_indexing_tasks = definer end diff --git a/spec/esse/async_indexing/tasks_spec.rb b/spec/esse/async_indexing/tasks_spec.rb index 22c2bf7..d4d8186 100644 --- a/spec/esse/async_indexing/tasks_spec.rb +++ b/spec/esse/async_indexing/tasks_spec.rb @@ -85,4 +85,13 @@ it_behaves_like "a task fetch for", :delete it_behaves_like "a task fetch for", :update_lazy_attribute end + + describe "#dup" do + it "duplicates the tasks" do + import_task = ->(**) {} + tasks.define(:import, &import_task) + new_tasks = tasks.dup + expect(new_tasks[:import]).to eq(import_task) + end + end end