dynamic-active-model 1.3.2 → 1.4.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: 1180fa600bbb8f28bf7f2be26b9150305b9fb4486c1229bbee4d1e7f2d461018
4
- data.tar.gz: 95a4e5c46ae8990dcf2250fee577f2c437a2f382229077713a33ae9575576812
3
+ metadata.gz: 014a76565d40730ce4afc012b743af0b83ce49cf375d46f75b30f678bd5fc253
4
+ data.tar.gz: c55980904ccf254e5c24d20a4e94b1e0d6c4f8a1596e74f469c7543fcac67c78
5
5
  SHA512:
6
- metadata.gz: 89f510068c30b03a9eb4c9d82c57dd06c052088a9314c6c135fbde78dba8586826cc2e5f99de17f1e5bd4480d2216975bc53bd13a3222fe784daee9f5c33832d
7
- data.tar.gz: 21b7af7eb2804253c4b214148d23ff86fd41c6a605ef7b86e219f0d141358f10882d1a13ab02811ea0061701a7d4620ffd6e043e36982019738aafa0276f66e5
6
+ metadata.gz: d9fb9bbec8b8480d6a69d6c1a987a8cf814e76c24bcc2a56e0d9960cd86d7e1f4e87baef1a5f53ac8f61faebaa5979d691e568d745ec94294d5f9e84f1be24b5
7
+ data.tar.gz: 2b0a0078aa60d97ddf0a713fdb4af1caf5d713eaae56faaee8f9802e5e02e96a95a0ed2eeb121a98cf8af8b1a07ff3ebbd10309f360e6421d5ebd92058a83ef1
@@ -74,6 +74,10 @@ OptionParser.new do |opts|
74
74
  options[:has_many_through] = true
75
75
  end
76
76
 
77
+ opts.on('--optional-belongs-to', 'Make every belongs_to optional, even on NOT NULL foreign keys') do
78
+ options[:optional_belongs_to] = true
79
+ end
80
+
77
81
  opts.on('-c', '--config FILE', "config file, default #{DEFAULT_CONFIG_FILE}") do |v|
78
82
  options[:config_file] = v
79
83
  end
@@ -101,6 +105,7 @@ if options[:config_file]
101
105
  options[:relationships] = config.delete('relationships') || {}
102
106
  options[:foreign_key_constraints] ||= config.delete('foreign_key_constraints') || false
103
107
  options[:has_many_through] ||= config.delete('has_many_through') || false
108
+ options[:optional_belongs_to] ||= config.delete('optional_belongs_to') || false
104
109
  if (module_name = config.delete('module'))
105
110
  raise ArgumentError, "Invalid module name: #{module_name}" unless module_name.match?(/\A[A-Z][A-Za-z0-9_]*\z/)
106
111
 
@@ -116,7 +121,8 @@ DYNAMIC_DATABASE = DynamicActiveModel::Explorer.explore(
116
121
  options[:skip_tables],
117
122
  options[:relationships],
118
123
  foreign_key_constraints: options[:foreign_key_constraints] || false,
119
- has_many_through: options[:has_many_through] || false
124
+ has_many_through: options[:has_many_through] || false,
125
+ optional_belongs_to: options[:optional_belongs_to] || false
120
126
  )
121
127
 
122
128
  if options[:class_files_dir]
@@ -66,6 +66,14 @@ module DynamicActiveModel
66
66
  @has_many_through = true
67
67
  end
68
68
 
69
+ # Makes every belongs_to optional, including those on NOT NULL foreign keys, so saving
70
+ # a record doesn't validate that its parent exists (or query for it). For tools that
71
+ # write rows whose parents may be missing, such as data repair scripts.
72
+ # @return [void]
73
+ def use_optional_belongs_to!
74
+ @optional_belongs_to = true
75
+ end
76
+
69
77
  # Builds all relationships between models based on foreign keys and constraints
70
78
  # This method:
71
79
  # 1. Maps foreign keys to their corresponding models
@@ -155,10 +163,19 @@ module DynamicActiveModel
155
163
  class_name: belongs_to_model.name,
156
164
  foreign_key: foreign_key,
157
165
  primary_key: primary_key,
158
- optional: nullable_column?(model, foreign_key)
166
+ optional: optional_belongs_to?(model, foreign_key)
159
167
  )
160
168
  end
161
169
 
170
+ # Whether a belongs_to is optional: always with use_optional_belongs_to!, otherwise
171
+ # only when its foreign key column is nullable.
172
+ # @param model [Class]
173
+ # @param column_name [String]
174
+ # @return [Boolean]
175
+ def optional_belongs_to?(model, column_name)
176
+ @optional_belongs_to || nullable_column?(model, column_name)
177
+ end
178
+
162
179
  # A nullable foreign key makes the belongs_to optional; NOT NULL makes it required,
