From f86892048277db370930bc629bb1bef9e19938e3 Mon Sep 17 00:00:00 2001 From: Jenna Blumenthal Date: Thu, 24 Aug 2023 10:20:32 -0400 Subject: [PATCH] Evaluate metadata block in the context of the controller (#873) --- Gemfile.lock | 2 +- README.md | 4 ++-- app/controllers/maintenance_tasks/runs_controller.rb | 2 +- maintenance_tasks.gemspec | 2 +- test/system/maintenance_tasks/runs_test.rb | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 81f596a5..753c66d6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - maintenance_tasks (2.3.0) + maintenance_tasks (2.3.1) actionpack (>= 6.0) activejob (>= 6.0) activerecord (>= 6.0) diff --git a/README.md b/README.md index 99f5c8fe..ce6b8725 100644 --- a/README.md +++ b/README.md @@ -840,8 +840,8 @@ or email of the user who performed the maintenance task. ```ruby # config/initializers/maintenance_tasks.rb -MaintenanceTasks.metadata = -> do - { user_email: current_user.email } +MaintenanceTasks.metadata = ->(context) do + { user_email: context.current_user.email } end ``` diff --git a/app/controllers/maintenance_tasks/runs_controller.rb b/app/controllers/maintenance_tasks/runs_controller.rb index b66d6ce6..58b7ef5d 100644 --- a/app/controllers/maintenance_tasks/runs_controller.rb +++ b/app/controllers/maintenance_tasks/runs_controller.rb @@ -14,7 +14,7 @@ def create(&block) name: params.fetch(:task_id), csv_file: params[:csv_file], arguments: params.fetch(:task_arguments, {}).permit!.to_h, - metadata: MaintenanceTasks.metadata&.call, + metadata: instance_exec(&MaintenanceTasks.metadata || -> {}), &block ) redirect_to(task_path(task)) diff --git a/maintenance_tasks.gemspec b/maintenance_tasks.gemspec index 7b4c556a..61a5a0ff 100644 --- a/maintenance_tasks.gemspec +++ b/maintenance_tasks.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |spec| spec.name = "maintenance_tasks" - spec.version = "2.3.0" + spec.version = "2.3.1" spec.author = "Shopify Engineering" spec.email = "gems@shopify.com" spec.homepage = "https://github.com/Shopify/maintenance_tasks" diff --git a/test/system/maintenance_tasks/runs_test.rb b/test/system/maintenance_tasks/runs_test.rb index 75ee1ea1..c45c95f7 100644 --- a/test/system/maintenance_tasks/runs_test.rb +++ b/test/system/maintenance_tasks/runs_test.rb @@ -21,7 +21,7 @@ class RunsTest < ApplicationSystemTestCase assert_equal "enqueued", run.status end - test "run a Task and log the user email" do + test "run a Task and log the provided metadata" do MaintenanceTasks.metadata = -> { { user_email: "michael.elfassy@shopify.com" } } visit(maintenance_tasks_path)