rails-surrender 8.0.0 → 8.1.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: '087ada6f5a21db65a56526bea6ec1858139018109ffa309e423c9365b41cb482'
4
- data.tar.gz: 9466d63c0a5ebf748a6ce6e11dcbd393e1f8f4cd54bf12128e35c13a6f87fa6b
3
+ metadata.gz: e0b54e235f4cc5f4a4462e7bd01068455eec4964d11310ee0ebaa9a49d7cc8a2
4
+ data.tar.gz: 54788efff4d38b82aa45622a88938d2f19330de5fdee70d63000d1a809fcd733
5
5
  SHA512:
6
- metadata.gz: 1cfef68fc036228de7d205f6341d89d2046486365c19ee97e99e2ac8f25c9c6d67bd4f9a7b99d1cd1ea7761405a6b61e5e39640ef13975ff8c34d1c83feaf0e5
7
- data.tar.gz: 45fe53ac8cbcfe836f11da1d30a25a53e2ba4eb8cfa2ed7f12956604950e261e6fe4f3d5394a1691888f3a79e509afe7ea0dafa97839f45a17790b21b904cd88
6
+ metadata.gz: '0387d6f2e74add2b4c5bf7e522edcb43761ae78c63ca7d4e3e07568d87d385f297bb96f3f497176df30267c126260e81f2b9645cff38a71204a13fb5e0bbb69c'
7
+ data.tar.gz: 1442baab193a1a7a6eacfb142eb3b72be73a34582f608c1ef7ebbe3e3f7e9d0a759ba393da381185dbe2f3e99463890efee4cce8e2ae03df4f468ba909ddd653
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 8.1.0
4
+
5
+ ### Added
6
+ - Supports Rails 7.0, 7.1 and 7.2 as well as 8.0 and 8.1, with no change in behaviour. Ruby 3.3 or newer is still
7
+ required. The version no longer names the Rails version it supports: 8.x is the current version, for Rails 7.0
8
+ through 8.1.
9
+ - Endpoint menus. With `config.surrender.menus = true`, a request with the `menu` query parameter gets the endpoint's
10
+ fields, filters, sorts and page size as JSON instead of its records, with the controller's `include:`, `exclude:`
11
+ and `skip_pagination` applied. `config.surrender.menu_param` renames the parameter. Menus are off by default.
12
+
13
+ ### Changed
14
+ - A singular expand whose record the ability can't read renders as `null`, as a missing record does. It used to be
15
+ left out of the response, so a client couldn't rely on a field's key being present.
16
+ - `Menu#fields` gives each field a `type` instead of a `kind`. An attribute's type is the Rails type of its column, or
17
+ `nil` for a method the model defines, an id list's is `:array`, and an expand's is `:object`, or `:array` for a
18
+ collection association, matching the JSON it renders as.
19
+ - `Menu#selection` holds a model's filters, sorts and page size, which apply only to the records at the top of a
20
+ response. `Menu#fields` lists the model's field menu, and an expand's entry carries the field menu of its target,
21
+ without filters or sorts no request can use. `Menu#filters`, `#sorts` and `#page_size` still work.
22
+
3
23
  ## 8.0.0
4
24
  The version now names the Rails version it supports: 8.0.0 supports Rails 8.0 and 8.1. It follows 7.0.0.
5
25
 
data/README.md CHANGED
@@ -4,12 +4,22 @@ API responses from your resources.
4
4
 
5
5
  ## Requirements
6
6
  * Ruby 3.3 or newer
7
- * Rails 8.0 or 8.1
7
+ * Rails 7.0 through 8.1
8
8
 
9
- The gem's version names the Rails version it supports. Use 8.x for Rails 8.0 and 8.1, and 7.0.0 (on the
10
- `7-0-stable` branch) for Rails 7.0.
9
+ CI runs the full suite on these Ruby and Rails versions:
11
10
 