163
180
  # whatever belongs_to_required_by_default is set to.
164
181
  # @param model [Class]
@@ -38,14 +38,18 @@ module DynamicActiveModel
38
38
  # foreign key constraints (see ForeignKeyConstraints)
39
39
  # @param has_many_through [Boolean] Also add has_many :through across join models
40
40
  # (see HasManyThrough)
41
+ # @param optional_belongs_to [Boolean] Make every belongs_to optional, even on NOT NULL
42
+ # foreign keys, so saves don't validate the parent exists
41
43
  # @return [Database] The configured database instance
42
44
  # @raise [ClassNameConflict] If two tables map to the same class name
43
45
  def self.explore(base_module, connection_options, skip_tables = [], relationships = {}, table_class_names = {},
44
- parent_class: nil, foreign_key_constraints: false, has_many_through: false)
46
+ parent_class: nil, foreign_key_constraints: false, has_many_through: false,
47
+ optional_belongs_to: false)
45
48
  database = create_models!(base_module, connection_options, skip_tables, table_class_names,
46
49
  parent_class: parent_class)
47
50
  build_relationships!(database, relationships, foreign_key_constraints: foreign_key_constraints,
48
- has_many_through: has_many_through)
51
+ has_many_through: has_many_through,
52
+ optional_belongs_to: optional_belongs_to)
49
53
  database
50
54
  end
51
55
 
@@ -78,11 +82,14 @@ module DynamicActiveModel
78
82
  # @param relationships [Hash] Custom foreign key relationships to add
79
83
  # @param foreign_key_constraints [Boolean] Also relate columns through foreign key constraints
80
84
  # @param has_many_through [Boolean] Also add has_many :through across join models
85
+ # @param optional_belongs_to [Boolean] Make every belongs_to optional
81
86
  # @return [void]
82
- def self.build_relationships!(database, relationships, foreign_key_constraints: false, has_many_through: false)
87
+ def self.build_relationships!(database, relationships, foreign_key_constraints: false, has_many_through: false,
88
+ optional_belongs_to: false)
83
89
  relations = Associations.new(database)
84
90
  relations.use_foreign_key_constraints! if foreign_key_constraints
85
91
  relations.use_has_many_through! if has_many_through
92
+ relations.use_optional_belongs_to! if optional_belongs_to
86
93
  relationships.each do |table_name, foreign_keys|
87
94
  foreign_keys.each do |foreign_key, relationship_name|
88
95
  relations.add_foreign_key(table_name, foreign_key, relationship_name)
@@ -55,6 +55,7 @@ module DynamicActiveModel
55
55
  parent_class: nil,
56
56
  foreign_key_constraints: false,
57
57
  has_many_through: false,
58
+ optional_belongs_to: false,
58
59
  skip_tables: [],
59
60
  relationships: {},
60
61
  table_class_names: {},
@@ -104,6 +105,15 @@ module DynamicActiveModel
104
105
  dynamic_active_model_config[:has_many_through]
105
106
  end
106
107
 
108
+ # Sets or gets whether every belongs_to is optional, including those on NOT NULL
109
+ # foreign keys, so saving a record doesn't validate that its parent exists
110
+ # @param enabled [Boolean, nil]
111
+ # @return [Boolean] The current setting
112
+ def optional_belongs_to(enabled = nil)
113
+ update_config(:optional_belongs_to, enabled) unless enabled.nil?
114
+ dynamic_active_model_config[:optional_belongs_to]
115
+ end
116
+
107
117
  # Sets or gets the list of tables to skip
108
118
  # @param tables [Array<String>, nil] Tables to skip
109
119
  # @return [Array<String>] The current list of skipped tables
@@ -180,7 +190,8 @@ module DynamicActiveModel
180
190
  table_class_names,
181
191
  parent_class: parent_class,
182
192
  foreign_key_constraints: foreign_key_constraints,
183
- has_many_through: has_many_through
193
+ has_many_through: has_many_through,
194
+ optional_belongs_to: optional_belongs_to
184
195
  )
185
196
  )
186
197
  database.update_all_models(extensions_path, extensions_suffix) if extensions_path
@@ -2,5 +2,5 @@
2
2
 
3
3
  module DynamicActiveModel
4
4
  # Gem version, bumped by release-please
5
- VERSION = '1.3.2'
5
+ VERSION = '1.4.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch