rails-surrender 7.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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +123 -0
  3. data/LICENSE +674 -0
  4. data/README.md +326 -0
  5. data/lib/rails/surrender/config/locales/en.yml +3 -0
  6. data/lib/rails/surrender/controller_additions.rb +27 -72
  7. data/lib/rails/surrender/fields/asks.rb +50 -0
  8. data/lib/rails/surrender/fields/attribute.rb +34 -0
  9. data/lib/rails/surrender/fields/expand.rb +65 -0
  10. data/lib/rails/surrender/fields/field_tree.rb +71 -0
  11. data/lib/rails/surrender/fields/id_list.rb +38 -0
  12. data/lib/rails/surrender/fields/node.rb +86 -0
  13. data/lib/rails/surrender/menu/endpoint_menu.rb +21 -0
  14. data/lib/rails/surrender/menu/field.rb +20 -0
  15. data/lib/rails/surrender/menu/field_menu.rb +58 -0
  16. data/lib/rails/surrender/menu/filter.rb +16 -0
  17. data/lib/rails/surrender/menu/menu.rb +27 -0
  18. data/lib/rails/surrender/menu/menu_param.rb +28 -0
  19. data/lib/rails/surrender/menu/selection_menu.rb +33 -0
  20. data/lib/rails/surrender/model_additions.rb +47 -32
  21. data/lib/rails/surrender/params/list_param.rb +23 -0
  22. data/lib/rails/surrender/railtie.rb +31 -9
  23. data/lib/rails/surrender/render/resource.rb +11 -30
  24. data/lib/rails/surrender/selection/filtering.rb +144 -0
  25. data/lib/rails/surrender/selection/page_size.rb +37 -0
  26. data/lib/rails/surrender/selection/selected.rb +50 -0
  27. data/lib/rails/surrender/selection/selection.rb +70 -0
  28. data/lib/rails/surrender/selection/sorting.rb +61 -0
  29. data/lib/rails/surrender/version.rb +1 -1
  30. data/lib/rails/surrender.rb +21 -12
  31. metadata +69 -41
  32. data/lib/rails/surrender/config/initializers/active_record_associations_patch.rb +0 -17
  33. data/lib/rails/surrender/config/initializers/active_record_preloader_patch.rb +0 -20
  34. data/lib/rails/surrender/helpers/filter_builder.rb +0 -50
  35. data/lib/rails/surrender/helpers/pagination_builder.rb +0 -25
  36. data/lib/rails/surrender/helpers/query_param_parser.rb +0 -98
  37. data/lib/rails/surrender/helpers/sort_builder.rb +0 -56
  38. data/lib/rails/surrender/model_filter_scopes.rb +0 -56
  39. data/lib/rails/surrender/render/configuration/inclusion_mapper_logic.rb +0 -69
  40. data/lib/rails/surrender/render/configuration/instance_logic.rb +0 -88
  41. data/lib/rails/surrender/render/configuration.rb +0 -93
  42. data/lib/rails/surrender/render/count.rb +0 -21
  43. data/lib/rails/surrender/render/ids.rb +0 -21
  44. data/lib/rails/surrender/render/resource/collection.rb +0 -26
  45. data/lib/rails/surrender/render/resource/inclusion_mapper.rb +0 -48
  46. data/lib/rails/surrender/render/resource/instance.rb +0 -75
  47. data/test/surrender_test.rb +0 -9
  48. data/test/test_helper.rb +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aba7043e3b9225507863919c15e03867b380091c2d91b199dfcba457b54bfcd7
4
- data.tar.gz: 71462ff57204518e6d1fd259bf203f1c0db7509abff4bb1ade164e6625fa844e
3
+ metadata.gz: e0b54e235f4cc5f4a4462e7bd01068455eec4964d11310ee0ebaa9a49d7cc8a2
4
+ data.tar.gz: 54788efff4d38b82aa45622a88938d2f19330de5fdee70d63000d1a809fcd733
5
5
  SHA512:
