From 55004984e12909b483663d818a1eadb780da70e4 Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@users.osdn.me>
Date: Tue, 28 Nov 2023 10:48:29 +0000
Subject: [PATCH] Fix autoloading plugin classes

---
 config/application.rb                              | 2 ++
 config/environments/test.rb                        | 3 +++
 lib/redmine/plugin_loader.rb                       | 4 ++--
 test/fixtures/plugins/foo_plugin/app/models/foo.rb | 4 ++++
 test/unit/lib/redmine/plugin_loader_test.rb        | 7 ++++++-
 test/unit/lib/redmine/plugin_test.rb               | 4 ++--
 6 files changed, 19 insertions(+), 5 deletions(-)
 create mode 100644 test/fixtures/plugins/foo_plugin/app/models/foo.rb

diff --git a/config/application.rb b/config/application.rb
index 48e855c9b..579fa1d9b 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -85,6 +85,8 @@ module RedmineApp
     # for more options (same options as config.cache_store).
     config.redmine_search_cache_store = :memory_store
 
+    config.redmine_plugin_directory = 'plugins'
+
     # Configure log level here so that additional environment file
     # can change it (environments/ENV.rb would take precedence over it)
     config.log_level = Rails.env.production? ? :info : :debug
diff --git a/config/environments/test.rb b/config/environments/test.rb
index 199f1bb27..d96df0c2f 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -71,4 +71,7 @@ Rails.application.configure do
   # config.action_view.annotate_rendered_view_with_filenames = true
 
   config.secret_key_base = 'a secret token for running the tests'
+
+  # Change the plugin directory when testing.
+  config.redmine_plugin_directory = 'test/fixtures/plugins'
 end
diff --git a/lib/redmine/plugin_loader.rb b/lib/redmine/plugin_loader.rb
index 2151e949b..54393dc63 100644
--- a/lib/redmine/plugin_loader.rb
+++ b/lib/redmine/plugin_loader.rb
@@ -84,7 +84,7 @@ module Redmine
   class PluginLoader
     # Absolute path to the directory where plugins are located
     cattr_accessor :directory
-    self.directory = Rails.root.join('plugins')
+    self.directory = Rails.root.join Rails.application.config.redmine_plugin_directory
 
     # Absolute path to the plublic directory where plugins assets are copied
     cattr_accessor :public_directory
@@ -126,7 +126,7 @@ module Redmine
         # Add the plugin directories to rails autoload paths
         engine_cfg = Rails::Engine::Configuration.new(directory.to_s)
         engine_cfg.paths.add 'lib', eager_load: true
-        engine_cfg.eager_load_paths.each do |dir|
+        engine_cfg.paths.eager_load.each do |dir|
           Rails.autoloaders.main.push_dir dir
           Rails.application.config.watchable_dirs[dir] = [:rb]
         end
diff --git a/test/fixtures/plugins/foo_plugin/app/models/foo.rb b/test/fixtures/plugins/foo_plugin/app/models/foo.rb
new file mode 100644
index 000000000..db4a60096
--- /dev/null
+++ b/test/fixtures/plugins/foo_plugin/app/models/foo.rb
@@ -0,0 +1,4 @@
+# frozen_string_literal: true
+
+class Foo < ActiveRecord::Base
+end
diff --git a/test/unit/lib/redmine/plugin_loader_test.rb b/test/unit/lib/redmine/plugin_loader_test.rb
index ed5943346..a0bec218e 100644
--- a/test/unit/lib/redmine/plugin_loader_test.rb
+++ b/test/unit/lib/redmine/plugin_loader_test.rb
@@ -23,8 +23,9 @@ class Redmine::PluginLoaderTest < ActiveSupport::TestCase
   def setup
     clear_public
 
+    # Change plugin directory for testing to default in config/environments/tesr.rb.
+    # plugins/foo => test/fixtures/plugins/foo
     @klass = Redmine::PluginLoader
-    @klass.directory = Rails.root.join('test/fixtures/plugins')
     @klass.public_directory = Rails.root.join('tmp/public/plugin_assets')
     @klass.load
   end
@@ -55,6 +56,10 @@ class Redmine::PluginLoaderTest < ActiveSupport::TestCase
     assert File.exist?("#{@klass.public_directory}/foo_plugin/stylesheets/foo.css")
   end
 
+  def test_autoload
+    assert_equal true, Object.const_defined?(:Foo)
+  end
+
   def clear_public
     FileUtils.rm_rf 'tmp/public'
   end
diff --git a/test/unit/lib/redmine/plugin_test.rb b/test/unit/lib/redmine/plugin_test.rb
index 02df23236..3128efec2 100644
--- a/test/unit/lib/redmine/plugin_test.rb
+++ b/test/unit/lib/redmine/plugin_test.rb
@@ -22,9 +22,9 @@ require_relative '../../../test_helper'
 class Redmine::PluginTest < ActiveSupport::TestCase
   def setup
     @klass = Redmine::Plugin
-    # Change plugin directory for testing to default
+    # Change plugin directory for testing to default in config/environments/tesr.rb.
     # plugins/foo => test/fixtures/plugins/foo
-    @klass.directory = Rails.root.join('test/fixtures/plugins')
+
     # In case some real plugins are installed
     @klass.clear
 
-- 
2.39.2

