Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customizable extension partials to good_job/jobs#show view #1200

Merged
merged 9 commits into from
Feb 22, 2024
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ For more of the story of GoodJob, read the [introductory blog post](https://isla
- [Dashboard](#dashboard)
- [API-only Rails applications](#api-only-rails-applications)
- [Live polling](#live-polling)
- [Extending dashboard views](#extending-dashboard-views)
- [Job priority](#job-priority)
- [Concurrency controls](#concurrency-controls)
- [How concurrency controls work](#how-concurrency-controls-work)
Expand Down Expand Up @@ -451,6 +452,42 @@ end

The Dashboard can be set to automatically refresh by checking "Live Poll" in the Dashboard header, or by setting `?poll=10` with the interval in seconds (default 30 seconds).

#### Extending dashboard views

GoodJob exposes some views that are intended to be overriden by placing views in your application:

- [`app/views/good_job/jobs/_custom_job_details.html.erb`](app/views/good_job/_custom_job_details.html.erb): content added to this partial will be displayed above the argument list on the good_job/jobs#show page.
- [`app/views/good_job/jobs/_custom_execution_details.html.erb`](app/views/good_job/_custom_execution_details.html.erb): content added to this partial will be displayed above each execution on the good_job/jobs#show page.

**Warning:** these partials expose classes (such as `GoodJob::Job`) that are considered internal implementation details of GoodJob. You should always test your custom partials after upgrading GoodJob.

For example, if your app deals with widgets and you want to show a link to the widget a job acted on, you can add the following to `app/views/good_job/_custom_job_details.html.erb`:

```erb
<%# file: app/views/good_job/_custom_job_details.html.erb %>
<% arguments = job.active_job.arguments rescue [] %>
<% widgets = arguments.select { |arg| arg.is_a?(Widget) } %>
<% if widgets.any? %>
<div class="my-4">
<h5>Widgets</h5>
<ul>
<% widgets.each do |widget| %>
<li><%= link_to widget.name, main_app.widget_url(widget) %></li>
<% end %>
</ul>
</div>
<% end %>
```

As a second example, you may wish to show a link to a log aggregator next to each job execution. You can do this by adding the following to `app/views/good_job/_custom_execution_details.html.erb`:

```erb
<%# file: app/views/good_job/_custom_execution_details.html.erb %>
<div class="py-3">
<%= link_to "Logs", main_app.logs_url(filter: { job_id: job.id }, start_time: execution.performed_at, end_time: execution.finished_at + 1.minute) %>
</div>
```

### Job priority

Higher priority numbers run first in all versions of GoodJob v3.x and below. GoodJob v4.x will change job `priority` to give smaller numbers higher priority (default: `0`), in accordance with Active Job's definition of priority (see #524). To opt-in to this behavior now, set `config.good_job.smaller_number_is_higher_priority = true` in your GoodJob initializer or `application.rb`.
Expand Down
11 changes: 11 additions & 0 deletions app/views/good_job/_custom_execution_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%#
Content added to this partial will be displayed above the collapsible JSON representation of each execution on the jobs#show view.

You can make use of the following variables:

- `job`: The `GoodJob::Job` instance.
- `execution`: The `GoodJob::DiscreteExecution` instance.
- `main_app`: Use this to access helpers (e.g. route helpers) from your application.

Note: the `GoodJob::Job` and `GoodJob::Execution` classes are considered an internal implementation detail of GoodJob and may change at any time. Please test your view extensions when upgrading.
%>
10 changes: 10 additions & 0 deletions app/views/good_job/_custom_job_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%#
Content added to this partial will be displayed above the argument list on the good_job/jobs#show page.

You can make use of the following variables:

- `job`: The `GoodJob::Job` instance. Calling `job.active_job` will deserialize an instance of your ActiveJob class.
- `main_app`: Use this to access helpers (e.g. route helpers) from your application.

Note: the `GoodJob::Job` class is considered an internal implementation detail and may change at any time. Please test your view extensions when upgrading.
%>
1 change: 1 addition & 0 deletions app/views/good_job/jobs/_executions.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</div>
<% end %>
<% end %>
<%= render 'good_job/custom_execution_details', execution: execution, job: @job %>
<%= tag.div id: dom_id(execution, "params"), class: "list-group-item collapse small bg-dark text-light" do %>
<%= tag.pre JSON.pretty_generate(execution.display_serialized_params) %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/good_job/jobs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
</div>
</div>

<%= render 'good_job/custom_job_details', job: @job %>

<div class="my-4">
<h5>
<%= t "good_job.models.job.arguments" %>
Expand Down
1 change: 1 addition & 0 deletions demo/app/views/good_job/_custom_execution_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="custom-execution-details-for-demo"></div>
1 change: 1 addition & 0 deletions demo/app/views/good_job/_custom_job_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="custom-job-details-for-demo"></div>
22 changes: 22 additions & 0 deletions spec/system/custom_partials_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Custom partials' do
before do
ActiveJob::Base.queue_adapter = GoodJob::Adapter.new(execution_mode: :external)
ExampleJob.perform_later
end

it 'renders custom partials on the Job#show page' do
job = GoodJob::Job.last
visit good_job.job_path(job.id)
expect(page).to have_content 'ExampleJob'
expect(page).to have_css ".custom-job-details-for-demo"
expect(page).not_to have_css ".custom-execution-details-for-demo"

GoodJob.perform_inline
visit good_job.job_path(job.id)
expect(page).to have_css ".custom-execution-details-for-demo"
end
end
Loading