sort_param 1.1.0 → 1.2.0

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: fb0d8250b0a318bf85f2031f497b703e044387bb9b0ce53588a7bd8aefe42cc1
4
- data.tar.gz: e48ff69e8d9f9d1eeb188222abceb7368b1fc928bf36e52b6f9822321b854782
3
+ metadata.gz: 410b605717d78b03414a2efa8fe9bcfc7db220f52b68ec20526068ee214dd695
4
+ data.tar.gz: 7b472ee03998faf9e05fb5aebb4a39e36d2a06853169598a9089a48cd04bf93a
5
5
  SHA512:
6
- metadata.gz: e99bca609530200771778839f406bf270d2fa4a11041f9f44f095f2d6e81882fbf7f814e1a14699aa7b194ed6fa907eb69cb804824c85f5bcf4c3f85ca34ed65
7
- data.tar.gz: 884b4a6af691fb6782c9b9f4db453d5e7f904e9b4ddf309c3e07999a51e136eae363acb5dbd2c60368ed315f17e707eacd8d2aa2c21f1bc8ad85dc91e4fa5765
6
+ metadata.gz: 2ad6f2f434973f1b1c25ce86d9949f02443a042ecc1a7279eb208c3eecfb04410adbcb0ea436b0341d729747c17665a8d7b25a9fb6d181f4d3623933b90e7a40
7
+ data.tar.gz: f838dd76a5d4e8231c6308f33575dae253c5cd4ab6f6e887488b3ab74da6252ee39ed4b3beb77e024c3f503e358a659788ee62ae78eeb6041f4049881a814ce2
data/CHANGELOG.md CHANGED
@@ -9,3 +9,8 @@
9
9
 
10
10
  ## [1.1.0]
11
11
  - Introduce definition#load to silently ignore non-whitelisted sort fields
12
+
13
+ ## [1.2.0]
14
+ - Introduce #fields to whitelist multiple fields with the same field defaults at once
15
+ - Allow field renaming using a Proc instead of hardcoding a string value
16
+
data/README.md CHANGED
@@ -137,12 +137,13 @@ Use `#load` method instead:
137
137
  => "first_name asc"
138
138
  ```
139
139
 
140
- ### Render a different field name in the output
141
- Set the `:rename` field option to output a different field name:
140
+ ### Output a different field name
141
+ Set the `:rename` field option to a string value or a Proc to output a different field name.
142
142
 
143
143
  ```ruby
144
144
  sort_param = SortParam.define do
145
145
  field :first_name, rename: 'users.name'
146
+ field :last_name, rename: ->(col) { "users.#{col}" }
146
147
  end
147
148
 
148
149
  sort_param.load!("+first_name", mode: :pg)
@@ -153,12 +154,28 @@ Set the `:rename` field option to output a different field name:
153
154
 
154
155
  sort_param.load!("+first_name")
155
156
  => {"users.name"=>{:direction=>:asc}}
157
+
158
+ sort_param.load!("+last_name")
159
+ => {"users.last_name"=>{:direction=>:asc}}
156
160
  ```
157
161
 
158
- <br/>
162
+ ### Whitelisting multiple fields with the same options at once
163
+ Use `#fields` instead of `#field`:
164
+
165
+ ```ruby
166
+ sort_param = SortParam.define do
167
+ fields :first_name, :last_name, nulls: :first, rename: ->(col) { "users.#{col}" }
168
+ field :email
169
+ end
170
+ ```
171
+
172
+ NOTE: Unlike `#field`, `#fields` can only accept a Proc for the `:rename` option.
173
+
159
174
 
160
175
  ### Rails example
161
176
 
177
+ ### controller
178
+
162
179
  ```ruby
163
180
  def index
164
181
  render json: User.all.order(sort_fields)
@@ -197,7 +214,7 @@ module HasSortParam
197
214
  end
198
215
  ```
199
216
 
200
- ### controller
217
+ ### controller v2
201
218
 
202
219
  ```ruby
203
220
  def index
@@ -21,19 +21,40 @@ module SortParam
21
21
  self
22
22
  end
23
23
 
24
- # Add a whitelisted column
24
+ # Whitelist a column
25
25
  #
26
26
  # @param name [String, Symbol] column name
27
27
  # @param defaults [Hash] column default options:
28
- # * nulls (Symbol) nulls sort order. `:last` or `:first`
29
- # * rename (String) field name in formatted output
28
+ # * nulls [Symbol] nulls sort order. `:last` or `:first`
29
+ # * rename [String, Proc] rename field in the formatted output.
30
+ # This can be a Proc code block that receives the :name as argument and
31
+ # returns a transformed field name.
30
32
  #
31
33
  # @return [self] Definition instance
32
- def field(name, defaults = {})
34
+ def field(name, **defaults)
33
35
  name = name.to_s
34
36
  return if name.strip.empty?
35
37
 
36
- fields_hash[name] = defaults
38
+ fields_hash[name] = preprocess_field_defaults(name, defaults)
39
+
40
+ self
41
+ end
42
+
43
+ # Whitelist multiple columns with the same column defaults.
44
+ #
45
+ # @param name [Array<String, Symbol>] list of column names
46
+ # @param defaults [Hash] column default options
47
+ #
48
+ # @see #field
49
+ #
50
+ # @return [self] Definition instance
51
+ #
52
+ def fields(*names, **defaults)
53
+ if defaults[:rename] && !defaults[:rename].is_a?(Proc)
54
+ raise ArgumentError.new(":rename should be a Proc")
55
+ end
56
+
57
+ names.each { |name| field(name, **defaults) }
37
58
 
38
59
  self
39
60
  end
@@ -100,6 +121,14 @@ module SortParam
100
121
  .format(*fields)
101
122
  end
102
123
 
124
+ def preprocess_field_defaults(field, defaults)
125
+ rename = defaults[:rename]
126
+ return defaults unless rename.is_a?(Proc)
127
+
128
+ defaults[:rename] = defaults[:rename].call(field)
129
+ defaults
130
+ end
131
+
103
132
  def non_whitelisted_fields(loaded_fields)
104
133
  loaded_fields.names - fields_hash.keys
105
134
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SortParam
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sort_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uy Jayson B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-20 00:00:00.000000000 Z
11
+ date: 2023-06-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Sort records using a sort query parameter à la JSON-API style
13
+ description: Sort records using a sort query parameter à la JSON:API format
14
14
  email:
15
15
  - uy.json.dev@gmail.com
16
16
  executables: []
@@ -57,5 +57,5 @@ requirements: []
57
57
  rubygems_version: 3.4.6
58
58
  signing_key:
59
59
  specification_version: 4
60
- summary: Sort records using a sort query parameter à la JSON-API style
60
+ summary: Sort records using a sort query parameter à la JSON:API format
61
61
  test_files: []