12
- Rails 8.0 is not compatible with version 3 of the `json` gem, so Rails 8.0 apps should keep `json` below 3:
11
+ | Ruby | Rails 7.0 | Rails 7.1 | Rails 7.2 | Rails 8.0 | Rails 8.1 |
12
+ |------|:---------:|:---------:|:---------:|:---------:|:---------:|
13
+ | 3.3 | ✅ | ✅ | ✅ | ✅ | ✅ |
14
+ | 3.4 | | | | ✅ | ✅ |
15
+ | 4.0 | | | | ✅ | ✅ |
16
+
17
+ 8.x is the current version and supports Rails 7.0 through 8.1. Its major version changes only with breaking
18
+ changes. Every other change, including new features, goes in the patch digit, unless a release changes which Rails
19
+ minor versions it supports. 7.0.0, on the `7-0-stable` branch, is the earlier version for Rails 7.0, and gets no new
20
+ features.
21
+
22
+ Rails 8.0 and earlier are not compatible with version 3 of the `json` gem, so apps on them should keep `json` below 3:
13
23
 
14
24
  ```ruby
15
25
  gem 'json', '< 3'
@@ -134,6 +144,66 @@ To list only the ids of the resources, exclude every other field they render by
134
144
  `exclude=title,genre,author`.
135
145
 
136
146
 
147
+ ### Menus
148
+ An endpoint can describe what it accepts. With menus turned on (see [Configuration](#configuration)), the `menu` query
149
+ parameter renders the endpoint's _menu_ instead of its resources: the fields it renders by default and those a request
150
+ may include, nested by expand, and, for a collection, the filters, sorts and page size a request may use.
151
+
152
+ ```
153
+ GET /books?menu
154
+ ```
155
+
156
+ ```json
157
+ {
158
+ "fields": [
159
+ { "name": "id", "type": "integer", "default": true },
160
+ { "name": "title", "type": "string", "default": true },
161
+ { "name": "pages", "type": "integer", "default": false },
162
+ { "name": "chapter_ids", "type": "array", "default": false },
163
+ { "name": "author", "type": "object", "default": false,
164
+ "fields": [
165
+ { "name": "id", "type": "integer", "default": true },
166
+ { "name": "name", "type": "string", "default": true }
167
+ ] },
168
+ { "name": "chapters", "type": "array", "default": false,
169
+ "fields": [ ... ] }
170
+ ],
171
+ "selection": {
172
+ "filters": {
173
+ "title": { "type": "string" },
174
+ "published_before": { "type": "datetime" },
175
+ "long": { "type": null }
176
+ },
177
+ "sorts": ["id", "title", "author.name", "title_length"],
178
+ "page": { "default": 50, "max": 100 }
179
+ }
180
+ }
181
+ ```
182
+
183
+ - `fields` are listed in the order they render. Only an expand has `fields` of its own, and expands that would loop
184
+ back into a model above are left out, as they never render.
185
+ - Each field's `type` is what it renders as. An attribute's is the Rails type of its column, such as `string` or
186
+ `datetime`, or `null` for a method the model defines. An `_ids` list is an `array`, and an expand is an `object`, or
187
+ an `array` of objects for a collection association.
188
+ - The controller's own `include:` and `exclude:` are applied, so a field the controller includes is listed as a
189
+ default. A field the controller excludes is listed as available if a request may still include it.
190
+ - Each filter's `type` is the Rails type of the column it compares, such as `datetime` for the `_before` and `_after`
191
+ filters, which take ISO 8601 times. A filter the model defines itself has a `null` type.
192
+ - `selection` is `null` for an endpoint that renders a single record, and `page` is `null` when the controller skips
193
+ pagination. An endpoint that surrenders a hash, an array or `nil` has `null` for both parts, as no ask applies to it.
194
+ - A menu lists names and types only. The syntax of each ask is described in the sections above.
195
+
196
+ A menu ignores every other ask in the request, and loads no records. The controller still runs its action, its
197
+ callbacks and anything before `surrender`, so a member route needs an id that exists, and the menu is only as visible
198
+ as the endpoint itself. A block passed to `surrender` doesn't decorate a menu, so every endpoint's menu has the same
199
+ shape.
200
+
201
+ Abilities aren't applied to a menu. They decide which records render inside a field, not which fields a model offers,
202
+ so a menu may list an expand whose records the current user can't read.
203
+
204
+ An STI subclass renders its own fields. The menu of a collection lists its base model's fields, so records of a
205
+ subclass may render more default fields than it lists. A request may only include fields the base model offers.
206
+
137
207
  ## Example Project
138
208
  A full working example project is provided in the [rails-surrender-demo](https://github.com/smmcbride/rails-surrender-demo)
139
209
  repository. It provides several models and endpoints demonstrating a working configuration of `rails-surrender`. The
@@ -195,22 +265,37 @@ config.surrender.default_per_page = 50 # the page size when a request doesn't
195
265
  config.surrender.max_per_page = 100 # the most a page holds, whatever `per` asks for; nil removes the limit
196
266
  ```
