How to use ActiveSupport::Configurable with Rails Engine

随声附和 提交于 2019-12-01 01:00:07

The source file for ActiveSupport::Configurable got decent documentation: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/configurable.rb

I like to put the configuration into it's own class within the engine (like kaminari does):

class MyGem
  def self.configuration
    @configuration ||= Configuration.new
  end

  def self.configure
    yield configuration
  end
end

class MyGem::Configuration
  include ActiveSupport::Configurable

  config_accessor(:foo) { "use a block to set default value" }
  config_accessor(:bar) # no default (nil)
end

Now I can configure the engine with this API:

MyGem.configure do |config|
  config.bar = 'baz'
end

And access the configuration with

MyGem.configuration.bar

try this out

I hope this is simple and clear.

Example Code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!