dynamic-active-model-rails 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43872099a56630aa1567fb6248215698f2da52769fb0141ee6456c7d7e2fed9e
4
- data.tar.gz: af832465a2d51f2f719b6ca1ad5c47bd2a41645c7b194fc6b3809bcfdea41984
3
+ metadata.gz: 9a7da7ef7df96bc40935c0defc56a7d05bb08a961dd8ccaf3e68638b1eacedc6
4
+ data.tar.gz: a454903cc4352593bf17662013b4bd2d0d3896ed4cc4ac6c4eb71e7cafc726dd
5
5
  SHA512:
6
- metadata.gz: 82234b92ca1348e4b2e2465832484e15a6986a6079d175714a473b8b74393bb415ac68450d96c9664fec28f549ead1f7a49914aa6570bb61927aef3d63e304bd
7
- data.tar.gz: c4b8cb05fcc0d1a5bcfed45788351064cb2d693e4e2013245ee4bd18d177cbd558e6ee9b4b50d80e719d5d1a1c0e75ddc01b309a20509ef86e322bd1d35671b1
6
+ metadata.gz: e1c4312ec8b9f804fc64db61d6ab9feee12d7d2dd2a29f01af146dcd9e2f599fe02394ff28ee5a43eb006474eba2a78c5f6ce4d394b94c8e4824f26b6b9b32d2
7
+ data.tar.gz: c5682d0674d704c08ba8da89b2cec584a5cb1231f5fe962b05705fc777f94f9f2629949aade2a785ffc915cfe40707d24fb01fdeefbdffb4fd7ac0c850592944
data/README.md CHANGED
@@ -78,6 +78,7 @@ The `cars_db` → `CarsDB` mapping is registered with the Rails autoloader only,
78
78
  | `module_name:` | `"<Name>DB"` | Namespace override. |
79
79
  | `parent_class:` | `'ApplicationRecord'` | Superclass of the generated abstract base class, given as a name so it can be reloaded. |
80
80
  | `connects_to:` | `nil` | Roles for Rails multi-database support, such as `{ writing: :cars, reading: :cars_replica }`, or `connects_to`'s own arguments (`{ database: ..., shards: ... }`). Can't be combined with a `connection`. |
81
+ | `foreign_key_constraints:` | `false` | Also derive associations from the database's foreign key constraints. This catches columns like `posts.author_id → users` and self-references, and costs one query per table at build time. See the core README. |
81
82
  | `extensions_path:` | `app/models/<folder>` | Directory of extension files, absolute or relative to `Rails.root`. The default folder may be absent; a configured path must exist. |
82
83
  | `extensions_suffix:` | `'.ext.rb'` | Suffix of extension files. The autoloader ignores files with this suffix. |
83
84
 
@@ -29,6 +29,9 @@ module DynamicActiveModel
29
29
  # @return [Hash, nil] Keyword arguments for the base class's connects_to
30
30
  attr_reader :connects_to
31
31
 
32
+ # @return [Boolean] Whether to also relate columns through foreign key constraints
33
+ attr_reader :foreign_key_constraints
34
+
32
35
  # @return [Hash] Custom relationship names by table and foreign key
33
36
  attr_reader :relationships
34
37
 
@@ -44,13 +47,17 @@ module DynamicActiveModel
44
47
  # @param extensions_suffix [String] File suffix of extension files
45
48
  # @param connects_to [Hash, nil] Roles ({ writing: :cars, reading: :cars_replica }) or
46
49
  # connects_to's own arguments ({ database: ..., shards: ... })
50
+ # @param foreign_key_constraints [Boolean] Also relate columns through the database's
51
+ # foreign key constraints
47
52
  # @raise [ArgumentError] If both a connection and connects_to are given
48
53
  def initialize(name, connection = nil, module_name: nil, parent_class: 'ApplicationRecord',
49
- extensions_path: nil, extensions_suffix: '.ext.rb', connects_to: nil)
54
+ extensions_path: nil, extensions_suffix: '.ext.rb', connects_to: nil,
55
+ foreign_key_constraints: false)
50
56
  raise ArgumentError, 'pass either a connection or connects_to:, not both' if connection && connects_to
51
57
 
52
58
  @connection = connection
53
59
  @connects_to = connects_to && normalize_connects_to(connects_to)
60
+ @foreign_key_constraints = foreign_key_constraints
54
61
  @module_name = module_name || default_module_name(name)
55
62
  @parent_class_name = parent_class.to_s
56
63
  @extensions_path = extensions_path&.to_s
@@ -61,7 +61,8 @@ module DynamicActiveModel
61
61
  def build
62
62
  @database = new_database
63
63
  @database.create_models!
64
- Explorer.build_relationships!(@database, definition.relationships)
64
+ Explorer.build_relationships!(@database, definition.relationships,
65
+ foreign_key_constraints: definition.foreign_key_constraints)
65
66
  load_extensions
66
67
  ActiveSupport.run_load_hooks(definition.load_hook, @database)
67
68
  rescue StandardError
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: 1.0.0
4
+ version: 1.1.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: 1.0.0
32
+ version: 1.1.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: 1.0.0
39
+ version: 1.1.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
42
  requirement: !ruby/object:Gem::Requirement