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.

Whitelist position globally for ransack; lock sort when reorderable

Damian Legawiec5d63ce2ceb85
diff --git a/packages/admin/src/components/spree/resource-table.tsx b/packages/admin/src/components/spree/resource-table.tsx
index 7474f6a132..89c07f8934 100644
--- a/packages/admin/src/components/spree/resource-table.tsx
+++ b/packages/admin/src/components/spree/resource-table.tsx
@@ -176,12 +176,17 @@ export function ResourceTable<T extends Record<string, any>>({
columns: urlColumns,
} = searchParams
+ // When the table is reorderable, sort is locked to the position field
+ // ascending — anything else makes drag-and-drop meaningless. Otherwise
+ // fall through to the URL params, then the table's declared default,
+ // then a sensible fallback.
+ const positionField = reorder?.positionField ?? 'position'
const defaultSort = table.defaultSort ?? {
field: 'updated_at',
direction: 'desc' as const,
}
- const sort = urlSort ?? defaultSort.field
- const dir = urlDir ?? defaultSort.direction
+ const sort = reorder ? positionField : (urlSort ?? defaultSort.field)
+ const dir: 'asc' | 'desc' = reorder ? 'asc' : (urlDir ?? defaultSort.direction)
const [searchInput, setSearchInput] = useState(search ?? '')
const deferredSearch = useDeferredValue(searchInput)
@@ -232,8 +237,7 @@ export function ResourceTable<T extends Record<string, any>>({
setLocalRows(fetchedRows)
}, [fetchedRows])
- const positionField = reorder?.positionField ?? 'position'
- const reorderActive = !!reorder && sort === positionField && dir === 'asc'
+ const reorderActive = !!reorder
const rows = reorderActive ? localRows : fetchedRows
// dnd-kit sensors: pointer for mouse/touch (5px activation distance keeps
@@ -332,6 +336,7 @@ export function ResourceTable<T extends Record<string, any>>({
allColumns={table.columns}
title={title ?? table.title}
actions={resolvedActions}
+ hideSort={reorderActive}
/>
<CardContent className="p-0">
<Table>
diff --git a/packages/admin/src/components/spree/table-toolbar.tsx b/packages/admin/src/components/spree/table-toolbar.tsx
index 009f7c35f2..d6e8a3717f 100644
--- a/packages/admin/src/components/spree/table-toolbar.tsx
+++ b/packages/admin/src/components/spree/table-toolbar.tsx
@@ -50,6 +50,8 @@ interface TableToolbarProps {
/** Title displayed in the toolbar header */
title?: string
actions?: React.ReactNode
+ /** Hide the sort dropdown — used when the table is drag-reorderable, where free sorting would defeat the drag. */
+ hideSort?: boolean
}
// ============================================================================
@@ -113,6 +115,7 @@ export function TableToolbar({
allColumns,
title,
actions,
+ hideSort = false,
}: TableToolbarProps) {
const [filterOpen, setFilterOpen] = useState(false)
const searchRef = useRef<HTMLInputElement>(null)
@@ -185,8 +188,8 @@ export function TableToolbar({
</Popover>
)}
- {/* Sort dropdown */}
- {sortableColumns.length > 0 && (
+ {/* Sort dropdown — hidden when the table is drag-reorderable, since free sorting would defeat the drag. */}
+ {!hideSort && sortableColumns.length > 0 && (
<SortDropdown columns={sortableColumns} sort={sort} onSortChange={onSortChange} />
)}
diff --git a/spree/core/app/models/concerns/spree/ransackable_attributes.rb b/spree/core/app/models/concerns/spree/ransackable_attributes.rb
index 6cd245a917..bc2d0c520a 100644
--- a/spree/core/app/models/concerns/spree/ransackable_attributes.rb
+++ b/spree/core/app/models/concerns/spree/ransackable_attributes.rb
@@ -6,7 +6,11 @@ module Spree::RansackableAttributes
class_attribute :whitelisted_ransackable_scopes
class_attribute :default_ransackable_attributes
- self.default_ransackable_attributes = %w[id name updated_at created_at]
+ # `position` is included so any `acts_as_list` model is sortable by it
+ # without each subclass having to opt in. Ransack ignores attributes
+ # the model doesn't actually expose, so this is a no-op for tables
+ # without a position column.
+ self.default_ransackable_attributes = %w[id name updated_at created_at position]
def self.ransackable_associations(*_args)
base = whitelisted_ransackable_associations || []