Rails 3 Engine - 为用户提供配置
我似乎找不到任何有关如何执行以下操作的文档:我需要为使用我的引擎的任何应用程序提供一个配置变量,以便它们可以轻松地将设置传递到我的引擎。
有人有任何正确或可接受的方法的链接吗?
编辑:作为更新,我找到了一个不错的方法来做到这一点。代码如下。
# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短回答:
创建一个名为 your_engine_class.rb 的文件并放入托管应用程序的配置/初始化程序中。以下是该文件的示例。
然后在你的主engine.rb中你可以访问:
更长的答案:我创建了一个存根引擎,其中包括这个和你需要的许多其他标准配置,你可以使用它作为起点:
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.
Then inside your main engine.rb you can access:
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/
我个人喜欢 Devise 使用的模式,它在引擎的模块中定义一个类方法,然后在父应用程序中定义一个初始化程序。
创建要从父应用程序调用的类方法
在父应用程序的初始化程序中调用类方法
现在,您可以在引擎的初始化过程中自由访问
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
Call the class method in an initializer in the parent application
Now you are free to access
your_config_var
in your engine's initialization process. You can create a file in your engine'sconfig/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.对我来说似乎更好如下:
Seems better to me as follow :