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