开发者

Rails Engine configuration

开发者 https://www.devze.com 2023-03-08 21:34 出处:网络
I\'m developing a Rails Engine that handles the OAuth mechanism with a third party. I would like to have all the configuration in a Struct attribute, so all the engine config data is stored together:

I'm developing a Rails Engine that handles the OAuth mechanism with a third party. I would like to have all the configuration in a Struct attribute, so all the engine config data is stored together:

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 开发者_Go百科= ""
      app.config.thirdparty.client_secret = ""
      app.config.thirdparty.redirect_uri = ""
    end
  end
end

Some of the configuration should be defined in the application level initializers:

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

But as config.thirdparty still is undefined while loading the application initializer, it fails.


Try specifying the load order for the initializer in your engine. You should be able to force it to load before the application configuration runs. Here is a guess at the point you want to load your initializer

initializer "third-party.some_init_task", :before=> :load_config_initializers do |app|

If that does not work, try loading it before another initializer.


You can configure rails engine through host/main application

Initializing object within devolopment.rb envirorment using gem within rails mountable engine

0

精彩评论

暂无评论...
验证码 换一张
取 消