Skip to content

Commit

Permalink
Fix models stored in eager_load_paths are not picked up by #viable_mo…
Browse files Browse the repository at this point in the history
…dels

Closes #3373
app.paths.eager_load is the Rails default value and does not reflect application-specific eager_load_paths.
https://github.com/rails/rails/blob/f77ec9aa67afd6f28dfbd21970df4f58acf025be/railties/lib/rails/engine/configuration.rb#L79-L81
  • Loading branch information
mshibuya committed Aug 9, 2021
1 parent b627580 commit 238f18e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/config.rb
Expand Up @@ -334,7 +334,7 @@ def viable_models
included_models.collect(&:to_s).presence || begin
@@system_models ||= # memoization for tests
([Rails.application] + Rails::Engine.subclasses.collect(&:instance)).flat_map do |app|
(app.paths['app/models'].to_a + app.paths.eager_load).collect do |load_path|
(app.paths['app/models'].to_a + app.config.eager_load_paths).collect do |load_path|
Dir.glob(app.root.join(load_path)).collect do |load_dir|
Dir.glob(load_dir + '/**/*.rb').collect do |filename|
# app/models/module/class.rb => module/class.rb => module/class => Module::Class
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy_app/app/active_record/eager_loaded/basketball.rb
@@ -0,0 +1,2 @@
class Basketball < Ball
end
2 changes: 2 additions & 0 deletions spec/dummy_app/app/mongoid/eager_loaded/basketball.rb
@@ -0,0 +1,2 @@
class Basketball < Ball
end
5 changes: 3 additions & 2 deletions spec/dummy_app/config/application.rb
Expand Up @@ -23,8 +23,9 @@ class Application < Rails::Application
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.load_defaults Rails.version[0, 3]
config.eager_load_paths.reject! { |p| p =~ %r{/app/(\w+)$} && !%w(controllers helpers views).push(CI_ORM).include?(Regexp.last_match[1]) }
config.autoload_paths += %W(#{config.root}/app/#{CI_ORM} #{config.root}/app/#{CI_ORM}/concerns #{config.root}/lib)
config.eager_load_paths.reject! { |p| p =~ %r{/app/([^/]+)} && !%W(controllers locales mailers #{CI_ORM}).include?(Regexp.last_match[1]) }
config.eager_load_paths += %W(#{config.root}/app/#{CI_ORM}/eager_loaded)
config.autoload_paths += %W(#{config.root}/lib)
config.i18n.load_path += Dir[Rails.root.join('app', 'locales', '*.{rb,yml}').to_s]
config.active_record.time_zone_aware_types = [:datetime, :time] if CI_ORM == :active_record
config.active_storage.service = :local if defined?(ActiveStorage)
Expand Down
4 changes: 4 additions & 0 deletions spec/rails_admin/config_spec.rb
Expand Up @@ -255,6 +255,10 @@ class RecursivelyEmbedsMany
it 'should not include classnames start with Concerns::' do
expect(RailsAdmin::Config.models_pool.select { |m| m.match(/^Concerns::/) }).to be_empty
end

it 'includes models in the directory added by config.eager_load_paths' do
expect(RailsAdmin::Config.models_pool).to include('Basketball')
end
end

describe '.parent_controller' do
Expand Down

0 comments on commit 238f18e

Please sign in to comment.