197
267
 
268
+ Menus are off unless the app turns them on, as they show the app's column names and the names of its own filters and
269
+ sorts. Set them per environment to keep them out of production:
270
+
271
+ ```ruby
272
+ config.surrender.menus = true # answer the menu query parameter; false by default
273
+ config.surrender.menu_param = 'menu' # the name it's asked with, such as 'docs'
274
+ ```
275
+
276
+ While menus are off, `menu` is an ordinary query parameter, so an app's own parameter of that name works as usual. The
277
+ app won't boot if `menu_param` is blank or is the name of a parameter surrender already reads, such as `count`.
278
+
198
279
  The app won't boot if the default is larger than the limit, or either isn't a positive whole number. Kaminari's own
199
280
  page size settings don't apply to surrender's pages.
200
281
 
201
282
  ### Describing what a request may ask
202
283
 
203
- `surrender_menu` lists everything a request may ask of a model, read from the same rules that check requests, for
204
- documentation or an API explorer like the demo's:
284
+ `surrender_menu` lists everything a request may ask of a model, read from the same rules that check requests. Unlike
285
+ an endpoint's [menu](#menus), it doesn't know a controller's `include:` and `exclude:`:
205
286
 
206
287
  ```ruby
207
288
  menu = Book.surrender_menu
208
- menu.fields # each with its name, kind (:attribute, :id_list or :expand), default?, and an expand's own menu
209
- menu.filters # each with its name and the column it compares, or nil for a filter the model defines
210
- menu.sorts # ['id', 'title', ..., 'author.name', 'title_length']
211
- menu.page_size.default, menu.page_size.max # 50 and 100 unless configured
289
+ menu.fields # each with its name, type (such as :string, :array or :object), default?, and an expand's field menu
290
+ menu.selection.filters # each with its name and the column it compares, or nil for a filter the model defines
291
+ menu.selection.sorts # ['id', 'title', ..., 'author.name', 'title_length']
292
+ menu.selection.page_size.default, menu.selection.page_size.max # 50 and 100 unless configured
212
293
  ```
213
294
 
295
+ The fields make up the model's field menu, which applies wherever the model renders, including inside an expand. The
296
+ filters, sorts and page size make up its selection menu, which applies only to the records at the top of a response.
297
+ `menu.filters`, `menu.sorts` and `menu.page_size` answer from the selection menu too.
298
+
214
299
  ### Additional Notes
215
300
 
216
301
  Surrender prevents circular renderings. If a `Book` model, for example, expands its `Author`, and `Author` expands its
@@ -220,7 +305,8 @@ into a model already rendered above it, and it treats an STI subclass as its bas
220
305
  Surrender prevents un-authorized access to associated resources by invoking the `current_ability` rule-set from the
221
306
  CanCanCan gem, if it is used in your Rails project. Any controller method named `current_ability`, public or private,
222
307
  that returns an object responding to `can?(:read, record)` is used the same way. If neither is present it will allow
223
- full access to associated resources.
308
+ full access to associated resources. A nested record the ability can't read renders as `null`, as a missing one does,
309
+ and is left out of a nested collection.
224
310
 
225
311
  ## Development
226
312
 
@@ -9,7 +9,18 @@ module Rails
9
9
  super
10
10
  end
11
11
 
12
- def surrender(resource, status: 200, include: [], exclude: [])
12
+ def surrender(resource, status: 200, include: [], exclude: [], &)
13
+ # Before the selection, which would parse and check the other asks a menu ignores.
14
+ if Surrender.menu_param.asked_in?(query_params)
15
+ render_endpoint_menu(resource, include: include, exclude: exclude)
16
+ else
17
+ render_selected(resource, status: status, include: include, exclude: exclude, &)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def render_selected(resource, status:, include:, exclude:)
13
24
  selection = Selection.new(query_params, paginate: @will_paginate)
14
25
  selected = selection.apply(resource)
15
26
  response.headers.merge!(selected.headers)