6
- metadata.gz: 2c819641efaffa38fff74066807f71471aecc15a7ae22f455311f6b92c3396dcbabf4fe369d3e7c1e8c22af47e4c820d1a253b67cd27b7df9b8510696647c0ca
7
- data.tar.gz: 4dc06b4dee19755b6759867c8ae9c9ac7efe6fd608871ea4012636a3a3daa01a06cd91194f9ca3597e9ec1099b16ca5cce1f8f3469990ec4189b38be96a0d7f2
6
+ metadata.gz: '0387d6f2e74add2b4c5bf7e522edcb43761ae78c63ca7d4e3e07568d87d385f297bb96f3f497176df30267c126260e81f2b9645cff38a71204a13fb5e0bbb69c'
7
+ data.tar.gz: 1442baab193a1a7a6eacfb142eb3b72be73a34582f608c1ef7ebbe3e3f7e9d0a759ba393da381185dbe2f3e99463890efee4cce8e2ae03df4f468ba909ddd653
data/CHANGELOG.md ADDED
@@ -0,0 +1,123 @@
1
+ # Changelog
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
+
23
+ ## 8.0.0
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.
25
+
26
+ ### Breaking
27
+ - `surrenders` takes the fields to render by default as arguments, and the fields a request may add as `available:`.
28
+ Surrender tells associations from attributes itself, so `attributes:`, `expands:`, `available_attributes:` and
29
+ `available_expands:` are removed and raise `ArgumentError`. For example,
30
+ `surrenders attributes: %i[id title], expands: [:author], available_attributes: [:pages]` becomes
31
+ `surrenders :id, :title, :author, available: [:pages]`.
32
+ - Requires Rails 8.0 or 8.1 and Ruby 3.3 or newer. Rails 7 and Ruby 3.0–3.2 are no longer supported.
33
+ - `kaminari` and `oj` are no longer pinned to exact versions (`~> 1.2` and `~> 3.13`).
34
+ - The `<name>_to` and `<name>_from` filters on date and time columns are removed. They duplicated `_before` and
35
+ `_after` but for a single instant, which mostly caused confusion. Use `_before` and `_after` with a time instead,
36
+ such as `created_before:'2022-01-01T00:00:01Z'`.
37
+ - The `_before` and `_after` filters accept only ISO 8601 dates and times, such as `'2022-01-01'` or
38
+ `'2022-01-01T13:30:00Z'`, and raise `Rails::Surrender::Error` for anything else. Previously a value that wasn't a
39
+ time was compared as `NULL` and silently matched nothing.
40
+ - The `reload:` option of `surrender` is removed, and passing it raises `ArgumentError`. It only existed to fetch a
41
+ record again so its associations could be preloaded; Surrender now preloads the record it is given. An association
42
+ the controller already loaded or changed in memory therefore renders as it is in memory, not as stored.
43
+ - Fields render in a fixed order: `id` first, then other attributes, then `*_ids` id lists, then expands. Within each
44
+ group, default fields come first, then those the controller includes, then those the request includes.
45
+ - An STI record inside a collection renders its own class's fields, as it does when rendered alone. A `Novel` in
46
+ `Book.all` used to render only `Book`'s fields.
47
+ - A polymorphic association listed as a field raises `ArgumentError` when the response is built. It already failed,
48
+ with `Polymorphic associations do not support computing the class`; support is tracked in
49
+ [#6](https://github.com/smmcbride/rails-surrender/issues/6).
50
+ - A name the controller includes that is not an association or `*_ids` id list renders as an attribute, so a name the
51
+ model can't answer raises `NoMethodError` instead of being silently skipped.
52
+ - The `ids` query param is removed, and a request that sends it renders the resources as usual. To list only ids,
53
+ exclude the other default fields, such as `exclude=title,genre,author`.
54
+ - A `sort_by_<name>` scope the model defines wins over a column of the same name, as `filter_by_` scopes the model
55
+ defines already win over generated ones. Previously the column sort was used and the scope never ran.
56
+ - `Rails::Surrender::FieldTree.new` takes the request's `include:` and `exclude:` as query strings, such as
57
+ `include: 'books:[pages]'`, rather than parsed lists.
58
+ - `Rails::Surrender::QueryParamParser` is removed. Its page defaults are `Rails::Surrender::Selection::PAGE_DEFAULT`
59
+ and `PER_PAGE_DEFAULT`. `Rails::Surrender::ModelFilterScopes` is now `Rails::Surrender::Selection::Filtering`.
60
+ - `surrender_attributes`, `surrender_expands`, `surrender_callable_attributes` and `surrender_callable_expands` are
61
+ removed. `surrender_menu` lists a model's fields, with their kind and whether they render by default, along with
62
+ its filters and sorts.
63
+ - A page holds at most 100 resources, whatever `per` asks for. `config.surrender.max_per_page` changes the limit, or
64
+ removes it when set to `nil`, and `config.surrender.default_per_page` changes the default of 50. `X-Pagination`
65
+ reports the page size used, not the one requested.
66
+ - `page` and `per` that aren't positive whole numbers are rejected with `Rails::Surrender::Error`. `per=0` and
67
+ `per=abc` used to raise `Kaminari::ZeroPerPageOperation`, and `per=-5` returned a page of Kaminari's default size
68
+ while `X-Pagination` reported -5.
69
+ - `Rails::Surrender::Selection::PER_PAGE_DEFAULT` is removed. `surrender_menu.page_size` gives the configured default
70
+ and limit.
71
+
72
+ ### Fixed
73
+ - Apps that eager load (the production default) no longer fail at boot with `undefined method 'surrenders'`. Surrender
74
+ now hooks into ActiveRecord and ActionController with load hooks instead of `after_initialize`.
75
+ - `filter_by_*` scopes are generated on first use instead of when a model is defined, so loading a model no longer
76
+ queries the database, models whose table is created after boot still get them, and abstract classes not named
77
+ `ApplicationRecord` are skipped. Errors while defining them are no longer silently swallowed.
78
+ `filter_by_*` scopes the app defines, on the model or a superclass, always take precedence over generated ones.
79
+ - Rendering a collection whose model has STI subclasses no longer raises `NameError`.
80
+ - Rendering an STI record no longer loops forever, and STI subclasses are recognised as their base class when
81
+ stopping circular expansion.
82
+ - `count` counts every matching resource instead of only the first page.
83
+ - `surrender nil` renders `{}`, and arrays are rendered as given, as intended.
84
+ - A `current_ability` defined as a private controller method is now used for authorization.
85
+ - A nested exclude such as `exclude=author:[name]` removes only the nested attributes, instead of the whole `author`.
86
+ - Associations excluded by the request or the controller, or read only by an excluded `*_ids` attribute, are no longer
87
+ preloaded, saving a query for each one. A rendered `*_ids` attribute preloads only its association, not that
88
+ association's own expands.
89
+ - A blank `sort` parameter is ignored instead of rejected, so API explorers that send empty fields still work.
90
+ - Quoted values in `filter`, `include` and `exclude` are kept as given. A space was inserted after every colon and
91
+ comma, even inside quotes, so `'13:30'` arrived as `'13: 30'`.
92
+ - Preloading always matches what renders. Collections of STI models no longer preload subclass associations they
93
+ never render ([#2](https://github.com/smmcbride/rails-surrender/issues/2)), and associations only a subclass renders are
94
+ preloaded instead of loaded once per record.
95
+ - A request include that is not available, at any depth, is rejected before any query runs.
96
+ - A `belongs_to` with no record renders `null` whatever the ability allows.
97
+ - A single resource no longer gets an `X-Sort` header, as it isn't sorted.
98
+ - Paginated requests run one query fewer: `page_total` in `X-Pagination` follows from the total instead of a second
99
+ count.
100
+ - A filter term with no value, such as `filter=genre`, is rejected as improperly formatted. It was reported as an
101
+ unknown filter named after its first letter: `g is not a valid filter parameter.`
102
+ - A sort through an association that holds many records, such as `sort=books.title` on authors, is rejected as an
103
+ invalid sort. It repeated each record once per associated record, and the page's total counted the repeats.
104
+
105
+ ### Changed
106
+ - Models gain `surrender_menu`: everything a request may ask of the model, read from the same rules that check
107
+ requests. Its fields are nested by expand and leave out expands back into a model above, its filters include those
108
+ the model defines, and so do its sorts.
109
+ - A selection applies a request's filter, sort and page to a relation, and models resolve filter and sort names with
110
+ `surrender_filter` and `surrender_sort`. `FilterBuilder`, `SortBuilder`, `PaginationBuilder` and `Render::Ids` and
111
+ `Render::Count` are removed.
112
+ - Surrender works out the fields a request renders once, as a field tree, and both preloading and rendering read it.
113
+ `Render::Configuration` and the classes under `Render::Resource` are replaced by `Rails::Surrender::FieldTree`.
114
+ - Locale files are loaded through the Railtie.
115
+ - Removed the unused ActiveRecord patch files and test skeleton.
116
+ - Added a request-level test suite run against Rails 8.0 and 8.1.
117
+
118
+ ## 7.0.0
119
+ - 0.1.0 renamed for the Rails version it supports, with no code changes. Declares the `actionpack`, `activerecord` and
120
+ `railties` `~> 7.0.0` dependencies 0.1.0 needed but never declared. Maintained on the `7-0-stable` branch.
121
+
122
+ ## 0.1.0
123
+ - Initial release.