strict_associations 0.1.7 → 0.1.8
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 +28 -0
- 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: c92d1223136a5265b920421b6905b4973aab91b4e6556d66e8a3d37fd07aba8b
|
|
4
|
+
data.tar.gz: d11fb79bd366064f7d3179bb20b2255da37a531db99e8fc0173e74b841a873d9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ca2c4f5da3309031d53bee33e67d4f0bb42ddc7f676f7f307d1fa1075559bcb117ad5928478ff5b6488d7947a54c061687ca5a2f34cfc848befbe4236bd6d20
|
|
7
|
+
data.tar.gz: e21378c926f911085b51d4fb5e0ab3b08db8ba12dbd1d9e5d3e6fa8abc8eb4f56e0431026cd9f1e16f05dfdb357279b156a3c5eca3e3734f1f5e01ab52780ec3
|
data/CHANGELOG.md
CHANGED
|
@@ -247,6 +247,13 @@ module StrictAssociations
|
|
|
247
247
|
(fk_columns - defined_fk_columns).each do |column|
|
|
248
248
|
assoc_name = column.delete_suffix("_id").to_sym
|
|
249
249
|
next if model.strict_association_skipped?(assoc_name)
|
|
250
|
+
# For third-party models, skip if any model already has a
|
|
251
|
+
# has_many/has_one pointing to this table with this FK.
|
|
252
|
+
# The dev can't add belongs_to to third-party code, but
|
|
253
|
+
# they CAN define has_many on their own model.
|
|
254
|
+
if is_third_party
|
|
255
|
+
next if has_many_covers_fk?(model, column)
|
|
256
|
+
end
|
|
250
257
|
|
|
251
258
|
msg = if is_third_party
|
|
252
259
|
<<~MSG.squish
|
|
@@ -298,6 +305,27 @@ module StrictAssociations
|
|
|
298
305
|
.map(&:column)
|
|
299
306
|
end
|
|
300
307
|
|
|
308
|
+
# Checks if any loaded model has a has_many/has_one pointing
|
|
309
|
+
# to the given model's table with the given FK column.
|
|
310
|
+
def has_many_covers_fk?(model, fk_column)
|
|
311
|
+
table = model.table_name
|
|
312
|
+
all_models.any? do |other|
|
|
313
|
+
other.reflect_on_all_associations.any? do |ref|
|
|
314
|
+
next unless %i[has_many has_one].include?(ref.macro)
|
|
315
|
+
next if ref.is_a?(
|
|
316
|
+
ActiveRecord::Reflection::ThroughReflection
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
begin
|
|
320
|
+
ref.klass.table_name == table &&
|
|
321
|
+
ref.foreign_key.to_s == fk_column
|
|
322
|
+
rescue NameError
|
|
323
|
+
false
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
301
329
|
def resolve_target(reflection)
|
|
302
330
|
reflection.klass
|
|
303
331
|
rescue NameError
|