Skip to content

Commit

Permalink
Fix connection issues when initializing ActiveRecord Abstract models (#…
Browse files Browse the repository at this point in the history
…3470)

When initializing abstract (ActiveRecord) models, #table_exists? is used and calls through the connection, which (in the case of a precompile step) can raise errors other than the ActiveRecord::NoDatabaseError that's currently handled.
  • Loading branch information
codealchemy committed Feb 15, 2022
1 parent 3d7f3b3 commit d96def6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rails_admin/abstract_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def all(adapter = nil)
def new(m)
m = m.constantize unless m.is_a?(Class)
(am = old_new(m)).model && am.adapter ? am : nil
rescue *([LoadError, NameError] + (defined?(ActiveRecord) ? ['ActiveRecord::NoDatabaseError'.constantize] : []))
rescue *([LoadError, NameError] + (defined?(ActiveRecord) ? ['ActiveRecord::NoDatabaseError'.constantize, 'ActiveRecord::ConnectionNotEstablished'.constantize] : []))
puts "[RailsAdmin] Could not load model #{m}, assuming model is non existing. (#{$ERROR_INFO})" unless Rails.env.test?
nil
end
Expand Down
10 changes: 10 additions & 0 deletions spec/rails_admin/abstract_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
expect(RailsAdmin::AbstractModel.new('WithoutTable')).to eq nil
end
end

context 'on ActiveRecord::ConnectionNotEstablished', active_record: true do
before do
expect(WithoutTable).to receive(:table_exists?).and_raise(ActiveRecord::ConnectionNotEstablished)
end

it 'does not raise error and returns nil' do
expect(RailsAdmin::AbstractModel.new('WithoutTable')).to eq nil
end
end
end

describe '#to_s' do
Expand Down

0 comments on commit d96def6

Please sign in to comment.