Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix table row link feature

Damian Legawiec69c47857d5f9
diff --git a/admin/app/helpers/spree/admin/table_helper.rb b/admin/app/helpers/spree/admin/table_helper.rb
index d43bbb0f5c..9d1aae2618 100644
--- a/admin/app/helpers/spree/admin/table_helper.rb
+++ b/admin/app/helpers/spree/admin/table_helper.rb
@@ -59,7 +59,7 @@ module Spree
when :boolean
render_boolean_column(value, column)
when :link
- render_link_column(record, value, column)
+ render_link_column(record, value, column, table)
when :image
render_image_column(record, value, column)
when :custom
@@ -148,11 +148,23 @@ module Spree
# @param record [Object] the record
# @param value [Object] the value
# @param column [Spree::Admin::Table::Column] column definition
+ # @param table [Spree::Admin::Table] the table
# @return [String]
- def render_link_column(record, value, column)
+ def render_link_column(record, value, column, table = nil)
return content_tag(:span, '-', class: 'text-gray-400') if value.blank?
- h(value.to_s.truncate(100))
+ url = nil
+ if table&.link_to_action == :show && respond_to?(:object_url)
+ url = object_url(record) rescue nil
+ elsif respond_to?(:edit_object_url)
+ url = edit_object_url(record) rescue nil
+ end
+
+ if url
+ link_to h(value.to_s.truncate(100)), url, data: { turbo_frame: '_top' }
+ else
+ h(value.to_s.truncate(100))
+ end
end
# Render image column
diff --git a/admin/app/models/spree/admin/table.rb b/admin/app/models/spree/admin/table.rb
index 828369629c..1e7a9b0d45 100644
--- a/admin/app/models/spree/admin/table.rb
+++ b/admin/app/models/spree/admin/table.rb
@@ -3,9 +3,9 @@ module Spree
class Table
attr_reader :columns, :bulk_actions, :context, :model_class
attr_accessor :search_param, :search_placeholder, :row_actions, :row_actions_edit, :row_actions_delete, :new_resource,
- :date_range_param, :date_range_label
+ :date_range_param, :date_range_label, :link_to_action
- def initialize(context, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil)
+ def initialize(context, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit)
@context = context
@model_class = model_class
@columns = {}
@@ -18,6 +18,7 @@ module Spree
@new_resource = new_resource
@date_range_param = date_range_param
@date_range_label = date_range_label
+ @link_to_action = link_to_action
end
# Check if date range filter is enabled
@@ -255,7 +256,8 @@ module Spree
row_actions_delete: row_actions_delete,
new_resource: new_resource,
date_range_param: date_range_param,
- date_range_label: date_range_label
+ date_range_label: date_range_label,
+ link_to_action: link_to_action
)
@columns.each do |key, column|
cloned.columns[key] = column.deep_clone
diff --git a/admin/app/views/spree/admin/tables/_table.html.erb b/admin/app/views/spree/admin/tables/_table.html.erb
index f4901ac7c7..fb98579a46 100644
--- a/admin/app/views/spree/admin/tables/_table.html.erb
+++ b/admin/app/views/spree/admin/tables/_table.html.erb
@@ -54,8 +54,7 @@
columns: columns,
table: table,
bulk_operations: bulk_operations,
- sortable: sortable,
- link_to_edit: edit_object_url != nil %>
+ sortable: sortable %>
<%= render 'spree/admin/shared/index_table_options', collection: collection %>
<% else %>
diff --git a/admin/app/views/spree/admin/tables/_table_body.html.erb b/admin/app/views/spree/admin/tables/_table_body.html.erb
index 143af2f55f..2de7ba3363 100644
--- a/admin/app/views/spree/admin/tables/_table_body.html.erb
+++ b/admin/app/views/spree/admin/tables/_table_body.html.erb
@@ -1,4 +1,4 @@
-<%# locals: (collection:, columns:, table:, bulk_operations: false, sortable: false, link_to_edit: true) %>
+<%# locals: (collection:, columns:, table:, bulk_operations: false, sortable: false) %>
<% has_bulk_actions = bulk_operations && table.bulk_operations_enabled? %>
<% has_row_actions = table.row_actions? %>
<% if has_bulk_actions %>
@@ -46,8 +46,7 @@
table: table,
bulk_operations: has_bulk_actions,
sortable: sortable,
- row_actions: has_row_actions,
- link_to_edit: link_to_edit
+ row_actions: has_row_actions
} %>
</tbody>
</table>
@@ -90,8 +89,7 @@
table: table,
bulk_operations: false,
sortable: sortable,
- row_actions: has_row_actions,
- link_to_edit: link_to_edit
+ row_actions: has_row_actions
} %>
</tbody>
</table>
diff --git a/admin/app/views/spree/admin/tables/_table_row.html.erb b/admin/app/views/spree/admin/tables/_table_row.html.erb
index 88909e4f9b..5641144670 100644
--- a/admin/app/views/spree/admin/tables/_table_row.html.erb
+++ b/admin/app/views/spree/admin/tables/_table_row.html.erb
@@ -1,4 +1,12 @@
-<%# locals: (record:, columns:, table:, bulk_operations: false, sortable: false, row_actions: true, link_to_edit: true) %>
+<%# locals: (record:, columns:, table:, bulk_operations: false, sortable: false, row_actions: true) %>
+<%
+ row_link_url = nil
+ if table.link_to_action == :show && respond_to?(:object_url)
+ row_link_url = object_url(record) rescue nil
+ elsif respond_to?(:edit_object_url)
+ row_link_url = edit_object_url(record) rescue nil
+ end
+%>
<tr id="<%= dom_id record %>" class="group"<% if sortable %> data-sortable-update-url="<%= edit_object_url(record) %>"<% end %>>
<% if sortable %>
<td class="w-5 move-handle text-center">
@@ -12,18 +20,20 @@
</td>
<% end %>
- <% columns.each_with_index do |column, index| %>
- <td class="<%= column_cell_class(column) %>" id="<%= dom_id record, column.key %>">
- <% if index == 0 && link_to_edit %>
- <%= link_to edit_object_url(record),
- class: 'flex items-center w-full no-underline hover:text-primary',
- data: { 'turbo-frame': '_top' } do %>
- <%= render_column_value(record, column, table) %>
- <% end %>
- <% else %>
+ <% columns.each do |column| %>
+ <% if row_link_url %>
+ <td class="<%= column_cell_class(column) %> cursor-pointer"
+ id="<%= dom_id record, column.key %>"
+ data-controller="row-link"
+ data-action="click->row-link#openLink">
+ <%= link_to '', row_link_url, class: 'hidden', data: { row_link_target: 'link', turbo_frame: '_top' } %>
<%= render_column_value(record, column, table) %>
- <% end %>
- </td>
+ </td>
+ <% else %>
+ <td class="<%= column_cell_class(column) %>" id="<%= dom_id record, column.key %>">
+ <%= render_column_value(record, column, table) %>
+ </td>
+ <% end %>
<% end %>
<% if row_actions %>
diff --git a/admin/app/views/spree/admin/tables/columns/_product_name.html.erb b/admin/app/views/spree/admin/tables/columns/_product_name.html.erb
index 4703828e51..d2a21c2dfe 100644
--- a/admin/app/views/spree/admin/tables/columns/_product_name.html.erb
+++ b/admin/app/views/spree/admin/tables/columns/_product_name.html.erb
@@ -1,8 +1,8 @@
<%# locals: (record:, column:, value:) %>
<% product = record %>
-<div class="flex items-center gap-3">
+<%= link_to edit_object_url(product), class: 'flex items-center gap-3 no-underline', data: { turbo_frame: '_top' } do %>
<%= render 'spree/admin/shared/product_image', object: product %>
<span class="text-gray-900 font-medium">
<%= product.name %>
</span>
-</div>
+<% end %>
diff --git a/admin/config/initializers/spree_admin_tables.rb b/admin/config/initializers/spree_admin_tables.rb
index 5857f7c28f..b6db7b3aee 100644
--- a/admin/config/initializers/spree_admin_tables.rb
+++ b/admin/config/initializers/spree_admin_tables.rb
@@ -408,7 +408,7 @@ Rails.application.config.after_initialize do
position: 70
# Register Users table
- Spree.admin.tables.register(:users, model_class: Spree.user_class, search_param: :multi_search, row_actions: false)
+ Spree.admin.tables.register(:users, model_class: Spree.user_class, search_param: :multi_search, row_actions: false, link_to_action: :show)
# User name with avatar
Spree.admin.tables.users.add :name,
@@ -528,7 +528,7 @@ Rails.application.config.after_initialize do
condition: -> { can?(:manage_tags, Spree.user_class) }
# Register Promotions table
- Spree.admin.tables.register(:promotions, model_class: Spree::Promotion, search_param: :name_cont)
+ Spree.admin.tables.register(:promotions, model_class: Spree::Promotion, search_param: :name_cont, link_to_action: :show)
Spree.admin.tables.promotions.add :name,
label: :name,
@@ -785,7 +785,7 @@ Rails.application.config.after_initialize do
method: ->(policy) { policy.owner.name }
# Register Stock Transfers table
- Spree.admin.tables.register(:stock_transfers, model_class: Spree::StockTransfer, search_param: :number_or_reference_cont)
+ Spree.admin.tables.register(:stock_transfers, model_class: Spree::StockTransfer, search_param: :number_or_reference_cont, link_to_action: :show)
Spree.admin.tables.stock_transfers.add :number,
label: :number,
@@ -903,7 +903,7 @@ Rails.application.config.after_initialize do
}
# Register Gift Cards table
- Spree.admin.tables.register(:gift_cards, model_class: Spree::GiftCard, search_param: :code_i_cont, row_actions: false)
+ Spree.admin.tables.register(:gift_cards, model_class: Spree::GiftCard, search_param: :code_i_cont, row_actions: false, link_to_action: :show)
Spree.admin.tables.gift_cards.add :code,
label: :code,
diff --git a/admin/lib/spree/admin/engine.rb b/admin/lib/spree/admin/engine.rb
index a21f4802e0..3fcb33877b 100644
--- a/admin/lib/spree/admin/engine.rb
+++ b/admin/lib/spree/admin/engine.rb
@@ -195,8 +195,9 @@ module Spree
# @param new_resource [Boolean] Whether to show "Create new" button in empty state (default: true)
# @param date_range_param [Symbol, nil] Ransack parameter base for date range filter (e.g., :completed_at)
# @param date_range_label [String, nil] Label for date range filter
+ # @param link_to_action [Symbol] Action to link to for :link columns (:edit or :show, default: :edit)
# @return [Spree::Admin::Table] The table instance
- def register(name, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil)
+ def register(name, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit)
name = name.to_sym
@registries[name] ||= Spree::Admin::Table.new(
name,
@@ -208,7 +209,8 @@ module Spree
row_actions_delete: row_actions_delete,
new_resource: new_resource,
date_range_param: date_range_param,
- date_range_label: date_range_label
+ date_range_label: date_range_label,
+ link_to_action: link_to_action
)
end