dynamic-active-model-rails 0.11.0 → 0.12.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: ff5e6fd6c5b29afc9e58143d52d5d43b39123423f1356fd4daa60cde68a5a5f1
4
- data.tar.gz: c3909cdbe0d699072ece815f55372f482c8a10c3c436c44866e1f55c24e52f8d
3
+ metadata.gz: aade59eb21651ddf7c6c7f50de964e3d3e1e6809e82e9d832cb05cfe6917df65
4
+ data.tar.gz: a0a3e87f460a462d3096193fdb4b2c405799903bf347c74944ce83b258c76afb
5
5
  SHA512:
6
- metadata.gz: a27b214728591ccf5fe23094287bb2486d86aace729b4efb330a9d87948d9e5a42eca19966990c1a2afb5076a82282d5a48d475c6a8d90da7bf357db4fc75206
7
- data.tar.gz: 58590282a387c2fd6c1f10434e30875c43831797567726dfed8526a3c0da7f82d38bcb9a9a4e84f365637999f4bea68f7509c70b554f51536c339833664a0c8f
6
+ metadata.gz: 562618b697479ecfcd9aff7cd04c8e9e40488064925dba63d76828ea5255c337355d0272d8e666d2f69b2b8e22f0fc5a67ffee86153075501ee34aab18b52de1
7
+ data.tar.gz: eb5fe6bbd793f081d53f2dfd5826226ffc10806a3e6afb922e9eeffc49f1f391b2009246049f72506b27d38b3982ebbb79ab4d193be0567958c42738e1b5a91f
data/README.md CHANGED
@@ -86,6 +86,19 @@ module CarsDB
86
86
  end
87
87
  ```
88
88
 
89
+ ## Load Hooks
90
+
91
+ Configuration that touches models at boot, like `has_paper_trail`, must be reapplied whenever the models are rebuilt. After every build, the gem runs an ActiveSupport load hook named after the database's folder. The block runs with the `DynamicActiveModel::Database` as `self`:
92
+
93
+ ```ruby
94
+ # config/initializers/paper_trail.rb
95
+ ActiveSupport.on_load(:grant_db) do
96
+ %w[users organizations].each { |table| get_model!(table).has_paper_trail }
97
+ end
98
+ ```
99
+
100
+ Avoid `Rails.application.config.after_initialize { GrantDB.database... }`. It runs once, so models rebuilt after a reload or migration would lose that setup.
101
+
89
102
  ## Lifecycle
90
103
 
91
104
  - **Lazy loading.** Nothing touches the database at boot. The first reference to a constant in a namespace builds that database's models. That's why `db:create`, `db:migrate` and `assets:precompile` don't need models to exist.
@@ -44,6 +44,11 @@ module DynamicActiveModel
44
44
  module_name.underscore
45
45
  end
46
46
 
47
+ # @return [Symbol] ActiveSupport load hook run after each build, e.g. :cars_db
48
+ def load_hook
49
+ folder.to_sym
50
+ end
51
+
47
52
  # @param root [Pathname, String] Application root
48
53
  # @return [String] Directory holding this database's .ext.rb files
49
54
  def extensions_path(root)
@@ -6,6 +6,8 @@ module DynamicActiveModel
6
6
  module Rails
7
7
  # Builds one declared database's models on demand and tears them down again.
8
8
  # Loading is synchronized so concurrent first references build only once.
9
+ # After every build it runs the database's ActiveSupport load hook, so
10
+ # ActiveSupport.on_load(:cars_db) { ... } reapplies after reloads and migrations.
9
11
  class DatabaseLoader
10
12
  # @return [DatabaseDefinition]
11
13
  attr_reader :definition
@@ -41,13 +43,15 @@ module DynamicActiveModel
41
43
 
42
44
  private
43
45
 
44
- # Creates models, relationships and extensions; undoes a partial build on error
46
+ # Creates models, relationships and extensions, then runs load hooks;
47
+ # undoes a partial build on error
45
48
  # @return [void]
46
49
  def build
47
50
  @database = new_database
48
51
  @database.create_models!
49
52
  Explorer.build_relationships!(@database, definition.relationships)
50
53
  load_extensions
54
+ ActiveSupport.run_load_hooks(definition.load_hook, @database)
51
55
  rescue StandardError
52
56
  reset!
53
57
  raise
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.11.0
32
+ version: 0.12.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.11.0
39
+ version: 0.12.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
42
  requirement: !ruby/object:Gem::Requirement