model_driven_api 3.8.0 → 3.9.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: 1f0f44e2c1015c731953af0fd3f2e05128e5e8d2493775a7788bd8ecab306f49
4
- data.tar.gz: 2fd0bb9b281db660563a58890d7c287e5589043e32e5d5b1b30a828b6103f563
3
+ metadata.gz: d0955834f57dc25b51a326caf3a64d2da728c635eacb2b01cf1e9f656e559ea5
4
+ data.tar.gz: 677af97d19ed54c6cba486f0d99bce4736e4e06e3f916c4d0c896a13eef8c865
5
5
  SHA512:
6
- metadata.gz: 5498cd511a63b1a8ac368015b4e10786518a80d030aa9b9c93656a1870bda7c677076559e3b1061fec1a557dcab9faecc77a064b4b250f52598b1249dade435c
7
- data.tar.gz: d3d2be5de0a1a2cc35a7db37b9cee0d8eaf8dbcb594e7b557bccda62354df909ba4258b70e9dd59f03ded31b73631a288e1acbdb1d0bd07c106847867cab7f8d
6
+ metadata.gz: 07f3a5338e772e90baf5687dc38b6b038b55b97da94bae909d544408767b67927da667507f3e84e52a80f4cc7ff26c275757d9700f1bbfb0ce8914784da206fd
7
+ data.tar.gz: ec926ebae3447ad6ba9f8231dd60b2992f75b2838b2b49be14378390fe63f99b0af3085f9759ea0e2d580477463fa10d092b651dc29452597f0801c420b4eb18
data/README.md CHANGED
@@ -449,6 +449,22 @@ When composing `json_attrs` across multiple concerns, use `ModelDrivenApi.smart_
449
449
  self.json_attrs = ModelDrivenApi.smart_merge((json_attrs || {}), { only: [:id, :name] })
450
450
  ```
451
451
 
