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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13438498e4ed22a7e815bd5f850a48007b17418ae5bf9226c68c360bf41a6a9e
4
- data.tar.gz: d23d68ced086dbb49c5777ef8ee4a206a1c29c1b6c12a5efcb068b5c0b8c4cc5
3
+ metadata.gz: 25a972b402c936a681cd59984869f8fa9fd08e6c5ad240578bc18c95c57cac45
4
+ data.tar.gz: ed73139eafd20795cb9a4be658c3f32a01c3546d51c37fd1f279eb9175697209
5
5
  SHA512:
6
- metadata.gz: 95c11cf8731208462dc152b1f31161e578cfdcc276502853d57112cf4694b370c191e8fc3412beef8255deae9211491b3dbe8ef86f30492467936bdfa9213833
7
- data.tar.gz: a0263fa147b67cc0435feba73311f0f1d7d54d5fd6d11379f5f3855113ff35e6ec3d67e24b941582925168f5115a1a1a18795690f931fdda5a626f6dd4b225fd
6
+ metadata.gz: c9fd52c2b932a4cc852935d03cfe3a3a3e00453d7a73aaeba698219fee78dcd354ea5a7f6443ef7c5273e401b6ad746fa50dec03ab469f178958f17231368f94
7
+ data.tar.gz: 81075c91ca822c5dd4b7f8866b6b75e4f1557aae08dc70af8096ed167141e02269749f5e57b920bb0036a9cb9c8ca58fb4b0f090f923aae252b431310a7842bc
@@ -190,6 +190,9 @@ class BlogDefinition < Plutonium::Resource::Definition
190
190
  # Configure sorting
191
191
  sort :title
192
192
  sort :published_at
193
+
194
+ # Configure default sorting (newest first)
195
+ default_sort :published_at, :desc
193
196
  end
194
197
  ```
195
198
 
@@ -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
@@ -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
- direction = selected_sort_directions[name] || "ASC"
228
- scope = sorter.apply(scope, direction:)
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
@@ -1,5 +1,5 @@
1
1
  module Plutonium
2
- VERSION = "0.26.6"
2
+ VERSION = "0.26.7"
3
3
  NEXT_MAJOR_VERSION = VERSION.split(".").tap { |v|
4
4
  v[1] = v[1].to_i + 1
5
5
  v[2] = 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plutonium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.6
4
+ version: 0.26.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich