thecore_backend_commons 3.13.1 → 3.14.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: 465d99db5c28a0099476fa5f4b862390f661dae07a0a54ac3c2c6b21597cbcfa
4
- data.tar.gz: b4282d66700aafe387cbb3382a531950c80787034b924cb56c12f97e2a222ccc
3
+ metadata.gz: 3f2a8859c842b9323f8900296504b3ee72e08b77f069678833d39b1e4299fc46
4
+ data.tar.gz: c68ab134016879d83e489e8725db03a4f351a6297a14c6ac3920db847e293c91
5
5
  SHA512:
6
- metadata.gz: e4b3948959b20e0e131e489596e6399952936ae84440b522df5fc41fc0f4f9d6fac4ef9ab01e8f7bb9c74fb8ddc8021d8587df41f29d314f375be59d9cd14358
7
- data.tar.gz: f97bfd47f4de73499ce7f48bd9b6837d56d9b19b9ba13a3a1c68c1a66a9482d94b4496aa2f318fc42762e857b08fb54881a6e6e1a4b26cbb76959df6eb6b9730
6
+ metadata.gz: 2222a3fc4f998d7ed0b32d15e6f1cbbc1c6faea4cf8986b00db86ff34849351f7b23337dc505ec44214e4f350aa0985907903ceef8f1b52a363ffafd70b1337b
7
+ data.tar.gz: 1456d9624b9bafe48b1ff9ed9008b07519f196aa6fdeb531b25b1e605100e51f7ac0fab44701c698305e53ad906154dd44f849b7e0a3e5f8e4005996cd426766
data/README.md CHANGED
@@ -165,7 +165,9 @@ loads every model in production.
165
165
 
166
166
  Consumers today: `model_driven_api`'s `ModelDrivenApiDefaultJsonAttrs` (default `json_attrs`)
167
167
  and `thecore_ui_rails_admin`'s `ThecoreUiRailsAdminDefaultNavigationConcern` (default
168
- `navigation_label`/`navigation_icon`) — see their own READMEs for what each one does. A model
168
+ `navigation_label`/`navigation_icon`) — see their own READMEs for what each one does — and,
169
+ since 3.14.0, this gem's own `BaseApplicationRecordConcern` (every model) and
170
+ `ThecoreBackendCommonsUserConcern` (`User`), registered after the other gems' defaults. A model
169
171
  that still needs custom behavior keeps (or adds) an explicit `Api::ModelName`/
170
172
  `RailsAdmin::ModelName` concern exactly as before; it simply runs *after* the default and
171
173
  overrides it. Full mechanics (idempotency, abstract/STI exclusion, `to_prepare` vs
@@ -3,11 +3,7 @@ Rails.application.configure do
3
3
  Integer.send(:include, FixnumConcern)
4
4
  String.send(:include, StringConcern)
5
5
  ApplicationCable::Connection.send(:include, CableConnectionConcern)
6
- # ApplicationRecord.send(:include, ApplicationRecordConcern)
7
- ApplicationRecord.subclasses.each do |d|
8
- d.send(:include, BaseApplicationRecordConcern) unless d.name.start_with?("ActiveStorage::", "ActionText::")
9
- end
10
-
11
- User.send(:include, ThecoreBackendCommonsUserConcern)
6
+ # BaseApplicationRecordConcern and ThecoreBackendCommonsUserConcern are applied to each model as
7
+ # it is defined: see ThecoreBackendCommons::Engine ("thecore_backend_commons.default_modules").
12
8
  end
13
9
  end
@@ -83,10 +83,16 @@ module ThecoreBackendCommons
83
83
  # Called automatically by `InheritedHook` -- only call directly from
84
84
  # application/test code that needs to (re-)apply defaults to a class
85
85
  # defined before the registry/hook was set up.
86
- def apply_to(klass)
86
+ #
87
+ # +only+ restricts it to one registered module: for a class defined
88
+ # before the hook existed, a gem applies its own module after the class
89
+ # body without re-running other gems' defaults there (one that assigns
90
+ # json_attrs without merging would wipe the model's own).
91
+ def apply_to(klass, only: nil)
87
92
  return klass if klass.abstract_class?
88
93
 
89
94
  entries.each do |entry|
95
+ next if only && !entry.mod.equal?(only)
90
96
  next unless entry.applies_to.call(klass)
91
97
  next if klass.include?(entry.mod)
92
98
 
@@ -1,4 +1,34 @@
1
1
  module ThecoreBackendCommons
2
2
  class Engine < ::Rails::Engine
3
+ # BaseApplicationRecordConcern and ThecoreBackendCommonsUserConcern reach every model as it is
4
+ # defined, through DefaultModuleRegistry, so the app behaves the same with or without eager
5
+ # loading (GitHub #7; until 3.13 a post-boot scan only reached the models already loaded).
6
+ #
7
+ # Registered from a to_prepare block added after every railtie's config/initializers
8
+ # (after: :load_config_initializers matches all of them): to_prepare blocks run in the order they
9
+ # were added, so these two are registered, and therefore included, after the other gems' default
10
+ # modules. That matters because some of them set json_attrs without merging (model_driven_api's
11
+ # ModelDrivenApiDefaultJsonAttrs: `{ except: [] }`), while BaseApplicationRecordConcern merges its
12
+ # methods into whatever is there. A model's own concern, in its class body, still comes last.
13
+ #
14
+ # A model already defined when the hook arrives (one required explicitly during boot, as the
15
+ # dummy app does with User) gets these two modules at once, after its body, as before 3.14,
16
+ # through DefaultModuleRegistry.apply_to(only:), parents before children (an STI child must
17
+ # inherit the module, not include it a second time).
18
+ OWN_DEFAULT_MODULES = {
19
+ "BaseApplicationRecordConcern" => ->(klass) { klass.name.present? && !klass.name.start_with?("ActiveStorage::", "ActionText::") },
20
+ "ThecoreBackendCommonsUserConcern" => ->(klass) { klass.name == "User" },
21
+ }.freeze
22
+
23
+ initializer "thecore_backend_commons.default_modules", after: :load_config_initializers do |app|
24
+ app.config.to_prepare do
25
+ already_defined = ApplicationRecord.descendants.sort_by { |klass| klass.ancestors.size }
26
+ OWN_DEFAULT_MODULES.each do |module_name, applies_to|
27
+ mod = module_name.constantize
28
+ ThecoreBackendCommons::DefaultModuleRegistry.register(mod, applies_to: applies_to)
29
+ already_defined.each { |klass| ThecoreBackendCommons::DefaultModuleRegistry.apply_to(klass, only: mod) }
30
+ end
31
+ end
32
+ end
3
33
  end
4
34
  end
@@ -1,3 +1,3 @@
1
1
  module ThecoreBackendCommons
2
- VERSION = "3.13.1".freeze
2
+ VERSION = "3.14.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_backend_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.13.1
4
+ version: 3.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni