Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

6.1 KB
Raw Blame
<%#
# Collection

This partial is used on the `index` and `show` pages
to display a collection of resources in an HTML table.

## Local variables:

- `collection_presenter`:
  An instance of [Administrate::Page::Collection][1].
  The table presenter uses `ResourceDashboard::COLLECTION_ATTRIBUTES` to determine
  the columns displayed in the table
- `resources`:
  An ActiveModel::Relation collection of resources to be displayed in the table.
  By default, the number of resources is limited by pagination
  or by a hard limit to prevent excessive page load times
- `page` and `collection_field_name`:
  The current page and the param namespace its sort links use.
  Administrate's has_many field also passes `table_title` and `resource_class`, which this partial does not use.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>

<%
  resource_name = collection_presenter.resource_name
  # The avatar has no column of its own: it sits in front of the name.
  columns = collection_presenter.attribute_types.reject { |_name, type| type == AvatarField }
  cell_styles = { "number" => "font-mono text-xs text-n-slate-11", "count-field" => "tabular-nums", "date-time" => "tabular-nums text-n-slate-11" }
%>

<div class="rounded-xl border border-n-weak bg-n-solid-1 overflow-hidden">
  <div class="relative overflow-x-auto">
    <%= render "table", caption: display_resource_name(resource_name) do %>
      <thead>
        <tr>
          <% columns.each_key do |attr_name| %>
            <% direction = collection_presenter.ordered_html_class(attr_name) %>
            <th scope="col" aria-sort="<%= { asc: "ascending", desc: "descending" }.fetch(direction, "none") %>">
              <%= link_to(
                    sanitized_order_params(page, collection_field_name).merge(collection_presenter.order_params_for(attr_name, key: collection_field_name)),
                    class: class_names("inline-flex items-center gap-1 hover:text-n-slate-12", "text-n-slate-12" => direction.present?)
                  ) do %>
                <%= t("helpers.label.#{resource_name}.#{attr_name}", default: attr_name.to_s).titleize %>
                <% if direction.present? %>
                  <span class="<%= direction == :asc ? "i-lucide-arrow-up" : "i-lucide-arrow-down" %> size-3.5"></span>
                <% end %>
              <% end %>
            </th>
          <% end %>
          <th scope="col"><span class="sr-only"><%= t("super_admin.collection.actions") %></span></th>
        </tr>
      </thead>

      <tbody>
        <% resources.each do |resource| %>
          <%# A SuperAdmin is a User without routes of its own. %>
          <% record = resource.is_a?(User) ? resource.becomes(User) : resource %>
          <% show_path = polymorphic_path([namespace, record]) if existing_action?(resource_name, :show) %>
          <%# `js-table-row` and `data-url` are what Administrate's table.js opens a row with. %>
          <%= tag.tr class: class_names("js-table-row", "cursor-pointer hover:bg-n-alpha-2 focus-visible:bg-n-alpha-2 focus-visible:outline-none" => show_path),
                     tabindex: ("0" if show_path), role: ("link" if show_path), data: { url: show_path } do %>
            <% attributes = collection_presenter.attributes_for(resource) %>
            <% avatar = attributes.find { |attribute| attribute.is_a?(AvatarField) } %>
            <% (attributes - [avatar]).each do |attribute| %>
              <td class="<%= class_names("max-w-80 truncate", cell_styles[attribute.html_class]) %>">
                <% cell = capture do %>
                  <% if attribute.name == "name" && avatar %>
                    <%= render_field avatar %>
                  <% elsif attribute.name == "name" %>
                    <span class="me-2 inline-grid size-7 place-items-center rounded-lg border border-n-weak bg-n-slate-3 align-middle text-xs font-semibold uppercase text-n-slate-11"><%= attribute.data.to_s.first %></span>
                  <% end %>
                  <%= render_field attribute %>
                <% end %>
                <%# The row is the keyboard target; cell links only carry the click, and a cell with its own link or button is left alone. %>
                <% if show_path && authorized_action?(resource, :show) && cell.present? && !cell.match?(/<(a|button)[\s>]/) %>
                  <a href="<%= show_path %>" class="text-n-slate-12" tabindex="-1"><%= cell %></a>
                <% else %>
                  <%= cell %>
                <% end %>
              </td>
            <% end %>

            <td class="w-px">
              <%# The icons let clicks through to the link: Administrate's table.js opens the row unless the click target has an href. %>
              <div class="flex justify-end gap-1.5">
                <%= render(
                  "button",
                  icon: "i-lucide-pencil pointer-events-none size-4",
                  url: [:edit, namespace, record],
                  variant: :faded,
                  size: :icon,
                  title: t("administrate.actions.edit"),
                  aria: { label: t("administrate.actions.edit") }
                ) if existing_action?(resource_name, :edit) && authorized_action?(resource, :edit) %>
                <%= render(
                  "button",
                  icon: "i-lucide-trash-2 pointer-events-none size-4",
                  url: [namespace, record],
                  variant: :faded_danger,
                  size: :icon,
                  title: t("administrate.actions.destroy"),
                  aria: { label: t("administrate.actions.destroy") },
                  method: :delete,
                  data: { confirm: t("administrate.actions.confirm") }
                ) if existing_action?(resource_name, :destroy) && authorized_action?(resource, :destroy) %>
              </div>
            </td>
          <% end %>
        <% end %>

        <% if resources.empty? %>
          <tr>
            <td colspan="<%= columns.size + 1 %>" class="!py-10 text-center text-n-slate-11">
              <%= t("super_admin.collection.empty", resource: display_resource_name(resource_name).downcase) %>
            </td>
          </tr>
        <% end %>
      </tbody>
    <% end %>
  </div>
</div>