dynamic-active-model-rails 1.0.0 → 1.2.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: 2df8e51695a6a96f2fc32f697985e96e6ae201bf52af9acffad5877d25128751
4
+ data.tar.gz: 3ac8715b733383d6160bf229cb234b0df74e6edb1c9220d2caeb95132e1292db
5
5
  SHA512:
6
- metadata.gz: 82234b92ca1348e4b2e2465832484e15a6986a6079d175714a473b8b74393bb415ac68450d96c9664fec28f549ead1f7a49914aa6570bb61927aef3d63e304bd
7
- data.tar.gz: c4b8cb05fcc0d1a5bcfed45788351064cb2d693e4e2013245ee4bd18d177cbd558e6ee9b4b50d80e719d5d1a1c0e75ddc01b309a20509ef86e322bd1d35671b1
6
+ metadata.gz: b605039cf58e47a07283d309dcc0dcbabe890314f94ee6c881b1825c26d6714ecbbc00814e119d14fcf7f3ca54b082a4ec7a1592005e39e556b57a155f5337e7
7
+ data.tar.gz: 660965617e683a0ae03a1b1f053590262953d5ea6cb664eeff623c6daeff5b16ed6ab41eb8b7eacdbc07c072657ee6e18fc532254a60c50258397ae721f121b5
data/README.md CHANGED
@@ -78,6 +78,8 @@ 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. |
82
+ | `has_many_through:` | `false` | Add `has_many :through` across join models: tables with a unique index on two foreign keys, such as `user_roles`. See the core README. |
81
83
  | `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
84
  | `extensions_suffix:` | `'.ext.rb'` | Suffix of extension files. The autoloader ignores files with this suffix. |
83
85
 
@@ -29,6 +29,12 @@ 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
+
35
+ # @return [Boolean] Whether to also add has_many :through across join models
36
+ attr_reader :has_many_through
37
+
32
38
  # @return [Hash] Custom relationship names by table and foreign key
33
39
  attr_reader :relationships
34
40
 
@@ -44,13 +50,19 @@ module DynamicActiveModel
44
50
  # @param extensions_suffix [String] File suffix of extension files
45
51
  # @param connects_to [Hash, nil] Roles ({ writing: :cars, reading: :cars_replica }) or
46
52
  # connects_to's own arguments ({ database: ..., shards: ... })
53
+ # @param foreign_key_constraints [Boolean] Also relate columns through the database's
54
+ # foreign key constraints
55
+ # @param has_many_through [Boolean] Also add has_many :through across join models
47
56
  # @raise [ArgumentError] If both a connection and connects_to are given
48
57
  def initialize(name, connection = nil, module_name: nil, parent_class: 'ApplicationRecord',
49
- extensions_path: nil, extensions_suffix: '.ext.rb', connects_to: nil)
58
+ extensions_path: nil, extensions_suffix: '.ext.rb', connects_to: nil,
59
+ foreign_key_constraints: false, has_many_through: false)
50
60
  raise ArgumentError, 'pass either a connection or connects_to:, not both' if connection && connects_to
51
61
 
52
62
  @connection = connection
53
63
  @connects_to = connects_to && normalize_connects_to(connects_to)
64
+ @foreign_key_constraints = foreign_key_constraints
65
+ @has_many_through = has_many_through
54
66
  @module_name = module_name || default_module_name(name)
55
67
  @parent_class_name = parent_class.to_s
56
68
  @extensions_path = extensions_path&.to_s
@@ -61,7 +61,9 @@ 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,
66
+ has_many_through: definition.has_many_through)
65
67
  load_extensions
66
68
  ActiveSupport.run_load_hooks(definition.load_hook, @database)
67
69
  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.2.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.2.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.2.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
42
  requirement: !ruby/object:Gem::Requirement