strict_associations 0.1.1 → 0.1.2
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 -13
- data/lib/strict_associations/validator.rb +30 -5
- 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: a9c960e3ba7830927d9cbbad3d3c67365722f2a0db54e92bb6654bc4acc2e31b
|
|
4
|
+
data.tar.gz: c13c5b0b7f4307a4cae04f8e014b33ad857f778c925c9f6a467c83686a555b68
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '018818a16c859a0b86d07ff832fd632d18ad0ef968ce34628d6a259858a1e028a219579f4056ead948fd35259018d067116a73f486fc75c084c54d7f647884c7'
|
|
7
|
+
data.tar.gz: a13326ea6f298d111bdbd98fb0d1ab92ad0a5f535907f03da210b375b02bd2cbef5a70229742acba4938328b2862d812bb89687f0b1a58002a7cad23fe269f86
|
data/CHANGELOG.md
CHANGED
|
@@ -2,17 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
## [0.1.
|
|
6
|
-
|
|
7
|
-
## [0.1.0] - 2026-03-12
|
|
5
|
+
## [0.1.2] - Unreleased
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
- Initial release
|
|
12
|
-
- Validates `has_many` and `has_one` associations declare `:dependent`
|
|
13
|
-
- Validates `belongs_to` associations have a matching inverse
|
|
14
|
-
- Validates polymorphic `belongs_to` declares `valid_types:`
|
|
15
|
-
- Detects `has_and_belongs_to_many` usage (configurable)
|
|
16
|
-
- Per-association opt-out via `strict: false`
|
|
17
|
-
- Per-model opt-out via `skip_strict_association`
|
|
18
|
-
- Railtie for automatic setup in Rails applications
|
|
7
|
+
## [0.1.1] - Unreleased
|
|
@@ -29,7 +29,10 @@ 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? ||
|
|
32
|
+
model.abstract_class? ||
|
|
33
|
+
!safe_table_exists?(model) ||
|
|
34
|
+
view?(model) ||
|
|
35
|
+
third_party?(model)
|
|
33
36
|
end
|
|
34
37
|
end
|
|
35
38
|
|
|
@@ -219,6 +222,8 @@ module StrictAssociations
|
|
|
219
222
|
end
|
|
220
223
|
|
|
221
224
|
def check_orphaned_foreign_keys(model, violations)
|
|
225
|
+
return unless owns_table?(model)
|
|
226
|
+
|
|
222
227
|
indexed_fk_columns = indexed_foreign_key_columns(model)
|
|
223
228
|
defined_fk_columns = model
|
|
224
229
|
.reflect_on_all_associations(:belongs_to)
|
|
@@ -245,10 +250,11 @@ module StrictAssociations
|
|
|
245
250
|
|
|
246
251
|
def indexed_foreign_key_columns(model)
|
|
247
252
|
model.connection.indexes(model.table_name).filter_map do |index|
|
|
248
|
-
|
|
249
|
-
next unless
|
|
253
|
+
columns = index.columns
|
|
254
|
+
next unless columns.is_a?(Array) && columns.one?
|
|
255
|
+
next unless columns.first.end_with?("_id")
|
|
250
256
|
|
|
251
|
-
|
|
257
|
+
columns.first
|
|
252
258
|
end
|
|
253
259
|
end
|
|
254
260
|
|
|
@@ -307,7 +313,26 @@ module StrictAssociations
|
|
|
307
313
|
end
|
|
308
314
|
|
|
309
315
|
def skipped?(model, ref)
|
|
310
|
-
ref.options[:strict] == false ||
|
|
316
|
+
ref.options[:strict] == false ||
|
|
317
|
+
model.strict_association_skipped?(ref.name) || third_party?(ref.active_record)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def third_party?(model)
|
|
321
|
+
return false unless model.name
|
|
322
|
+
|
|
323
|
+
source = Object.const_source_location(model.name)&.first
|
|
324
|
+
return false unless source
|
|
325
|
+
|
|
326
|
+
!File.expand_path(source).start_with?(app_root)
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def app_root
|
|
330
|
+
@app_root ||= File.expand_path(defined?(Rails) ? Rails.root.to_s : Dir.pwd)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def owns_table?(model)
|
|
334
|
+
model.superclass == ActiveRecord::Base ||
|
|
335
|
+
model.table_name != model.superclass.table_name
|
|
311
336
|
end
|
|
312
337
|
end
|
|
313
338
|
end
|