Skip to content

Commit 4bc5b88

Browse files
committed
Don't swallow errors caused by errors in the model class when detecting models
1 parent 9edd164 commit 4bc5b88

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

CHANEGLOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased (main)
44

5+
- Don't swallow errors caused by errors in the model class when detecting models
6+
57
## v1.0.0 (2025-04-08)
68

79
- Fix detection of relationships when there are multiple associations between users and companies

lib/userlist/rails.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ def self.current_company
4141

4242
def self.detect_model(*names)
4343
names.each do |name|
44-
begin
45-
model = name.constantize
44+
model = Object.const_get(name) if Object.const_defined?(name)
4645

47-
return model if model.is_a?(Class)
48-
rescue NameError
49-
false
50-
end
46+
return model if model.is_a?(Class)
5147
end
5248

5349
nil

spec/userlist/rails_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@
124124
end
125125
end
126126
end
127+
128+
describe '.detect_model' do
129+
it 'should raise an error when the model name is invalid' do
130+
expect { described_class.detect_model('invalid') }.to raise_error(NameError)
131+
end
132+
133+
it 'should not raise an error when the model is not found' do
134+
expect { described_class.detect_model('NonExistentModel') }.not_to raise_error
135+
end
136+
137+
it 'should return the model class when it exists' do
138+
expect(described_class.detect_model('User')).to eq(User)
139+
end
140+
141+
it 'should return the first model that exists when given a list of model names' do
142+
expect(described_class.detect_model('NonExistentModel', 'User')).to eq(User)
143+
end
144+
end
127145
end

0 commit comments

Comments
 (0)