dynamic-active-model 1.3.1 → 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 +4 -4
- data/bin/dynamic-db-explorer +7 -1
- data/lib/dynamic-active-model/associations.rb +28 -1
- data/lib/dynamic-active-model/explorer.rb +10 -3
- data/lib/dynamic-active-model/setup.rb +12 -1
- data/lib/dynamic-active-model/template_class_file.rb +2 -1
- data/lib/dynamic-active-model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 014a76565d40730ce4afc012b743af0b83ce49cf375d46f75b30f678bd5fc253
|
|
4
|
+
data.tar.gz: c55980904ccf254e5c24d20a4e94b1e0d6c4f8a1596e74f469c7543fcac67c78
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d9fb9bbec8b8480d6a69d6c1a987a8cf814e76c24bcc2a56e0d9960cd86d7e1f4e87baef1a5f53ac8f61faebaa5979d691e568d745ec94294d5f9e84f1be24b5
|
|
7
|
+
data.tar.gz: 2b0a0078aa60d97ddf0a713fdb4af1caf5d713eaae56faaee8f9802e5e02e96a95a0ed2eeb121a98cf8af8b1a07ff3ebbd10309f360e6421d5ebd92058a83ef1
|
data/bin/dynamic-db-explorer
CHANGED
|
@@ -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
|
|
@@ -154,10 +162,29 @@ module DynamicActiveModel
|
|
|
154
162
|
relationship_name.singularize.to_sym,
|
|
155
163
|
class_name: belongs_to_model.name,
|
|
156
164
|
foreign_key: foreign_key,
|
|
157
|
-
primary_key: primary_key
|
|
165
|
+
primary_key: primary_key,
|
|
166
|
+
optional: optional_belongs_to?(model, foreign_key)
|
|
158
167
|
)
|
|
159
168
|
end
|
|
160
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
|
+
|
|
179
|
+
# A nullable foreign key makes the belongs_to optional; NOT NULL makes it required,
|
|
180
|
+
# whatever belongs_to_required_by_default is set to.
|
|
181
|
+
# @param model [Class]
|
|
182
|
+
# @param column_name [String]
|
|
183
|
+
# @return [Boolean]
|
|
184
|
+
def nullable_column?(model, column_name)
|
|
185
|
+
model.columns_hash.fetch(column_name.to_s).null
|
|
186
|
+
end
|
|
187
|
+
|
|
161
188
|
# Adds a has_many relationship to a model
|
|
162
189
|
# @param relationship_name [String] Name of the relationship
|
|
163
190
|
# @param model [Class] The referenced (parent) model that gets the association
|
|
@@ -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
|
|
@@ -111,7 +111,7 @@ module DynamicActiveModel
|
|
|
111
111
|
def append_association!(str, assoc_type, assoc, association_options)
|
|
112
112
|
str << " #{assoc_type} #{assoc.name.inspect}"
|
|
113
113
|
association_options.compact.each do |name, value|
|
|
114
|
-
str << ", #{name}: '#{value}'"
|
|
114
|
+
str << ", #{name}: #{value.is_a?(String) ? "'#{value}'" : value.inspect}"
|
|
115
115
|
end
|
|
116
116
|
str << "\n"
|
|
117
117
|
end
|
|
@@ -141,6 +141,7 @@ module DynamicActiveModel
|
|
|
141
141
|
assoc.options[:foreign_key]
|
|
142
142
|
end
|
|
143
143
|
options[:primary_key] = assoc.options[:primary_key] unless assoc.options[:primary_key] == 'id'
|
|
144
|
+
options[:optional] = true if assoc.options[:optional]
|
|
144
145
|
options
|
|
145
146
|
end
|
|
146
147
|
|