如何将现有的 Rails 3 应用程序转换为引擎?

发布于 2024-11-16 19:34:04 字数 504 浏览 3 评论 0 原文

如何将我正在开发的 论坛应用程序 转换为 Rails 引擎,以便将其嵌入在其他应用程序中?

我应该添加、保留或删除什么?我应该提供一种集成模型的方法吗?如何设置路由和用户配置?如何将其打包成 Gem?我应该注意什么?


阅读文章和文档后,我设法缩小了问题范围:

  • 我应该为模型命名空间吗?也就是说,我应该将它们保存在引擎的模块和 app/models/engine 文件夹中吗?
  • 我应该保留 config 中的哪些配置文件?
  • public 文件夹怎么样?在 Rails 3.1 中,样式表和 javascript 被移至 app/assets 文件夹中,这解决了这个问题,但是如何在 Rails 3.0 中实现相同的效果呢?

How can I convert the Forum application I've been developing into a Rails Engine, so that it may be embedded inside other applications?

What should I add, keep, or remove? Should I offer a way to integrate the models? How do I set up routes and user configuration? How do I package it into a Gem? What should I watch out for?


After reading the articles and the documentation, I managed to narrow down my questions:

  • Should I namespace the models? That is, should I keep them in my Engine's module and in the app/models/engine folder?
  • What configuration files in config should I keep around?
  • What about the public folder? In Rails 3.1, stylesheets and javascripts were moved to the app/assets folder, which solved this problem, but how do I achieve the same effect in Rails 3.0?

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

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

发布评论

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

评论(1

后来的我们 2024-11-23 19:34:04

这里的问题太多,无法正确回答。这是只要深入挖掘并尝试就会为您带来回报的事情之一。当你更深入地了解它时,回来提出新的具体问题。

以下是我最近执行此操作时使用的一些资源。

大多数情况下,您可以保留这些东西在它们所在的应用程序目录中。您还应该能够将 routes.rb 保留在 config 目录中,但如果您的某些路由与应用程序的路由发生冲突,则可能会出现一些问题。

您可能需要创建一个生成器来创建包含引擎所需的所有表的迁移。可以创建其他生成器来覆盖默认视图之类的东西。

请创建一个使用您的 gem 的测试应用程序。您将遇到的许多问题是确保正确加载引擎的依赖项。在开发过程中,编辑测试应用程序的 Gemfile 以直接指向 gem 的源...类似这样:

gem 'my-forum', :path => '~/work/my-forum'

命名空间

您至少应该命名您的表/模型,这样您就不会'不会遇到命名冲突。查看您当前的论坛应用程序,我至少会在所有表格前加上“forum_”前缀。例如,使用您的引擎的人很可能会拥有名为 Category 的不同模型......因此 ForumCategory 将是更好的选择。

绝对命名您在 lib 目录中创建的任何类。

配置文件

您需要将 routes.rb 保留在 config 目录中。您可能还需要保留初始化程序。任何应用程序特定的东西都可能需要转移到其他地方。

公共文件

使用 Rails 3.0.x,您可以将样式表和 javascript 保存在公共目录中。我认为您需要添加一些代码到您的 Engine 类中......

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

Too many questions here to answer them all properly. This is one of those things that will pay off for you by just digging in and trying it out. As you get deeper into it, come back and ask new specific questions.

Here are some of the resources I used when I recently did this.

For the most part, you can keep the things in your app directory where they are. You should also be able to keep your routes.rb in the config directory, but there can be some gotchas if some of your routes collide with those of the app.

You will likely want to create a generator to create a migration that has all of the tables your engine requires. Other generators can be created to override default views and that sort of thing.

Do create a test application that uses your gem. Many of the issues you will run into are making sure you are loading your engine's dependencies properly. While you are in development, edit the Gemfile of your test application to point straight to the source of your gem... something like this:

gem 'my-forum', :path => '~/work/my-forum'

Namespacing

You should at least name your tables/models so you don't run into naming collisions. Looking at your current forum app, I'd at least prefix all of your tables with 'forum_'. It is quite likely that someone using your engine will have a different model named Category for example... so ForumCategory would be a better choice.

Definitely namespace any classes you create in the lib directory.

Config Files

You'll want to keep your routes.rb in the config directory. You may also need to keep your initializers around as well. Any app specific things will likely need to get moved elsewhere.

Public Files

With Rails 3.0.x, you can keep stylesheets and javascripts in the public directory. I think there is a bit of code you need to add to your Engine class though...

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文