From 825dc17ae78f83d9043e9a225095711b365c7464 Mon Sep 17 00:00:00 2001 From: tohosaku Date: Tue, 26 Oct 2021 11:51:15 +0000 Subject: [PATCH 5/5] Use zeitwerk --- app/models/custom_field.rb | 2 -- app/models/enumeration.rb | 5 ----- app/models/group.rb | 2 -- app/models/group_builtin.rb | 3 --- app/models/principal.rb | 3 --- config/application.rb | 2 +- config/initializers/zeitwerk.rb | 31 +++++++++++++++++++++++++++++++ lib/redmine/plugin_loader.rb | 8 ++++---- lib/redmine/twofa.rb | 2 +- 9 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 config/initializers/zeitwerk.rb diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 9787b2ee4..b81dae233 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -359,5 +359,3 @@ class CustomField < ActiveRecord::Base end end end - -require_dependency 'redmine/field_format' diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index 53360e5ff..5f0e023ed 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -174,8 +174,3 @@ class Enumeration < ActiveRecord::Base end end end - -# Force load the subclasses in development mode -require_dependency 'time_entry_activity' -require_dependency 'document_category' -require_dependency 'issue_priority' diff --git a/app/models/group.rb b/app/models/group.rb index a676816ec..881dfb149 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -127,5 +127,3 @@ class Group < Principal Watcher.where('user_id = ?', id).delete_all end end - -require_dependency "group_builtin" diff --git a/app/models/group_builtin.rb b/app/models/group_builtin.rb index 0c29d50da..5d2eccde5 100644 --- a/app/models/group_builtin.rb +++ b/app/models/group_builtin.rb @@ -56,6 +56,3 @@ class GroupBuiltin < Group private :create_instance end end - -require_dependency "group_anonymous" -require_dependency "group_non_member" diff --git a/app/models/principal.rb b/app/models/principal.rb index 495d55669..7004e7d93 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -217,6 +217,3 @@ class Principal < ActiveRecord::Base end end end - -require_dependency "user" -require_dependency "group" diff --git a/config/application.rb b/config/application.rb index fc6e6a33f..e74fcbf67 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,7 +24,7 @@ module RedmineApp # -- all .rb files in that directory are automatically loaded. # Custom directories with classes and modules you want to be autoloadable. - config.autoload_paths += %W(#{config.root}/lib) + config.autoloader = :zeitwerk # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named. diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb new file mode 100644 index 000000000..f5630b3ee --- /dev/null +++ b/config/initializers/zeitwerk.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +lib = Rails.root.join('lib/redmine') +Rails.autoloaders.main.push_dir lib, namespace: Redmine + +IGNORE_LIST = [ + 'wiki_formatting/textile/redcloth3.rb', + 'core_ext.rb', + 'core_ext' +] + +class RedmineInflector < Zeitwerk::Inflector + def camelize(basename, abspath) + abspath.match?('redmine\/version.rb\z') ? 'VERSION' : super + end +end + +Rails.autoloaders.each do |loader| + loader.inflector = RedmineInflector.new + loader.inflector.inflect( + 'html' => 'HTML', + 'csv' => 'CSV', + 'pdf' => 'PDF', + 'url' => 'URL', + 'pop3' => 'POP3', + 'imap' => 'IMAP' + ) + IGNORE_LIST.each do |mod| + loader.ignore lib.join(mod) + end +end diff --git a/lib/redmine/plugin_loader.rb b/lib/redmine/plugin_loader.rb index 3009560e2..43219a116 100644 --- a/lib/redmine/plugin_loader.rb +++ b/lib/redmine/plugin_loader.rb @@ -89,7 +89,7 @@ module Redmine def self.create_assets_reloader plugin_assets_dirs = {} - @plugin_directories.each do |dir| + directories.each do |dir| plugin_assets_dirs[dir.assets_dir] = ['*'] end ActiveSupport::FileUpdateChecker.new([], plugin_assets_dirs) do @@ -123,9 +123,9 @@ 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 - Rails.application.config.eager_load_paths += engine_cfg.eager_load_paths - Rails.application.config.autoload_once_paths += engine_cfg.autoload_once_paths - Rails.application.config.autoload_paths += engine_cfg.autoload_paths + engine_cfg.eager_load_paths.each do |dir| + Rails.autoloaders.main.push_dir dir + end end end diff --git a/lib/redmine/twofa.rb b/lib/redmine/twofa.rb index 612cdec99..cfa0ea422 100644 --- a/lib/redmine/twofa.rb +++ b/lib/redmine/twofa.rb @@ -55,7 +55,7 @@ module Redmine def self.scan_builtin_schemes Dir[Rails.root.join('lib', 'redmine', 'twofa', '*.rb')].each do |file| - require_dependency file + require file end end private_class_method :scan_builtin_schemes -- 2.30.2