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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 413a5326728f775abc9050baf64f35ab7f0f90d88ebb32627d360ade8156fb3e
4
- data.tar.gz: e2eb32329274995e220142c30c9c7055d2f6a675849b3df7d7680cddd17f8537
3
+ metadata.gz: a9c960e3ba7830927d9cbbad3d3c67365722f2a0db54e92bb6654bc4acc2e31b
4
+ data.tar.gz: c13c5b0b7f4307a4cae04f8e014b33ad857f778c925c9f6a467c83686a555b68
5
5
  SHA512:
6
- metadata.gz: e9aa8d31880309f3338f40dff593f768b6fee510e47a160e2a0a28f76c1cf949182f2e340cc8ef9a2ca40b70b812380e74386a5623e0489425406a0f0295b85e
7
- data.tar.gz: b558d3374c0b552323b73c47b5b34c2e37a873e8f0999d6fbd0687982e481854718322774b94f8444fad58ba724de899c16a784209ff07a936c3cac0e8595d29
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.1] - Unreleased
6
-
7
- ## [0.1.0] - 2026-03-12
5
+ ## [0.1.2] - Unreleased
8
6
 
9
- ### Added
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? || !safe_table_exists?(model) || view?(model)
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
- column = index.columns.first
249
- next unless index.columns.one? && column.end_with?("_id")
253
+ columns = index.columns
254
+ next unless columns.is_a?(Array) && columns.one?
255
+ next unless columns.first.end_with?("_id")
250
256
 
251
- column
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 || model.strict_association_skipped?(ref.name)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StrictAssociations
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict_associations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lange