@@ -28,7 +39,11 @@ module Rails
28
39
  render(json: surrender_response.json_data, status: status)
29
40
  end
30
41
 
31
- private
42
+ # Never decorated by the block, so every endpoint's menu has the same shape.
43
+ def render_endpoint_menu(resource, include:, exclude:)
44
+ menu = EndpointMenu.new(resource, include: include, exclude: exclude, paginate: @will_paginate)
45
+ render(json: Response.new(data: menu.as_json).json_data)
46
+ end
32
47
 
33
48
  def ability
34
49
  # Apps commonly override CanCanCan's current_ability as a private method.
@@ -3,17 +3,14 @@
3
3
  module Rails
4
4
  module Surrender
5
5
  module Fields
6
- # A field rendered as the value its record returns for it.
7
- Attribute = Data.define(:name) do
6
+ # A field rendered as the value its record returns for it. Its type is the Rails type of the attribute, such as
7
+ # :string or :datetime, or nil for a method the model defines.
8
+ Attribute = Data.define(:name, :type) do
8
9
  # Every record leads with its id, wherever the model or the asks list it.
9
10
  def rank
10
11
  name == :id ? 0 : 1
11
12
  end
12
13
 
13
- def kind
14
- :attribute
15
- end
16
-
17
14
  def preload; end
18
15
 
19
16
  def circular?(_above)
@@ -9,8 +9,9 @@ module Rails
9
9
  3
10
10
  end
11
11
 
12
- def kind
13
- :expand
12
+ # The JSON shape it renders as: an array of records for a collection, or one record.
13
+ def type
14
+ reflection.collection? ? :array : :object
14
15
  end
15
16
 
16
17
  def preload
@@ -26,22 +27,21 @@ module Rails
26
27
  Node.new(target, asks.below(name), above + [target.base_class])
27
28
  end
28
29
 
29
- def menu(above:)
30
- Menu.new(target, above: above + [target.base_class])
30
+ def menu(asks:, above:)
31
+ FieldMenu.new(target, asks: asks.below(name), above: above + [target.base_class])
31
32
  end
32
33
 
33
- # The record at the top is never checked against the ability, only the records nested in it.
34
+ # The record at the top is never checked against the ability, only the records nested in it. A nested record
35
+ # the ability can't read renders as nil, like a missing one, so every field the menu lists keeps its key.
34
36
  def write(record, into:, ability:, branch:)
35
37
  value = record.public_send(name)
36
38
 
37
- if reflection.collection?
38
- into[name] = value.select { |nested| ability.can?(:read, nested) }
39
+ into[name] = if reflection.collection?
40
+ value.select { |nested| ability.can?(:read, nested) }
39
41
  .map { |nested| branch.for(nested.class).render(nested, ability) }
40
- elsif value.nil?
41
- into[name] = nil
42
- elsif ability.can?(:read, value)
43
- into[name] = branch.for(value.class).render(value, ability)
44
- end
42
+ elsif value && ability.can?(:read, value)
43
+ branch.for(value.class).render(value, ability)
44
+ end
45
45
  end
46
46
 
47
47
  def describe(branch)
@@ -9,8 +9,8 @@ module Rails
9
9
  2
10
10
  end
11
11
 
12
- def kind
13
- :id_list
12
+ def type
13
+ :array
14
14
  end
15
15
 
16
16
  def preload
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Surrender
5
+ # What one endpoint accepts: its model's field menu shaped by the controller's include and exclude, and, for a
6
+ # relation, its selection menu. Rendered instead of the records when a request asks for it.
7
+ class EndpointMenu
8
+ def initialize(resource, include: [], exclude: [], paginate: true)
9
+ model = resource.try(:klass) || (resource.class if resource.is_a?(ActiveRecord::Base))
10
+ @fields = model && FieldMenu.new(model, asks: Fields::Asks.new(controller_include: include,
11
+ controller_exclude: exclude))
12
+ @selection = SelectionMenu.new(model, paginate: paginate) if resource.is_a?(ActiveRecord::Relation)
13
+ end
14
+
15
+ # Nil, hashes and arrays render as given, so no ask applies to them and both parts are null.
16
+ def as_json(*)
17
+ { fields: @fields, selection: @selection }.as_json
18
+ end
19
+ end
20
+ end
21
+ end
@@ -2,13 +2,18 @@
2
2
 
