逐一查看rails s命令后所有的initializer之Eng

2019-11-05  本文已影响0人  will2yang

set_load_path:添加config里的一些路径到$LOAD_PATH并且uniq

initializer :set_load_path, before: :bootstrap_hook do
  _all_load_paths.reverse_each do |path|
    $LOAD_PATH.unshift(path) if File.directory?(path)
  end
  $LOAD_PATH.uniq!
end

设置好自动加载的路径和自动加载一次的路径,并且freeze防止被错误修改.

initializer :set_autoload_paths, before: :bootstrap_hook do
  ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
  ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)

  # Freeze so future modifications will fail rather than do nothing mysteriously
  config.autoload_paths.freeze
  config.eager_load_paths.freeze
  config.autoload_once_paths.freeze
end

add_routing_paths: 设置routes文件的路径

initializer :add_routing_paths do |app|
  routing_paths = paths["config/routes.rb"].existent

  if routes? || routing_paths.any?
    app.routes_reloader.paths.unshift(*routing_paths)
    app.routes_reloader.route_sets << routes
  end
end

add_locales:设置好i18n的路径

initializer :add_locales do
  config.i18n.railties_load_path << paths["config/locales"]
end

add_view_paths:设置好view文件的path

initializer :add_view_paths do
  views = paths["app/views"].existent
  unless views.empty?
    ActiveSupport.on_load(:action_controller) { prepend_view_path(views) if respond_to?(:prepend_view_path) }
    ActiveSupport.on_load(:action_mailer) { prepend_view_path(views) }
  end
end

load_environment_config: 设置好环境配置文件的路径

initializer :load_environment_config, before: :load_environment_hook, group: :all do
  paths["config/environments"].existent.each do |environment|
    require environment
  end
end

prepend_helpers_path:设置好帮助文件的路径

initializer :prepend_helpers_path do |app|
  if !isolated? || (app == self)
    app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
  end
end

load_config_initializers: 加载app的initializers文件

initializer :load_config_initializers do
  config.paths["config/initializers"].existent.sort.each do |initializer|
    load_config_initializer(initializer)
  end
end

engines_blank_point:额外的一些设置

initializer :engines_blank_point do
  # We need this initializer so all extra initializers added in engines are
  # consistently executed after all the initializers above across all engines.
end
上一篇下一篇

猜你喜欢

热点阅读