452
+ ### Default `json_attrs` — no `Api::ModelName` concern required
453
+
454
+ A model doesn't need an explicit `Api::ModelName` concern just to get a working API shape.
455
+ `ModelDrivenApiDefaultJsonAttrs` is registered into
456
+ [`ThecoreBackendCommons::DefaultModuleRegistry`](../thecore_backend_commons/README.md#defaultmoduleregistry--shared-applicationrecordinherited-hook)
457
+ and automatically `include`d into every `ApplicationRecord` subclass as it's defined, setting
458
+ `self.json_attrs = { except: [] }` (every column, no `methods`, no sideloaded `include`) — the
459
+ simplest safe default. A model with its own `Api::ModelName` concern (or `ModelDrivenApiUser`/
460
+ `ModelDrivenApiRole`) is unaffected: that concern's `include` always runs after the default and
461
+ freely overrides or merges on top of it via `ModelDrivenApi.smart_merge`. Only a model with no
462
+ concern at all keeps the bare default.
463
+
464
+ **Temporary dependency note**: this currently requires `thecore_backend_commons` from a Gemfile
465
+ `git:` pin to its `release/3` branch, since the RubyGems release hasn't caught up with the
466
+ registry yet — see this repo's `CLAUDE.md` for the exact commit/removal condition.
467
+
452
468
  ---
453
469
 
454
470
  ## Raw SQL endpoint
@@ -0,0 +1,18 @@
1
+ require "concerns/model_driven_api_default_json_attrs"
2
+
3
+ # Registers the generic default `json_attrs` module (ADR 0001) into
4
+ # ThecoreBackendCommons's shared DefaultModuleRegistry so it is `include`d
5
+ # into every `ApplicationRecord` subclass as it is defined.
6
+ #
7
+ # Installed from `config.to_prepare` -- not `config.after_initialize` -- for
8
+ # the same reason `ThecoreBackendCommons::DefaultModuleRegistry.install!` is:
9
+ # Rails runs `to_prepare` callbacks *before* `eager_load!`, so the module must
10
+ # already be registered before eager loading defines every model class in
11
+ # production. `to_prepare` also re-runs on every class reload in development;
12
+ # `DefaultModuleRegistry.register` is idempotent for the same module object,
13
+ # so re-running this block on reload is safe.
14
+ Rails.application.configure do
15
+ config.to_prepare do
16
+ ThecoreBackendCommons::DefaultModuleRegistry.register(ModelDrivenApiDefaultJsonAttrs)
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ # Registered into ThecoreBackendCommons::DefaultModuleRegistry (see
2
+ # config/initializers/default_json_attrs_registration.rb) so it is
3
+ # automatically `include`d into *every* `ApplicationRecord` subclass at
4
+ # class-definition time -- not just the handful of named classes
5
+ # (ModelDrivenApiUser, ModelDrivenApiRole, ...) that get a bespoke concern.
6
+ #
7
+ # See vendor/external/thecore/docs/adr/0001-application-record-defaults-over-generated-concerns.md
8
+ # in the host app for the design rationale: default model behavior should
9
+ # not require a generated per-model `Api::ModelName` concern file for the
10
+ # no-customization case.
11
+ #
12
+ # A model that *does* need custom serialization still gets (or keeps) an
13
+ # explicit `Api::ModelName` concern, `include`d directly in the model file
14
+ # exactly as today -- because `ApplicationRecord.inherited` (which applies
15
+ # this default) fires *before* the subclass's own body executes, that later
16
+ # explicit `include` always runs after this default has already set
17
+ # `json_attrs`, so it freely overrides (or, via `ModelDrivenApi.smart_merge`,
18
+ # merges on top of) the default's value. See `ModelDrivenApiUser`/
19
+ # `ModelDrivenApiRole` for that pattern.
20
+ module ModelDrivenApiDefaultJsonAttrs
21
+ extend ActiveSupport::Concern
22
+
23
+ included do
24
+ ## DSL (AKA what to show in the returned JSON)
25
+ # Use self.json_attrs to drive json rendering for
26
+ # API model responses (index, show and update ones).
27
+ # For reference:
28
+ # https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
29
+ # The object passed accepts only these keys:
30
+ # - only: list [] of model field names in symbol notation to be shown in JSON
31
+ # serialization.
32
+ # - except: exclude these fields from the JSON serialization, is a list []
33
+ # of model field names in symbol notation.
34
+ # - methods: include the result of some methods defined in the model (virtual
35
+ # fields).
36
+ # - include: include associated models, it's a list [] of hashes {} which also
37
+ # accepts the [:only, :except, :methods, :include] keys.
38
+ #
39
+ # Default shape: no `only`/`except` restriction beyond the empty array
40
+ # below (so every column is serialized), no `methods`, no `include` (no
41
+ # associations sideloaded) -- the simplest safe default for a model that
42
+ # has no customization needs. `cattr_accessor` (not a plain method
43
+ # definition) is required so `json_attrs` lands as an *own* method on
44
+ # each concrete model class -- `Api::V2::InfoController#schema`/`#dsl`
45
+ # check `instance_methods(false).include?(:json_attrs)` while walking
46
+ # `ApplicationRecord.subclasses`, and a merely-inherited method would
47
+ # silently fail that check and drop the model from introspection output.
48
+ cattr_accessor :json_attrs
49
+ self.json_attrs = { except: [] }
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.8.0".freeze
2
+ VERSION = "3.9.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.0
4
+ version: 3.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
@@ -195,6 +195,7 @@ files:
195
195
  - config/initializers/after_initialize_for_model_driven_api.rb
196
196
  - config/initializers/auto_include_json.rb
197
197
  - config/initializers/cors_api_thecore.rb
198
+ - config/initializers/default_json_attrs_registration.rb
198
199
  - config/initializers/knock.rb
199
200
  - config/initializers/time_with_zone.rb
200
201
  - config/initializers/wrap_parameters.rb
@@ -210,6 +211,7 @@ files:
210
211
  - lib/api/v3/serializer_factory.rb
211
212
  - lib/concerns/api_exception_management.rb
212
213
  - lib/concerns/model_driven_api_application_record.rb
214
+ - lib/concerns/model_driven_api_default_json_attrs.rb
213
215
  - lib/concerns/model_driven_api_push_message.rb
214
216
  - lib/concerns/model_driven_api_push_subscriber.rb
215
217
  - lib/concerns/model_driven_api_role.rb