3
3
  module Rails
4
4
  module Surrender
5
- class Menu
6
- # A field a request may ask a model for: its kind (:attribute, :id_list or :expand), whether it renders by
7
- # default, and, for an expand, the menu of the model it leads to.
8
- Field = Data.define(:name, :kind, :default, :menu) do
5
+ class FieldMenu
6
+ # A field a request may ask a model for: the type of what it renders, whether it renders by default, and, for an
7
+ # expand, the field menu of the model it leads to. An attribute's type is its Rails type, or nil for a method the
8
+ # model defines; an id list is an :array, and an expand an :object or, for a collection, an :array.
9
+ Field = Data.define(:name, :type, :default, :menu) do
9
10
  def default?
10
11
  default
11
12
  end
13
+
14
+ def as_json(*)
15
+ { name: name, type: type, default: default }.merge({ fields: menu }.compact).as_json
16
+ end
12
17
  end
13
18
  end
14
19
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'field'
4
+
5
+ module Rails
6
+ module Surrender
7
+ # The fields a request may ask one model for: its default and available fields, nested by expand. It applies
8
+ # wherever the model renders, at the top or inside an expand. A controller's asks shape it as they shape rendering.
9
+ class FieldMenu
10
+ # `above` holds the base class of every model from the top of the menu down to this one.
11
+ def initialize(model, asks: Fields::Asks.new, above: [model.base_class])
12
+ @model = model
13
+ @asks = asks
14
+ @above = above
15
+ end
16
+
17
+ # In the order fields render, with defaults before available fields within each group. Expands back into a model
18
+ # above are left out, as they never render.
19
+ def fields
20
+ @fields ||= offered_fields.map do |field|
21
+ Field.new(name: field.name, type: field.type, default: default?(field.name),
22
+ menu: field.menu(asks: @asks, above: @above))
23
+ end
24
+ end
25
+
26
+ def as_json(*)
27
+ fields.as_json
28
+ end
29
+
30
+ private
31
+
32
+ # A field the controller includes renders by default even when the model doesn't offer it, but a request may
33
+ # only include what the model offers, so a field the controller excludes stays on the menu only if it's offered.
34
+ def offered_fields
35
+ (@model.surrender_fields + @asks.controller_includes + @model.surrender_available_fields)
36
+ .uniq
37
+ .select { |name| on_menu?(name) }
38
+ .map { |name| @model.surrender_field(name) }
39
+ .reject { |field| field.circular?(@above) }
40
+ .each_with_index
41
+ .sort_by { |field, position| [field.rank, default?(field.name) ? 0 : 1, position] }
42
+ .map(&:first)
43
+ end
44
+
45
+ def on_menu?(name)
46
+ default?(name) || @model.surrenders?(name)
47
+ end
48
+
49
+ def default?(name)
50
+ defaults.include?(name)
51
+ end
52
+
53
+ def defaults
54
+ @defaults ||= (@model.surrender_fields + @asks.controller_includes).reject { |name| @asks.excludes?(name) }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -2,10 +2,15 @@
2
2
 
3
3
  module Rails
4
4
  module Surrender
5
- class Menu
5
+ class SelectionMenu
6
6
  # A filter a request may name, and the column it compares, or nil for a filter the model defines under a name of
7
7
  # its own.
8
- Filter = Data.define(:name, :column)
8
+ Filter = Data.define(:name, :column) do
9
+ # The column's Rails type tells a client what value to send, such as an ISO 8601 time for a datetime.
10
+ def as_json(*)
11
+ { type: column&.type }.as_json
12
+ end
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -1,54 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'field'
4
- require_relative 'filter'
3
+ require_relative 'endpoint_menu'
4
+ require_relative 'field_menu'
5
+ require_relative 'menu_param'
6
+ require_relative 'selection_menu'
5
7
 
6
8
  module Rails
7
9
  module Surrender
8
- # Everything a request may ask of one model: its default and available fields, nested by expand, its filters and
9
- # sorts, and the size of its pages. It is read from the same rules that check requests, so it lists exactly what a
10
- # request may ask.
10
+ # Everything a request may ask of one model: its field menu and its selection menu. It is read from the same rules
11
+ # that check requests, so it lists exactly what a request may ask.
11
12
  class Menu
