我正在开发一个与第三方处理 OAuth 机制的 Rails 引擎。我想将所有配置都放在 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 仍然未定义,所以它失败了。