strict_associations 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 14e631cb0e94a7fd616afc1c62fd686625008b0f30916a6f45a3614038e4a460
4
+ data.tar.gz: a229561f5c4b1a54abf6433e54cfa1e13ec0480cd2274906fef3f5c52aa85049
5
+ SHA512:
6
+ metadata.gz: 57336b91485f245790e47e8e6afe1974024f308b747c628b7eb370640e30873e2f190c196974f182dede3591fbcec43e2ba3fec03cb1536b9a1ad600cf6cc32e
7
+ data.tar.gz: 24dadd9079a717411b2a76a7df76c2bf056140ab13d662e6879e4de7b3b070100ffeef25669e1ba65526f51cfe5e28b741654efd01db2561bbe7a12944b9936c
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2026-03-12
4
+
5
+ ### Added
6
+
7
+ - Initial release
8
+ - Validates `has_many` and `has_one` associations declare `:dependent`
9
+ - Validates `belongs_to` associations have a matching inverse
10
+ - Validates polymorphic `belongs_to` declares `valid_types:`
11
+ - Detects `has_and_belongs_to_many` usage (configurable)
12
+ - Per-association opt-out via `strict: false`
13
+ - Per-model opt-out via `skip_strict_association`
14
+ - Railtie for automatic setup in Rails applications
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 SOFware
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrictAssociations
4
+ class Configuration
5
+ def initialize
6
+ @habtm_allowed = false
7
+ end
8
+
9
+ def allow_habtm(value = true)
10
+ @habtm_allowed = value
11
+ end
12
+
13
+ def habtm_allowed?
14
+ @habtm_allowed
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrictAssociations
4
+ class Railtie < Rails::Railtie
5
+ initializer "strict_associations.extend_active_record" do
6
+ ActiveSupport.on_load(:active_record) do
7
+ # Register :strict and :valid_types as recognized association options
8
+ %i[BelongsTo HasMany HasOne].each do |builder_name|
9
+ ActiveRecord::Associations::Builder
10
+ .const_get(builder_name)
11
+ .singleton_class.prepend(
12
+ Module.new do
13
+ define_method(:valid_options) do |options|
14
+ extra = [:strict]
15
+ extra << :valid_types if builder_name == :BelongsTo
16
+
17
+ super(options) + extra
18
+ end
19
+ end
20
+ )
21
+ end
22
+
23
+ # Provide .skip_strict_association on all models
24
+ #
25
+ # Inside on_load(:active_record), self is ActiveRecord::Base, so we define
26
+ # directly.
27
+ def self.skip_strict_association(*names)
28
+ @_strict_associations_skipped ||= Set.new
29
+ names.each do |n|
30
+ @_strict_associations_skipped.add(n.to_sym)
31
+ end
32
+ end
33
+
34
+ def self.strict_associations_skipped
35
+ @_strict_associations_skipped || Set.new
36
+ end
37
+
38
+ def self.strict_association_skipped?(name)
39
+ klass = self
40
+ while klass && klass != ActiveRecord::Base
41
+ return true if klass.strict_associations_skipped.include?(name.to_sym)
42
+
43
+ klass = klass.superclass
44
+ end
45
+ false
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrictAssociations
4
+ class Validator
5
+ def initialize(configuration, models: nil)
6
+ @config = configuration
7
+ @explicit_models = models
8
+ end
9
+
10
+ def call
11
+ violations = []
12
+
13
+ models_to_check.each do |model|
14
+ check_habtm(model, violations)
15
+ check_belongs_to_inverses(model, violations)
16
+ check_polymorphic_inverses(model, violations)
17
+ check_dependent_options(model, violations)
18
+ end
19
+
20
+ violations
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :config, :explicit_models
26
+
27
+ def models_to_check
28
+ candidates = explicit_models || all_models
29
+ candidates.reject do |model|
30
+ model.abstract_class? || !safe_table_exists?(model) || view?(model)
31
+ end
32
+ end
33
+
34
+ def all_models
35
+ ActiveRecord::Base.descendants.reject(&:abstract_class?)
36
+ end
37
+
38
+ def safe_table_exists?(model)
39
+ model.table_exists?
40
+ rescue ActiveRecord::NoDatabaseError
41
+ false
42
+ end
43
+
44
+ def view?(model)
45
+ model.connection.view_exists?(model.table_name)
46
+ rescue ActiveRecord::NoDatabaseError
47
+ false
48
+ end
49
+
50
+ def check_habtm(model, violations)
51
+ return if config.habtm_allowed?
52
+
53
+ habtm = :has_and_belongs_to_many
54
+ model.reflect_on_all_associations(habtm).each do |ref|
55
+ next if skipped?(model, ref)
56
+
57
+ violations << Violation.new(
58
+ model:,
59
+ association_name: ref.name,
60
+ rule: :habtm_banned,
61
+ message: <<~MSG.squish
62
+ has_and_belongs_to_many is not allowed.
63
+ Use a join model with has_many :through instead.
64
+ MSG
65
+ )
66
+ end
67
+ end
68
+
69
+ def check_belongs_to_inverses(model, violations)
70
+ refs = model.reflect_on_all_associations(:belongs_to)
71
+ refs.each do |ref|
72
+ next if skipped?(model, ref)
73
+ next if ref.options[:polymorphic]
74
+
75
+ target = resolve_target(ref)
76
+ next unless target
77
+
78
+ unless inverse_exists?(model, ref, target)
79
+ fk = ref.foreign_key
80
+ inverse_name = model.table_name.to_sym
81
+ violations << Violation.new(
82
+ model:,
83
+ association_name: ref.name,
84
+ rule: :missing_inverse,
85
+ message: <<~MSG.squish
86
+ #{target} has no has_many/has_one pointing back to \
87
+ #{model.table_name} with foreign key #{fk}. \
88
+ Define the inverse. \
89
+ Or mark this association with strict: false. \
90
+ Or call skip_strict_association :#{inverse_name} \
91
+ on #{target}
92
+ MSG
93
+ )
94
+ end
95
+ end
96
+ end
97
+
98
+ def check_polymorphic_inverses(model, violations)
99
+ refs = model.reflect_on_all_associations(:belongs_to)
100
+ refs.each do |ref|
101
+ next if skipped?(model, ref)
102
+ next unless ref.options[:polymorphic]
103
+
104
+ resolved = resolve_valid_types(model, ref)
105
+
106
+ unless resolved
107
+ violations << Violation.new(
108
+ model:,
109
+ association_name: ref.name,
110
+ rule: :unregistered_polymorphic,
111
+ message: <<~MSG.squish
112
+ #{model}##{ref.name} is polymorphic but has no \
113
+ valid_types declared. Add valid_types: to the \
114
+ association. Or mark with strict: false.
115
+ MSG
116
+ )
117
+ next
118
+ end
119
+
120
+ type_violations, types =
121
+ resolved.partition { |r| r.is_a?(Violation) }
122
+ violations.concat(type_violations)
123
+ check_registered_polymorphic_types(
124
+ model, ref, types, violations
125
+ )
126
+ end
127
+ end
128
+
129
+ def resolve_valid_types(model, ref)
130
+ inline = ref.options[:valid_types]
131
+ return unless inline
132
+
133
+ resolved = []
134
+ Array(inline).each do |t|
135
+ resolved << t.to_s.constantize
136
+ rescue NameError => e
137
+ resolved << Violation.new(
138
+ model:,
139
+ association_name: ref.name,
140
+ rule: :invalid_valid_type,
141
+ message: <<~MSG.squish
142
+ #{model}##{ref.name} declares valid_types \
143
+ containing "#{t}" but #{e.message}. \
144
+ Check for typos in valid_types.
145
+ MSG
146
+ )
147
+ end
148
+
149
+ resolved
150
+ end
151
+
152
+ def check_registered_polymorphic_types(
153
+ model, ref, types, violations
154
+ )
155
+ fk = ref.foreign_key.to_s
156
+ source_table = model.table_name
157
+
158
+ types.each do |type_class|
159
+ next if polymorphic_inverse_exists?(
160
+ type_class, source_table, fk
161
+ )
162
+
163
+ violations << Violation.new(
164
+ model:,
165
+ association_name: ref.name,
166
+ rule: :missing_polymorphic_inverse,
167
+ message: <<~MSG.squish
168
+ #{type_class} is registered for \
169
+ #{model}##{ref.name} but has no has_many/has_one \
170
+ pointing back to #{source_table} with foreign \
171
+ key #{fk}.
172
+ MSG
173
+ )
174
+ end
175
+ end
176
+
177
+ def check_dependent_options(model, violations)
178
+ %i[has_many has_one].each do |macro|
179
+ model.reflect_on_all_associations(macro).each do |ref|
180
+ next if skipped?(model, ref)
181
+ next if ref.is_a?(
182
+ ActiveRecord::Reflection::ThroughReflection
183
+ )
184
+ next if target_is_view?(ref)
185
+
186
+ unless ref.options.key?(:dependent)
187
+ violations << Violation.new(
188
+ model:,
189
+ association_name: ref.name,
190
+ rule: :missing_dependent,
191
+ message: <<~MSG.squish
192
+ #{model}##{ref.name} is missing a :dependent \
193
+ option. Add dependent: :destroy, :delete_all, \
194
+ :nullify, or :restrict_with_exception. \
195
+ Or mark with strict: false. \
196
+ Or call skip_strict_association :#{ref.name} \
197
+ on #{model}.
198
+ MSG
199
+ )
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ def resolve_target(reflection)
206
+ reflection.klass
207
+ rescue NameError
208
+ nil
209
+ end
210
+
211
+ def inverse_exists?(source_model, belongs_to_ref, target)
212
+ fk = belongs_to_ref.foreign_key.to_s
213
+ source_table = source_model.table_name
214
+
215
+ target.reflect_on_all_associations.any? do |ref|
216
+ next unless %i[has_many has_one].include?(ref.macro)
217
+ next if ref.is_a?(
218
+ ActiveRecord::Reflection::ThroughReflection
219
+ )
220
+
221
+ begin
222
+ ref.klass.table_name == source_table &&
223
+ ref.foreign_key.to_s == fk
224
+ rescue NameError
225
+ false
226
+ end
227
+ end
228
+ end
229
+
230
+ def polymorphic_inverse_exists?(type, table, fk)
231
+ type.reflect_on_all_associations.any? do |ref|
232
+ next unless %i[has_many has_one].include?(ref.macro)
233
+ next if ref.is_a?(
234
+ ActiveRecord::Reflection::ThroughReflection
235
+ )
236
+
237
+ begin
238
+ ref.klass.table_name == table &&
239
+ ref.foreign_key.to_s == fk
240
+ rescue NameError
241
+ false
242
+ end
243
+ end
244
+ end
245
+
246
+ def target_is_view?(reflection)
247
+ target = resolve_target(reflection)
248
+ target && view?(target)
249
+ end
250
+
251
+ def skipped?(model, ref)
252
+ ref.options[:strict] == false ||
253
+ model.strict_association_skipped?(ref.name)
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrictAssociations
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrictAssociations
4
+ class Violation
5
+ attr_reader :model, :association_name, :rule, :message
6
+
7
+ def initialize(model:, association_name:, rule:, message:)
8
+ @model = model
9
+ @association_name = association_name
10
+ @rule = rule
11
+ @message = message
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "strict_associations/version"
4
+ require_relative "strict_associations/configuration"
5
+ require_relative "strict_associations/validator"
6
+ require_relative "strict_associations/violation"
7
+ require_relative "strict_associations/railtie" if defined?(Rails::Railtie)
8
+
9
+ module StrictAssociations
10
+ class ViolationError < StandardError; end
11
+
12
+ class << self
13
+ def configure(&block)
14
+ @configure_block = block
15
+ block.call(configuration)
16
+ end
17
+
18
+ def configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def validate!(models: nil)
23
+ eager_load_if_needed
24
+ config = Configuration.new
25
+ @configure_block&.call(config)
26
+ @configuration = config
27
+
28
+ validator = Validator.new(config, models:)
29
+ violations = validator.call
30
+
31
+ return if violations.empty?
32
+
33
+ raise ViolationError, format_violations(violations)
34
+ end
35
+
36
+ def reset!
37
+ @configuration = nil
38
+ @configure_block = nil
39
+ end
40
+
41
+ private
42
+
43
+ def eager_load_if_needed
44
+ return unless defined?(Rails)
45
+ return if Rails.application.config.eager_load
46
+
47
+ Rails.application.eager_load!
48
+ end
49
+
50
+ def format_violations(violations)
51
+ lines = violations.map do |v|
52
+ " #{v.model}.#{v.association_name}" \
53
+ " [#{v.rule}]: #{v.message}"
54
+ end
55
+
56
+ <<~MSG
57
+ StrictAssociations found #{violations.size} violation(s):
58
+
59
+ #{lines.join("\n")}
60
+ MSG
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strict_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lange
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
32
+ - !ruby/object:Gem::Dependency
33
+ name: railties
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '7.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '7.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9'
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '3.0'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "~>"
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ - !ruby/object:Gem::Dependency
67
+ name: sqlite3
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '2.0'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '2.0'
80
+ - !ruby/object:Gem::Dependency
81
+ name: standard
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '1.0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '0.22'
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '0.22'
108
+ - !ruby/object:Gem::Dependency
109
+ name: reissue
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: '0.4'
115
+ type: :development
116
+ prerelease: false
117
+ version_requirements: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '0.4'
122
+ description: |
123
+ Enforces that both sides of an association (e.g. belongs_to, has_many) are
124
+ explicitly defined. Enforces that has_many (or has_one) associations define a
125
+ :dependent option, or explicitly opt-out via `strict: false`
126
+ email:
127
+ - jeff.lange@sofwarellc.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - CHANGELOG.md
133
+ - LICENSE.txt
134
+ - lib/strict_associations.rb
135
+ - lib/strict_associations/configuration.rb
136
+ - lib/strict_associations/railtie.rb
137
+ - lib/strict_associations/validator.rb
138
+ - lib/strict_associations/version.rb
139
+ - lib/strict_associations/violation.rb
140
+ homepage: https://github.com/SOFware/strict_associations
141
+ licenses:
142
+ - MIT
143
+ metadata:
144
+ source_code_uri: https://github.com/SOFware/strict_associations
145
+ changelog_uri: https://github.com/SOFware/strict_associations/blob/main/CHANGELOG.md
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '3.1'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubygems_version: 3.6.7
161
+ specification_version: 4
162
+ summary: Enforces explicit definition of both sides of ActiveRecord associations (e.g.
163
+ belongs_to, has_many)
164
+ test_files: []