12
- # `above` holds the base class of every model from the top of the menu down to this one.
13
- def initialize(model, above: [model.base_class])
14
- @model = model
15
- @above = above
16
- end
17
-
18
- # In the order fields render, with defaults before available fields within each kind. Expands back into a model
19
- # above are left out, as they never render.
20
- def fields
21
- @fields ||= offered_fields.map do |field|
22
- Field.new(name: field.name, kind: field.kind, default: default?(field), menu: field.menu(above: @above))
23
- end
24
- end
13
+ attr_reader :selection
25
14
 
26
- def filters
27
- @model.surrender_filter_columns.map { |name, column| Filter.new(name: name, column: column) }
28
- end
15
+ delegate :filters, :sorts, :page_size, to: :selection
29
16
 
30
- def sorts
31
- @model.surrender_sort_names
17
+ def initialize(model)
18
+ @field_menu = FieldMenu.new(model)
19
+ @selection = SelectionMenu.new(model)
32
20
  end
33
21
 
34
- def page_size
35
- Surrender.page_size
36
- end
37
-
38
- private
39
-
40
- def offered_fields
41
- (@model.surrender_fields + @model.surrender_available_fields)
42
- .uniq
43
- .map { |name| @model.surrender_field(name) }
44
- .reject { |field| field.circular?(@above) }
45
- .each_with_index
46
- .sort_by { |field, position| [field.rank, default?(field) ? 0 : 1, position] }
47
- .map(&:first)
48
- end
49
-
50
- def default?(field)
51
- @model.surrender_fields.include?(field.name)
22
+ def fields
23
+ @field_menu.fields
52
24
  end
53
25
  end
54
26
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Surrender
5
+ # The query param a request sends to ask for an endpoint menu, and whether the app answers it. While menus are
6
+ # off the name is not reserved, so an app's own param of that name reaches it as usual.
7
+ MenuParam = Data.define(:enabled, :name) do
8
+ def initialize(enabled: false, name: 'menu')
9
+ name = name.to_s
10
+ raise ArgumentError, 'menu_param needs a name' if name.empty?
11
+ raise ArgumentError, "menu_param can't be #{name.inspect}, which surrender already reads" if reserved?(name)
12
+
13
+ super
14
+ end
15
+
16
+ def asked_in?(query_params)
17
+ enabled && query_params.key?(name.to_sym)
18
+ end
19
+
20
+ private
21
+
22
+ # A menu named after a param surrender already reads would hide it.
23
+ def reserved?(name)
24
+ %w[include exclude filter sort page per count].include?(name)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'filter'
4
+
5
+ module Rails
6
+ module Surrender
7
+ # The filters, sorts and page size a request may use to pick a model's records. They apply only at the top of a
8
+ # response, to a relation; nothing filters or sorts the records inside an expand.
9
+ class SelectionMenu
10
+ def initialize(model, paginate: true)
11
+ @model = model
12
+ @paginate = paginate
13
+ end
14
+
15
+ def filters
16
+ @model.surrender_filter_columns.map { |name, column| Filter.new(name: name, column: column) }
17
+ end
18
+
19
+ def sorts
20
+ @model.surrender_sort_names
21
+ end
22
+
23
+ # Nil when the controller skips pagination, as its records then come in one piece.
24
+ def page_size
25
+ Surrender.page_size if @paginate
26
+ end
27
+
28
+ def as_json(*)
29
+ { filters: filters.to_h { |filter| [filter.name, filter] }, sorts: sorts, page: page_size }.as_json
30
+ end
31
+ end
32
+ end
33
+ end
@@ -29,7 +29,7 @@ module Rails
29
29
  @surrender_available_fields || []
30
30
  end
31
31
 
32
- # Everything a request may ask of the model: its fields, filters and sorts.
32
+ # Everything a request may ask of the model: its field menu and its selection menu.
33
33
  def surrender_menu
34
34
  Menu.new(self)
35
35
  end
@@ -47,7 +47,7 @@ module Rails
47
47
  elsif (association = id_list_association(name))
48
48
  Fields::IdList.new(name: name, association: association)
49
49
  else
50
- Fields::Attribute.new(name: name)
50
+ Fields::Attribute.new(name: name, type: type_for_attribute(name).type)
51
51
  end
