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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbe650347474030e15a049132a0e002e1a0dd421fd747d6b0a4c6cc2e8efce91
4
- data.tar.gz: 31ba848896030b174536c90bca7f4970a24272958bb8f15b7d85c03897ad0da7
3
+ metadata.gz: c92d1223136a5265b920421b6905b4973aab91b4e6556d66e8a3d37fd07aba8b
4
+ data.tar.gz: d11fb79bd366064f7d3179bb20b2255da37a531db99e8fc0173e74b841a873d9
5
5
  SHA512:
6
- metadata.gz: d290a381bce9063d13f5f6134145b4d05883681a28e3ac4b9fc44415846e7e0561ecdfdbe48572febfb8341f764c96ce1b68fe61ac2ee3715257c4d0e044ce21
7
- data.tar.gz: 4d872852b7716e934d3271af605f6264714601d262f0c1bdc7841310e4a1251c0acaffc82f4fa4610e552a5348bc4c5f317b07cd8eb55149f8b90609049219b7
6
+ metadata.gz: 8ca2c4f5da3309031d53bee33e67d4f0bb42ddc7f676f7f307d1fa1075559bcb117ad5928478ff5b6488d7947a54c061687ca5a2f34cfc848befbe4236bd6d20
7
+ data.tar.gz: e21378c926f911085b51d4fb5e0ab3b08db8ba12dbd1d9e5d3e6fa8abc8eb4f56e0431026cd9f1e16f05dfdb357279b156a3c5eca3e3734f1f5e01ab52780ec3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
 
4
4
 
5
- ## [0.1.7] - Unreleased
5
+ ## [0.1.8] - Unreleased
6
6
 
7
- ## [0.1.6] - Unreleased
7
+ ## [0.1.7] - Unreleased
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StrictAssociations
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lange