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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a41e36c429c6609f0bf28443610c799cb0d7ece5b2c72f31a3ba3f0dcabc1366
4
- data.tar.gz: dbc1018a1d8c146aa8c4e2d49558b8731b90d77a56b6dbcf52a204161de6e3af
3
+ metadata.gz: d51dbc1b21c0c15b0df12958d5faae54b5ac9fd03b91b8bc81fab41d4b7440c9
4
+ data.tar.gz: 82affa137bf43762c2ca968ff22460d51697f3c70ddea71f4a5bdeba67a0c0ed
5
5
  SHA512:
6
- metadata.gz: 5ce5abda2b0ed3dc7e7d397cc53d86f64706bb8b57ec953b51f9900a04262a86d169da1b70a4ee98d428d61a2160ea67f7f4ab4603dcfc2404fae601fb3a4d27
7
- data.tar.gz: 32d7d39a71358d1ecee58bb6a9d737d94d8fd1c483daf1036981d1d58e742fc8501240a45ec19560eb56ed33fc94e1f847c72bfe83eecb845f5939463b0c34ce
6
+ metadata.gz: 54beee9276456e62f0b3dd35d2c1aa878897c26772e9bd64dab8119a9b42877e084762066723abc26750bdcea885480a2c23f00294dc57f6db46a30e8a13e2ae
7
+ data.tar.gz: a460cf7ba3bdee9581bcf62c5f776f483e47bcb5be23b5ebd9f1a4ae699e468c1b38433d1af1f25d57ef1682321c26837ab28ca3ef61339c1b4ba3402504880f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
 
4
4
 
5
- ## [0.1.3] - Unreleased
5
+ ## [0.1.4] - Unreleased
6
6
 
7
- ## [0.1.2] - Unreleased
7
+ ## [0.1.3] - Unreleased
@@ -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.view_exists?(model.table_name)
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 <= ref.klass &&
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StrictAssociations
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lange