如何将现有的 Rails 3 应用程序转换为引擎? [英] How do I convert an existing Rails 3 Application into an Engine?

查看:58
本文介绍了如何将现有的 Rails 3 应用程序转换为引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我一直在开发的论坛应用程序转换成Rails引擎,以便它可以嵌入到其他应用程序中吗?

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

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

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:

  • 我应该为模型命名吗?也就是说,我应该将它们保存在我的引擎模块和 app/models/engine 文件夹中吗?
  • config 中应该保留哪些配置文件?
  • public 文件夹怎么样?在 Rails 3.1 中,样式表和 javascripts 被移到了 app/assets 文件夹,解决了这个问题,但是我如何在 Rails 3.0 中达到同样的效果?
  • 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?

推荐答案

这里的问题太多,无法全部正确回答.这是只需深入研究并尝试即可为您带来回报的事情之一.当你深入了解它时,回来提出新的具体问题.

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.

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

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.

您可能希望创建一个 generator 来创建一个包含您的引擎所需的所有表的迁移.可以创建其他生成器来覆盖默认视图等.

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.

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

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'

命名空间

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

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.

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

Definitely namespace any classes you create in the lib directory.

配置文件

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

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.

公共文件

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

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

这篇关于如何将现有的 Rails 3 应用程序转换为引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