From 562aa52f4b07e7b483bbcb4d03690885c301c53a Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Thu, 12 Oct 2023 10:56:34 +0900 Subject: [PATCH] Show Run#metadata in the web UI As the example in the README, a common use case of metadata is to record who ran the task. By displaying metadata in the web UI, we can know who ran the task at a glance. --- .../runs/_arguments.html.erb | 24 +++-------------- .../maintenance_tasks/runs/_metadata.html.erb | 4 +++ .../maintenance_tasks/runs/_run.html.erb | 4 ++- .../runs/_serializable.html.erb | 26 +++++++++++++++++++ .../maintenance_tasks/tasks/_task.html.erb | 4 ++- test/system/maintenance_tasks/runs_test.rb | 19 ++++++++++++++ 6 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 app/views/maintenance_tasks/runs/_metadata.html.erb create mode 100644 app/views/maintenance_tasks/runs/_serializable.html.erb diff --git a/app/views/maintenance_tasks/runs/_arguments.html.erb b/app/views/maintenance_tasks/runs/_arguments.html.erb index ef9003335..e7f809bdb 100644 --- a/app/views/maintenance_tasks/runs/_arguments.html.erb +++ b/app/views/maintenance_tasks/runs/_arguments.html.erb @@ -1,22 +1,4 @@ -<% if run.arguments.present? %> -
-
Arguments:
- - - <% run.arguments.transform_values(&:to_s).each do |key, value| %> - - - - - <% end %> - -
<%= key %> - <% next if value.empty? %> - <% if value.include?("\n") %> -
<%= value %>
- <% else %> - <%= value %> - <% end %> -
-
+<% if arguments.present? %> +
Arguments:
+ <%= render "maintenance_tasks/runs/serializable", serializable: arguments %> <% end %> diff --git a/app/views/maintenance_tasks/runs/_metadata.html.erb b/app/views/maintenance_tasks/runs/_metadata.html.erb new file mode 100644 index 000000000..8e5a98924 --- /dev/null +++ b/app/views/maintenance_tasks/runs/_metadata.html.erb @@ -0,0 +1,4 @@ +<% if metadata.present? %> +
Metadata:
+ <%= render "maintenance_tasks/runs/serializable", serializable: metadata %> +<% end %> diff --git a/app/views/maintenance_tasks/runs/_run.html.erb b/app/views/maintenance_tasks/runs/_run.html.erb index f70496d96..61439b236 100644 --- a/app/views/maintenance_tasks/runs/_run.html.erb +++ b/app/views/maintenance_tasks/runs/_run.html.erb @@ -16,7 +16,9 @@ <%= render "maintenance_tasks/runs/csv", run: run %> <%= tag.hr if run.csv_file.present? && run.arguments.present? %> - <%= render "maintenance_tasks/runs/arguments", run: run %> + <%= render "maintenance_tasks/runs/arguments", arguments: run.arguments %> + <%= tag.hr if run.csv_file.present? || run.arguments.present? && run.metadata.present? %> + <%= render "maintenance_tasks/runs/metadata", metadata: run.metadata %>
<% if run.paused? %> diff --git a/app/views/maintenance_tasks/runs/_serializable.html.erb b/app/views/maintenance_tasks/runs/_serializable.html.erb new file mode 100644 index 000000000..6c79e3310 --- /dev/null +++ b/app/views/maintenance_tasks/runs/_serializable.html.erb @@ -0,0 +1,26 @@ +<% if serializable.present? %> + <% case serializable %> + <% when Hash %> +
+ + + <% serializable.transform_values(&:to_s).each do |key, value| %> + + + + + <% end %> + +
<%= key %> + <% next if value.empty? %> + <% if value.include?("\n") %> +
<%= value %>
+ <% else %> + <%= value %> + <% end %> +
+
+ <% else %> + <%= serializable.inspect %> + <% end %> +<% end %> diff --git a/app/views/maintenance_tasks/tasks/_task.html.erb b/app/views/maintenance_tasks/tasks/_task.html.erb index 614aebcbc..1fff619b8 100644 --- a/app/views/maintenance_tasks/tasks/_task.html.erb +++ b/app/views/maintenance_tasks/tasks/_task.html.erb @@ -21,6 +21,8 @@ <%= render "maintenance_tasks/runs/csv", run: run %> <%= tag.hr if run.csv_file.present? && run.arguments.present? %> - <%= render "maintenance_tasks/runs/arguments", run: run %> + <%= render "maintenance_tasks/runs/arguments", arguments: run.arguments %> + <%= tag.hr if run.csv_file.present? || run.arguments.present? && run.metadata.present? %> + <%= render "maintenance_tasks/runs/metadata", metadata: run.metadata %> <% end %>
diff --git a/test/system/maintenance_tasks/runs_test.rb b/test/system/maintenance_tasks/runs_test.rb index 54f05e569..403074e6d 100644 --- a/test/system/maintenance_tasks/runs_test.rb +++ b/test/system/maintenance_tasks/runs_test.rb @@ -32,6 +32,11 @@ class RunsTest < ApplicationSystemTestCase assert_title("Maintenance::UpdatePostsTask") assert_text("Enqueued") assert_text("Waiting to start.") + assert_text("Metadata") + assert_table do |table| + table.assert_text("user_email") + table.assert_text("michael.elfassy@shopify.com") + end end run = Run.last assert_equal("michael.elfassy@shopify.com", run.metadata["user_email"]) @@ -41,6 +46,20 @@ class RunsTest < ApplicationSystemTestCase MaintenanceTasks.metadata = nil end + test "metadata can be non-hash" do + MaintenanceTasks.metadata = -> { "hello metadata" } + visit(maintenance_tasks_path) + + assert_difference("Run.count") do + click_on("Maintenance::UpdatePostsTask") + click_on("Run") + + assert_text("hello metadata") + end + ensure + MaintenanceTasks.metadata = nil + end + test "run a CSV Task" do visit maintenance_tasks_path