strict_associations 0.1.3 → 0.1.4
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/CHANGELOG.md +2 -2
- data/lib/strict_associations/validator.rb +31 -12
- data/lib/strict_associations/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: d51dbc1b21c0c15b0df12958d5faae54b5ac9fd03b91b8bc81fab41d4b7440c9
|
|
4
|
+
data.tar.gz: 82affa137bf43762c2ca968ff22460d51697f3c70ddea71f4a5bdeba67a0c0ed
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54beee9276456e62f0b3dd35d2c1aa878897c26772e9bd64dab8119a9b42877e084762066723abc26750bdcea885480a2c23f00294dc57f6db46a30e8a13e2ae
|
|
7
|
+
data.tar.gz: a460cf7ba3bdee9581bcf62c5f776f483e47bcb5be23b5ebd9f1a4ae699e468c1b38433d1af1f25d57ef1682321c26837ab28ca3ef61339c1b4ba3402504880f
|
data/CHANGELOG.md
CHANGED
|
@@ -29,10 +29,7 @@ module StrictAssociations
|
|
|
29
29
|
def models_to_check
|
|
30
30
|
candidates = explicit_models || all_models
|
|
31
31
|
candidates.reject do |model|
|
|
32
|
-
model.abstract_class? ||
|
|
33
|
-
!safe_table_exists?(model) ||
|
|
34
|
-
view?(model) ||
|
|
35
|
-
third_party?(model)
|
|
32
|
+
model.abstract_class? || !safe_table_exists?(model) || view?(model)
|
|
36
33
|
end
|
|
37
34
|
end
|
|
38
35
|
|
|
@@ -47,11 +44,22 @@ module StrictAssociations
|
|
|
47
44
|
end
|
|
48
45
|
|
|
49
46
|
def view?(model)
|
|
50
|
-
model.connection
|
|
47
|
+
conn = model.connection
|
|
48
|
+
table = model.table_name
|
|
49
|
+
conn.view_exists?(table) || materialized_view?(conn, table)
|
|
51
50
|
rescue ActiveRecord::NoDatabaseError
|
|
52
51
|
false
|
|
53
52
|
end
|
|
54
53
|
|
|
54
|
+
def materialized_view?(conn, table_name)
|
|
55
|
+
return false unless conn.adapter_name == "PostgreSQL"
|
|
56
|
+
|
|
57
|
+
conn.select_value(
|
|
58
|
+
"SELECT 1 FROM pg_matviews WHERE matviewname = " \
|
|
59
|
+
"#{conn.quote(table_name)}"
|
|
60
|
+
).present?
|
|
61
|
+
end
|
|
62
|
+
|
|
55
63
|
def check_habtm(model, violations)
|
|
56
64
|
return if config.habtm_allowed?
|
|
57
65
|
|
|
@@ -225,9 +233,7 @@ module StrictAssociations
|
|
|
225
233
|
return unless owns_table?(model)
|
|
226
234
|
|
|
227
235
|
indexed_fk_columns = indexed_foreign_key_columns(model)
|
|
228
|
-
defined_fk_columns = model
|
|
229
|
-
.reflect_on_all_associations(:belongs_to)
|
|
230
|
-
.map { |ref| ref.foreign_key.to_s }
|
|
236
|
+
defined_fk_columns = sti_family_foreign_keys(model)
|
|
231
237
|
|
|
232
238
|
(indexed_fk_columns - defined_fk_columns).each do |column|
|
|
233
239
|
assoc_name = column.delete_suffix("_id").to_sym
|
|
@@ -248,6 +254,22 @@ module StrictAssociations
|
|
|
248
254
|
end
|
|
249
255
|
end
|
|
250
256
|
|
|
257
|
+
# Collects belongs_to foreign keys from the model and all STI descendants sharing
|
|
258
|
+
# the same table. We check this because a belongs_to may be defined on one of the
|
|
259
|
+
# STI subclasses rather than the parent. In that case, a belongs_to IS defined,
|
|
260
|
+
# so we shouldn't raise a violation.
|
|
261
|
+
def sti_family_foreign_keys(model)
|
|
262
|
+
model_family = [model] + model.descendants.select do |descendant|
|
|
263
|
+
descendant.table_name == model.table_name
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
model_family.flat_map do |family_model|
|
|
267
|
+
family_model
|
|
268
|
+
.reflect_on_all_associations(:belongs_to)
|
|
269
|
+
.map { |ref| ref.foreign_key.to_s }
|
|
270
|
+
end.uniq
|
|
271
|
+
end
|
|
272
|
+
|
|
251
273
|
def indexed_foreign_key_columns(model)
|
|
252
274
|
model.connection.indexes(model.table_name).filter_map do |index|
|
|
253
275
|
columns = index.columns
|
|
@@ -271,12 +293,9 @@ module StrictAssociations
|
|
|
271
293
|
next if ref.options[:polymorphic]
|
|
272
294
|
|
|
273
295
|
begin
|
|
274
|
-
# Use <= instead of == so STI children match a belongs_to pointing to their
|
|
275
|
-
# parent
|
|
276
296
|
# The table_name guard ensures this only applies for true STI (shared
|
|
277
297
|
# table), not unrelated inheritance with different tables.
|
|
278
|
-
source_model
|
|
279
|
-
source_model.table_name == ref.klass.table_name &&
|
|
298
|
+
source_model.table_name == ref.klass.table_name &&
|
|
280
299
|
ref.foreign_key.to_s == fk
|
|
281
300
|
rescue NameError
|
|
282
301
|
false
|