gpt4 book ai didi

ruby-on-rails-3 - 从 Gem 添加到 Rails autoload_paths

转载 作者:行者123 更新时间:2023-12-04 05:42:43 24 4
gpt4 key购买 nike

我想写一个添加 app/services 的 gem Rails 应用程序的目录。

因为我想从 Gem 中添加它,所以我想出了这个解决方案:

class Railtie < ::Rails::Railtie
config.after_initialize do |app|
::Rails.logger.info "adding #{ActiveService::Configuration.path} to autoload_path"
app.config.autoload_paths = [ActiveService::Configuration.path] + app.config.autoload_paths
end
end

问题是 config.autoload_path是一个卡住数组,因此修改它似乎不是一个好主意。

关于如何以更好的方式实现这一目标的任何建议?

最佳答案

config.autoload_paths被卡住在 :set_autload_paths initializer .数组被传递给 ActiveSupport::Dependencies.autoload_paths ,所以修改原始数组没有多大意义。因此它被卡住了。

你应该可以连接到 :before => :set_autoload_paths并扩展config.autoload_paths在它通过和卡住之前:

class Railtie < ::Rails::Railtie
initializer 'activeservice.autoload', :before => :set_autoload_paths do |app|
app.config.autoload_paths << ActiveService::Configuration.path
end
end

关于初始化钩子(Hook)的文档可以在 guides.rubyonrails.org/initialization.html 找到。

关于ruby-on-rails-3 - 从 Gem 添加到 Rails autoload_paths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6279325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com