You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,38 @@ But since we're devout followers of the SOLID principles, we can define a sort c
142
142
which returns the sorted collection. Under the hood the sort class is initialized with the current scope and the
143
143
direction parameter.
144
144
145
+
#### Dynamic sorting (prefix-based)
146
+
147
+
Sometimes you want to allow sorting by a dynamic subset of attributes that share a common prefix (e.g., JSON/JSONB keys, translated columns, join records). You can register a dynamic sort by attribute prefix using `dynamically_sorts_by`.
148
+
149
+
- The configured prefix is matched against each parsed sort attribute.
150
+
- The prefix is stripped and only the dynamic part is passed to your sort handler.
151
+
- You can provide either a lambda/proc or a class. The callable receives `(collection, dynamic_attribute, direction)`.
152
+
153
+
Example with a lambda (PostgreSQL JSONB text value):
154
+
155
+
```ruby
156
+
# Allows sorting by any key in the `data` column: e.g. sort=-data.name,data.created_at
# Registers an attribute prefix that can be dynamically used for sorting. Attribute prefix is stripped from parsed sort parameter and passed to the sort proc or class.
58
+
# @param [Symbol] attribute_prefix The "sortable" attribute prefix, e.g. `:'data.'` for sorting by `data.name` and `data.created_at`
59
+
# @param [proc, Class] sort A proc or a sort class, defaults to a simple order(attribute => direction)
0 commit comments