我正在开发一个Rails引擎,它可以与第三方一起处理OAuth机制.我想在Struct属性中拥有所有配置,因此所有引擎配置数据都存储在一起:
require 'rails' module ThirdParty class Engine < ::Rails::Engine initializer "third-party.some_init_task" do |app| ThirdPartyConfig = Struct.new(:uri,:client_id,:client_secret,:redirect_uri) app.config.thirdparty = ThirdPartyConfig.new app.config.thirdparty.uri = "https://thirdparty.com" app.config.thirdparty.client_id = "" app.config.thirdparty.client_secret = "" app.config.thirdparty.redirect_uri = "" end end end
应在应用程序级初始化程序中定义一些配置:
class Application < Rails::Application config.thirdparty.client_id = <valid_client_id> config.thirdparty.client_secret = <valid_client_secret> config.thirdparty.redirect_uri = <redirect_uri> end
但是,由于config.thirdparty在加载应用程序初始化程序时仍未定义,因此失败.
解决方法
尝试为引擎中的初始化程序指定加载顺序.您应该能够在应用程序配置运行之前强制加载它.以下是您要加载初始值设定项的猜测
initializer "third-party.some_init_task",:before=> :load_config_initializers do |app|
如果这不起作用,请尝试在另一个初始化程序之前加载它.