Rails 3 Engine - 为用户提供配置

发布于 2024-09-30 04:53:31 字数 828 浏览 0 评论 0原文

我似乎找不到任何有关如何执行以下操作的文档:我需要为使用我的引擎的任何应用程序提供一个配置变量,以便它们可以轻松地将设置传递到我的引擎。

有人有任何正确或可接受的方法的链接吗?

编辑:作为更新,我找到了一个不错的方法来做到这一点。代码如下。

# file: lib/my_engine.rb
module MyEngine

  class Engine < Rails::Engine

    initializer "my_engine.configure_rails_initialization" do |app|
      # Engine configures Rails app here, this is not what my question was about
    end

  end

  # This is what I was trying to figure out
  def self.config(&block)
    @@config ||= MyEngine::Configuration.new

    yield @@config if block

    return @@config
  end

end

这允许任何使用我的引擎的应用程序在其任何初始值设定项或其environment.rb文件中像下面这样配置它,调用 MyEngine::Configuration 类中定义的任何方法:

MyEngine.config do |config|
  config.some_configuration_option = "Whatever"
end

I can't seem to find any documentation on how to do the following: I need to provide a config variable for any applications using my engine so that they can easily pass settings to my engine.

Does anyone have any links to a proper or accepted way to do this?

EDIT: As an update, I figured out a decent way to do this. Code is below.

# file: lib/my_engine.rb
module MyEngine

  class Engine < Rails::Engine

    initializer "my_engine.configure_rails_initialization" do |app|
      # Engine configures Rails app here, this is not what my question was about
    end

  end

  # This is what I was trying to figure out
  def self.config(&block)
    @@config ||= MyEngine::Configuration.new

    yield @@config if block

    return @@config
  end

end

This allows any application using my engine to configure it like below in any of their initializers or their environment.rb file, calling any methods defined in the MyEngine::Configuration class:

MyEngine.config do |config|
  config.some_configuration_option = "Whatever"
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-10-07 04:53:31

简短回答:

创建一个名为 your_engine_class.rb 的文件并放入托管应用程序的配置/初始化程序中。以下是该文件的示例。

module YourEngineClass
  class Engine < Rails::Engine
    config.param_name = 'value'    
  end
end

然后在你的主engine.rb中你可以访问:

config.param_name

更长的答案:我创建了一个存根引擎,其中包括这个和你需要的许多其他标准配置,你可以使用它作为起点:

http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

Short answer:

Create a file called your_engine_class.rb and put in config/initializers of your hosting app. Here is an example of this file.

module YourEngineClass
  class Engine < Rails::Engine
    config.param_name = 'value'    
  end
end

Then inside your main engine.rb you can access:

config.param_name

Longer answer: I created a stub engine that includes this and lots of other standard configuration you'll need, you can use it as a starting point:

http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

动听の歌 2024-10-07 04:53:31

我个人喜欢 Devise 使用的模式,它在引擎的模块中定义一个类方法,然后在父应用程序中定义一个初始化程序。

创建要从父应用程序调用的类方法

module YourEngine
  class << self
    attr_accessor :your_config_var
  end

  def self.setup(&block)
    # You can yield your own object encapsulating your configuration logic/state
    yield self
  end
end

在父应用程序的初始化程序中调用类方法

YourEngine.setup do |config|
  config.your_config_var = "nyan cat"
end

现在,您可以在引擎的初始化过程中自由访问 your_config_var。您可以在引擎的 config/initializers 目录中创建一个文件,或使用 Rails Initializer钩子。

我喜欢这种方法,因为您可以更好地控制向客户端公开的配置对象,并且不会覆盖引擎的 config 方法。

I personally like the pattern Devise uses, which defines a class method in your engine's module and then an initializer in the parent application.

Create a class method to call from the parent application

module YourEngine
  class << self
    attr_accessor :your_config_var
  end

  def self.setup(&block)
    # You can yield your own object encapsulating your configuration logic/state
    yield self
  end
end

Call the class method in an initializer in the parent application

YourEngine.setup do |config|
  config.your_config_var = "nyan cat"
end

Now you are free to access your_config_var in your engine's initialization process. You can create a file in your engine's config/initializers directory or use the Rails Initializer hooks.

I like this approach since you have more control on what configuration object you expose to clients and aren't overriding the engine's config method.

可爱咩 2024-10-07 04:53:31

对我来说似乎更好如下:

#lib/my_engine.rb
require 'rails'

module MyEngine
  class Engine < Rails::Engine

  end

  def self.config(&block)
    yield Engine.config if block
    Engine.config
  end
end

Seems better to me as follow :

#lib/my_engine.rb
require 'rails'

module MyEngine
  class Engine < Rails::Engine

  end

  def self.config(&block)
    yield Engine.config if block
    Engine.config
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文