有人能帮我理解一下Rails引擎和可安装的应用程序之间的区别吗?在Rails3.1中,您可以使用"rails new plugin ___“”命令创建任何一个插件。
rails plugin new forum --full # Engine
rails plugin new forum --mountable # Mountable App
什么时候你想使用一种而不是另一种?例如,我知道你可以把引擎包装成宝石。这不是可安装的应用程序的情况吗?还有什么不同之处?
发布于 2011-07-27 00:22:15
我注意到以下几点:
全引擎
使用完整的引擎时,父应用程序会从引擎继承路由。没有必要在parent_app/config/routes.rb
中指定任何内容。在Gemfile中指定gem就足以让父应用程序继承模型、路由等。引擎路由指定为:
# my_engine/config/routes.rb
Rails.application.routes.draw do
# whatever
end
没有模型、控制器等的命名空间。父应用程序可以立即访问这些。
可安装引擎
默认情况下,引擎的命名空间是隔离的:
# my_engine/lib/my_engine/engine.rb
module MyEngine
class Engine < Rails::Engine
isolate_namespace MyEngine
end
end
使用可挂载的引擎,路由是命名空间的,父应用程序可以将此功能捆绑在单个路由下:
# my_engine/config/routes.rb
MyEngine::Engine.routes.draw do
#whatever
end
# parent_app/config/routes.rb
ParentApp::Application.routes.draw do
mount MyEngine::Engine => "/engine", :as => "namespaced"
end
模型、控制器等与父应用程序隔离--尽管帮助程序可以很容易地共享。
这些是我发现的主要区别。也许还有其他的?我已经在here上询问过了,但还没有收到回复。
我的印象是,由于完整的引擎不会将自己与父应用程序隔离开来,因此最好将其用作与父应用程序相邻的独立应用程序。我相信名称冲突可能会发生。
在您希望避免名称冲突并将引擎捆绑在父应用程序中的一个特定路由下的情况下,可以使用可安装的引擎。例如,我正在构建我的第一个为客户服务而设计的引擎。父应用程序可以将其功能捆绑在单个路由下,例如:
mount Cornerstone::Engine => "/cornerstone", :as => "help"
如果我偏离了我的假设,请有人告诉我,我会解决这个问题。我写了一篇关于here干杯主题的小文章!
发布于 2013-06-24 01:34:43
这两个选项都将生成engine。不同之处在于,--mountable
将在一个独立的名称空间中创建引擎,而--full
将创建一个共享主应用程序名称空间的引擎。
这些差异将通过三种方式表现出来:
1)引擎类文件将调用 isolate_namespace
**:**
lib/my_full_engine/engine.rb:
module MyFullEngine
class Engine < Rails::Engine
end
end
lib/my_mountable_engine/engine.rb:
module MyMountableEngine
class Engine < Rails::Engine
isolate_namespace MyMountableEngine # --mountable option inserted this line
end
end
2)引擎的 config/routes.rb
文件将命名为:
全引擎:
Rails.application.routes.draw do
end
挂载引擎:
MyMountableEngine::Engine.routes.draw do
end
3)控制器、帮助器、视图和资产的文件结构将被命名为:
创建app/controllers/my_mountable_engine/application_controller.rb
创建app/helpers/my_mountable_engine/application_helper.rb
创建应用程序/邮件程序创建应用程序/模型
创建app/views/layouts/my_mountable_engine/application.html.erb
创建app/assets/images/my_mountable_engine
创建app/assets/stylesheets/my_mountable_engine/application.css
创建app/assets/javascripts/my_mountable_engine/application.js
create config/routes.rb create lib/my_my_Engine.rb
创建lib/task/my_my_Engine.rake
创建lib/my_my_engine/version.rb
创建lib/my_my_engine/Engine.rb
解释
--full
选项的用例似乎非常有限。就我个人而言,我想不出任何好的理由,为什么你想要将代码分离到一个引擎中,而不是隔离名称空间-它本质上只会给你提供两个紧密耦合的应用程序,共享相同的文件结构,以及由此带来的所有冲突和代码泄漏。
我看到的每一篇文档都演示了--mountable
选项,实际上,当前的edge guide强烈鼓励您包含isolate namespace
-这等同于说使用--mountable
而不是--full
。
最后是术语混淆:不幸的是,rails plugin -h
显示了以下描述:
--full #使用捆绑的Rails应用程序生成用于测试的rails引擎
--mountable #生成可挂载的隔离应用程序
这给人的印象是,您使用--full
创建“引擎”,使用--mountable
创建其他称为“可挂载的应用程序”的东西,而实际上它们都是引擎-一个名称空间,一个没有。这必然会导致混淆,因为希望创建引擎的用户可能会认为--full
是更相关的选项。
结论
rails plugin new something --full
=您的应用程序名称空间中的引擎。(为什么you?)rails plugin new something --mountable
= Engine会有自己的命名空间。(太棒了)参考文献
发布于 2012-09-25 15:57:21
我也是这样想的,所以最终来到了这里。在我看来,前面的答案基本上涵盖了这个问题,但我认为下面的答案可能也会有所帮助:
# generate plugins (NOTE: using same name each time to minimize differences)
# -----------------------------------------------------------------------------
$ rails plugin new test-plugin -T
$ mv test-plugin{,.01}
$ rails plugin new test-plugin -T --mountable
$ mv test-plugin{,.02}
$ rails plugin new test-plugin -T --full
$ mv test-plugin{,.03}
$ rails plugin new test-plugin -T --full --mountable
$ mv test-plugin{,.04}
# compare "stock" (01) with "mountable" (02)
# -----------------------------------------------------------------------------
$ diff -r test-plugin.01 test-plugin.02
Only in test-plugin.02: app
Only in test-plugin.02: config
Only in test-plugin.02/lib/test-plugin: engine.rb
diff -r test-plugin.01/lib/test-plugin.rb test-plugin.02/lib/test-plugin.rb
0a1,2
> require "test-plugin/engine"
>
Only in test-plugin.02: script
diff -r test-plugin.01/test-plugin.gemspec test-plugin.02/test-plugin.gemspec
18a19
> # s.add_dependency "jquery-rails"
# compare "stock" (01) with "full" (03)
# -----------------------------------------------------------------------------
$ diff -r test-plugin.01 test-plugin.03
Only in test-plugin.03: app
Only in test-plugin.03: config
Only in test-plugin.03/lib/test-plugin: engine.rb
diff -r test-plugin.01/lib/test-plugin.rb test-plugin.03/lib/test-plugin.rb
0a1,2
> require "test-plugin/engine"
>
Only in test-plugin.03: script
diff -r test-plugin.01/test-plugin.gemspec test-plugin.03/test-plugin.gemspec
18a19
> # s.add_dependency "jquery-rails"
# compare "mountable" (02) with "full" (03)
# -----------------------------------------------------------------------------
$ diff -r test-plugin.02 test-plugin.03
Only in test-plugin.03/app/assets/javascripts/test-plugin: .gitkeep
Only in test-plugin.02/app/assets/javascripts/test-plugin: application.js
Only in test-plugin.03/app/assets/stylesheets/test-plugin: .gitkeep
Only in test-plugin.02/app/assets/stylesheets/test-plugin: application.css
Only in test-plugin.03/app/controllers: .gitkeep
Only in test-plugin.02/app/controllers: test-plugin
Only in test-plugin.03/app/helpers: .gitkeep
Only in test-plugin.02/app/helpers: test-plugin
Only in test-plugin.03/app/mailers: .gitkeep
Only in test-plugin.03/app/models: .gitkeep
Only in test-plugin.03/app/views: .gitkeep
Only in test-plugin.02/app/views: layouts
diff -r test-plugin.02/config/routes.rb test-plugin.03/config/routes.rb
1c1
< TestPlugin::Engine.routes.draw do
---
> Rails.application.routes.draw do
diff -r test-plugin.02/lib/test-plugin/engine.rb test-plugin.03/lib/test-plugin/engine.rb
3d2
< isolate_namespace TestPlugin
# compare "mountable" (02) with "full & mountable" (04)
# -----------------------------------------------------------------------------
$ diff -r test-plugin.02 test-plugin.04
<no difference>
# compare "full" (03) with "full & mountable" (04)
# -----------------------------------------------------------------------------
$ diff -r test-plugin.03 test-plugin.04
Only in test-plugin.03/app/assets/javascripts/test-plugin: .gitkeep
Only in test-plugin.04/app/assets/javascripts/test-plugin: application.js
Only in test-plugin.03/app/assets/stylesheets/test-plugin: .gitkeep
Only in test-plugin.04/app/assets/stylesheets/test-plugin: application.css
Only in test-plugin.03/app/controllers: .gitkeep
Only in test-plugin.04/app/controllers: test-plugin
Only in test-plugin.03/app/helpers: .gitkeep
Only in test-plugin.04/app/helpers: test-plugin
Only in test-plugin.03/app/mailers: .gitkeep
Only in test-plugin.03/app/models: .gitkeep
Only in test-plugin.03/app/views: .gitkeep
Only in test-plugin.04/app/views: layouts
diff -r test-plugin.03/config/routes.rb test-plugin.04/config/routes.rb
1c1
< Rails.application.routes.draw do
---
> TestPlugin::Engine.routes.draw do
diff -r test-plugin.03/lib/test-plugin/engine.rb test-plugin.04/lib/test-plugin/engine.rb
2a3
> isolate_namespace TestPlugin
(对我来说)特别有趣的是,这两者之间没有区别
rails plugin new test-plugin -T --mountable
和
rails plugin new test-plugin -T --full --mountable
https://stackoverflow.com/questions/6118905
复制相似问题