app/views/shared/_batches.html.erb
<% unless batches.empty? %>
<%= pagination batches if showing_all %>
<%# We call to_a here as otherwise rails tries to 'optimise' the query with a nested scope. Not
# only would this be less eficient, it is also invalid SQL.
# We also need to build the cache manualy, at least until rails provides eager_count. You CAN do it with
# custom selects and joins, but that gets a little messy.
%>
<% request_count_cache = BatchRequest.where(batch_id:batches.to_a).group(:batch_id).count %>
<table class="table table-striped">
<thead>
<tr>
<th>Batch</th>
<th>Items</th>
<th>Created by</th>
<th>Assigned to</th>
<th>Created at</th>
<th>Status</th>
<% if in_progress %>
<th>Last completed task</th>
<% end %>
<th> </th>
</tr>
</thead>
<tbody>
<% batches.each_with_index do |batch, index| %>
<tr class="batch">
<td><%= link_to batch.id, batch_path(batch), {title: "#{batch.state} batch #{batch.id}"} %></td>
<td><%= request_count_cache[batch.id]||0 %></td>
<td><%= batch.user.blank? ? "Unknown" : batch.user.login %></td>
<td><%= batch.assigned_user %></td>
<td><%= batch.created_at.to_date.to_formatted_s(:rfc822) %></td>
<td>
<% if batch.failed? %>
<%= batch.state.capitalize %>
<% unless batch.failures.blank? %>
(<%= batch.failures.first.reason %>) <!-- TODO: Make sure a reason is being saved when failing a batch -->
<% end %>
<% else %>
<%= batch.state.humanize %>
<% end %>
</td>
<% if in_progress %>
<% unless batch.last_completed_task.nil? %>
<td><%= batch.last_completed_task.name %></td>
<% else %>
<% if batch.started? %>
<td>Only just started</td>
<% elsif batch.pending? %>
<td>Pending</td>
<% else %>
<td></td>
<% end %>
<% end %>
<% end %>
<td>
<% if batch.pipeline %>
<% if batch.state == "completed" -%>
<%= link_to "Release batch", release_batch_url(batch) %>
<% else -%>
<%= link_to("View batch", batch_path(batch), title: "View #{batch_state} batch #{index+1}") %>
<% end -%>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="card-body">
<% if @pipeline %>
<%= link_to "View all #{batches.first.state} batches", controller: :batches, action: batches.first.state, pipeline_id: @pipeline.id %>
<% end %>
</div>
<% else -%>
<div class="card-body"><p>No batches have this status</p></div>
<% end -%>