plutonium 0.26.6 → 0.26.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/guide/deep-dive/resources.md +3 -0
- data/docs/modules/query.md +10 -0
- data/docs/modules/table.md +8 -0
- data/lib/plutonium/definition/sorting.rb +18 -0
- data/lib/plutonium/resource/controllers/queryable.rb +4 -0
- data/lib/plutonium/resource/query_object.rb +31 -4
- data/lib/plutonium/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25a972b402c936a681cd59984869f8fa9fd08e6c5ad240578bc18c95c57cac45
|
4
|
+
data.tar.gz: ed73139eafd20795cb9a4be658c3f32a01c3546d51c37fd1f279eb9175697209
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9fd52c2b932a4cc852935d03cfe3a3a3e00453d7a73aaeba698219fee78dcd354ea5a7f6443ef7c5273e401b6ad746fa50dec03ab469f178958f17231368f94
|
7
|
+
data.tar.gz: 81075c91ca822c5dd4b7f8866b6b75e4f1557aae08dc70af8096ed167141e02269749f5e57b920bb0036a9cb9c8ca58fb4b0f090f923aae252b431310a7842bc
|
data/docs/modules/query.md
CHANGED
@@ -43,6 +43,9 @@ class PostDefinition < Plutonium::Resource::Definition
|
|
43
43
|
sort :title
|
44
44
|
sort :created_at
|
45
45
|
sort :view_count
|
46
|
+
|
47
|
+
# Define default sort (when no sort params are provided)
|
48
|
+
default_sort :created_at, :desc # or default_sort { |scope| scope.order(featured: :desc, created_at: :desc) }
|
46
49
|
end
|
47
50
|
```
|
48
51
|
|
@@ -273,6 +276,13 @@ end
|
|
273
276
|
- Consider performance impact of JOINs in search queries
|
274
277
|
- Use `.distinct` when searching across associations
|
275
278
|
|
279
|
+
### Default Sorting
|
280
|
+
- Define a default sort to show newest items first by default: `default_sort :id, :desc`
|
281
|
+
- Use field and direction: `default_sort :created_at, :desc`
|
282
|
+
- Or use a block for complex sorting: `default_sort { |scope| scope.order(featured: :desc, created_at: :desc) }`
|
283
|
+
- The default sort is only applied when no sort parameters are provided by the user
|
284
|
+
- Child definitions inherit the default sort from parent definitions
|
285
|
+
|
276
286
|
### URL Structure
|
277
287
|
- The `q` parameter namespace keeps query params organized
|
278
288
|
- All filter inputs are nested under their filter name
|
data/docs/modules/table.md
CHANGED
@@ -31,6 +31,9 @@ class PostDefinition < Plutonium::Resource::Definition
|
|
31
31
|
# Define sorting
|
32
32
|
sort :title
|
33
33
|
sort :published_at
|
34
|
+
|
35
|
+
# Define default sort (newest first)
|
36
|
+
default_sort :created_at, :desc
|
34
37
|
|
35
38
|
# Enable search
|
36
39
|
search do |scope, query|
|
@@ -200,6 +203,11 @@ class PostDefinition < Plutonium::Resource::Definition
|
|
200
203
|
sort :author_name, using: "users.name" do |scope, direction:|
|
201
204
|
scope.joins(:author).order("users.name #{direction}")
|
202
205
|
end
|
206
|
+
|
207
|
+
# Default sort when no user sorting is applied
|
208
|
+
default_sort :created_at, :desc # Simple form
|
209
|
+
# or with a block for complex sorting:
|
210
|
+
# default_sort { |scope| scope.order(featured: :desc, created_at: :desc) }
|
203
211
|
end
|
204
212
|
```
|
205
213
|
|
@@ -6,9 +6,27 @@ module Plutonium
|
|
6
6
|
included do
|
7
7
|
defineable_props :sort
|
8
8
|
|
9
|
+
class_attribute :_default_sort, instance_writer: false, instance_predicate: false
|
10
|
+
|
9
11
|
def self.sorts(*names)
|
10
12
|
names.each { |name| sort name }
|
11
13
|
end
|
14
|
+
|
15
|
+
def self.default_sort(field = nil, direction = :asc, &block)
|
16
|
+
self._default_sort = if block_given?
|
17
|
+
block
|
18
|
+
elsif field
|
19
|
+
[field, direction]
|
20
|
+
end
|
21
|
+
_default_sort
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set a sensible default: newest items first
|
25
|
+
default_sort :id, :desc
|
26
|
+
end
|
27
|
+
|
28
|
+
def default_sort
|
29
|
+
self.class._default_sort
|
12
30
|
end
|
13
31
|
end
|
14
32
|
end
|
@@ -25,6 +25,10 @@ module Plutonium
|
|
25
25
|
query_object.define_sorter key, value[:block], **value[:options]
|
26
26
|
end
|
27
27
|
|
28
|
+
if current_definition.respond_to?(:default_sort) && current_definition.default_sort
|
29
|
+
query_object.default_sort_config = current_definition.default_sort
|
30
|
+
end
|
31
|
+
|
28
32
|
current_definition.defined_filters.each do |key, value|
|
29
33
|
with = value[:options][:with]
|
30
34
|
if with.is_a?(Class) && with < Plutonium::Query::Filter
|
@@ -2,6 +2,7 @@ module Plutonium
|
|
2
2
|
module Resource
|
3
3
|
class QueryObject
|
4
4
|
attr_reader :search_filter, :search_query
|
5
|
+
attr_accessor :default_sort_config
|
5
6
|
|
6
7
|
# Initializes a QueryObject with the given resource_class and parameters.
|
7
8
|
#
|
@@ -221,12 +222,20 @@ module Plutonium
|
|
221
222
|
# @return [Object] The modified scope.
|
222
223
|
def apply_sorts(scope, params)
|
223
224
|
selected_sort_directions = extract_sort_directions(params)
|
224
|
-
selected_sort_fields.each do |name|
|
225
|
-
next unless (sorter = sort_definitions[name])
|
226
225
|
|
227
|
-
|
228
|
-
|
226
|
+
if selected_sort_fields.any?
|
227
|
+
# Apply user-selected sorts
|
228
|
+
selected_sort_fields.each do |name|
|
229
|
+
next unless (sorter = sort_definitions[name])
|
230
|
+
|
231
|
+
direction = selected_sort_directions[name] || "ASC"
|
232
|
+
scope = sorter.apply(scope, direction:)
|
233
|
+
end
|
234
|
+
elsif default_sort_config
|
235
|
+
# Apply default sort when no sorts are selected
|
236
|
+
scope = apply_default_sort(scope)
|
229
237
|
end
|
238
|
+
|
230
239
|
scope
|
231
240
|
end
|
232
241
|
|
@@ -250,6 +259,24 @@ module Plutonium
|
|
250
259
|
end
|
251
260
|
end.compact
|
252
261
|
end
|
262
|
+
|
263
|
+
# Applies the default sort to the given scope
|
264
|
+
#
|
265
|
+
# @param scope [Object] The initial scope
|
266
|
+
# @return [Object] The sorted scope
|
267
|
+
def apply_default_sort(scope)
|
268
|
+
case default_sort_config
|
269
|
+
when Proc
|
270
|
+
# Block form: default_sort { |scope| scope.order(...) }
|
271
|
+
default_sort_config.call(scope)
|
272
|
+
when Array
|
273
|
+
# Field/direction form: default_sort :created_at, :desc
|
274
|
+
field, direction = default_sort_config
|
275
|
+
scope.order(field => direction)
|
276
|
+
else
|
277
|
+
scope
|
278
|
+
end
|
279
|
+
end
|
253
280
|
end
|
254
281
|
end
|
255
282
|
end
|
data/lib/plutonium/version.rb
CHANGED