How do I convert an existing Rails 3 Application into an Engine?

会有一股神秘感。 提交于 2019-12-02 17:35:36

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!