52
52
  end
53
53
 
@@ -8,6 +8,8 @@ module Rails
8
8
  config.surrender = ActiveSupport::OrderedOptions.new
9
9
  config.surrender.default_per_page = 50
10
10
  config.surrender.max_per_page = 100
11
+ config.surrender.menus = false
12
+ config.surrender.menu_param = 'menu'
11
13
 
12
14
  # Built once at boot, so a page size setting that makes no sense stops the app from starting.
13
15
  initializer 'rails_surrender.page_size' do |app|
@@ -16,6 +18,12 @@ module Rails
16
18
  )
17
19
  end
18
20
 
21
+ initializer 'rails_surrender.menu_param' do |app|
22
+ Rails::Surrender.menu_param = Rails::Surrender::MenuParam.new(
23
+ enabled: app.config.surrender.menus, name: app.config.surrender.menu_param
24
+ )
25
+ end
26
+
19
27
  initializer 'rails_surrender.locales' do |app|
20
28
  app.config.i18n.load_path += Dir[File.expand_path('config/locales/*.yml', __dir__)]
21
29
  end
@@ -20,6 +20,10 @@ module Rails
20
20
  max ? [requested, max].min : requested
21
21
  end
22
22
 
23
+ def as_json(*)
24
+ to_h.as_json
25
+ end
26
+
23
27
  private
24
28
 
25
29
  def check_positive_whole_number(setting, value)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module Surrender
5
- VERSION = '8.0.0'
5
+ VERSION = '8.1.0'
6
6
  end
7
7
  end
@@ -7,9 +7,17 @@ module Rails
7
7
  # The page size the app configures with config.surrender.default_per_page and max_per_page, set at boot.
8
8
  attr_writer :page_size
9
9
 
10
+ # Whether the app answers requests for endpoint menus, and the param they ask with, from config.surrender.menus
11
+ # and menu_param, set at boot.
12
+ attr_writer :menu_param
13
+
10
14
  def page_size
11
15
  @page_size ||= Selection::PageSize.new
12
16
  end
17
+
18
+ def menu_param
19
+ @menu_param ||= MenuParam.new
20
+ end
13
21
  end
14
22
  end
15
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-surrender
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.0
4
+ version: 8.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn McBride
@@ -15,7 +15,7 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '8.0'
18
+ version: '7.0'
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
21
  version: '9'
@@ -25,7 +25,7 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: '8.0'
28
+ version: '7.0'
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '9'
@@ -35,7 +35,7 @@ dependencies:
35
35
  requirements:
36
36
  - - ">="
37
37
  - !ruby/object:Gem::Version
38
- version: '8.0'
38
+ version: '7.0'
39
39
  - - "<"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '9'
@@ -45,7 +45,7 @@ dependencies:
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: '8.0'
48
+ version: '7.0'
49
49
  - - "<"
50
50
  - !ruby/object:Gem::Version
51
51
  version: '9'
@@ -83,7 +83,7 @@ dependencies:
83
83
  requirements:
84
84
  - - ">="
85
85
  - !ruby/object:Gem::Version
86
- version: '8.0'
86
+ version: '7.0'
87
87
  - - "<"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '9'
@@ -93,7 +93,7 @@ dependencies:
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '8.0'
96
+ version: '7.0'
97
97
  - - "<"
98
98
  - !ruby/object:Gem::Version
99
99
  version: '9'
@@ -119,9 +119,13 @@ files:
119
119
  - lib/rails/surrender/fields/field_tree.rb
120
120
  - lib/rails/surrender/fields/id_list.rb
121
121
  - lib/rails/surrender/fields/node.rb
122
+ - lib/rails/surrender/menu/endpoint_menu.rb
122
123
  - lib/rails/surrender/menu/field.rb
124
+ - lib/rails/surrender/menu/field_menu.rb
123
125
  - lib/rails/surrender/menu/filter.rb
124
126
  - lib/rails/surrender/menu/menu.rb
127
+ - lib/rails/surrender/menu/menu_param.rb
128
+ - lib/rails/surrender/menu/selection_menu.rb
125
129
  - lib/rails/surrender/model_additions.rb
126
130
  - lib/rails/surrender/params/list_param.rb
127
131
  - lib/rails/surrender/railtie.rb