Wagons give you an easy way to create different extensions for your own Ruby on Rails applications. See how it’s done here.
Ruby on Rails has a fantastic ecosystem with thousands of plugins and helpful gems. There is even an easy way to extract partial applications into own Engines for further use in different applications. However, if your own application required some extensibility, your are left on your own. So far.
Say you have a large application (probably an open source product?), deployed on many different sites. While everybody loves your application, there are some requests for this special feature or that different behavior. You would like to keep your application focused but still make your various customers happy. Why no put the desired functionality into separate plugins, giving everybody exactly what they need?
Here be Wagons
Wagons are extensions to your application train running on Rails. You can see them as plugins that extend the behavior of your specific Rails application. The Wagons framework makes it easy to create and manage them.
First of all, wagons are basically Rails Engines. Wagons provide a handful of additions so your wagon engines actually know your application. They differ from engines in a few points:
- Wagons extend your application, engines extend Rails.
- Wagon migrations are kept separately from your application’s
migrations to enable easy addition and removal of wagons.
- When developing and testing, wagons use your main application instead
of a dummy application.
Once the gem is installed, create your first wagon with
rails generate wagon your_brand_new_wagon.
Extending your application
Now that you have your brand new wagon, how do you append it to your application train? The answer is easy: Ruby!
Adding completely new models, controllers and views is already covered by Rails Engines and is straight forward. Please have a look there for details. As usual, you put everything into the wagon’s app directory.
Extending existing models or controllers of your application is more interesting. Inside the wagon, create a module with the required functionality. ActiveSupport::Concern is your friend. This module should then be included into your application’s class. Do that in a config.to_prepare block in your_brand_new_wagon/lib/your_brand_new_wagon/wagon.rb. This is the file
where you can hook into your main application. If you are familiar with Rails Engines, you already guessed right: we inherit from Rails::Engine here.
View templates are a bit trickier to extend. It is possible to overwrite an entire template from your wagon. Simply use the same filename as in your main application. A basic view helper allows you to provide extension points inside your application templates. This can be used to display additional data or more menu entries for example. Put the helper method render_extensions(:your_key) in your template, and all partials
starting with _your_key*_ in the same subpath will be rendered at this place.
Last but not least, a few rake tasks support you in running migrations and even load seed data for whatever wagons should come with a certain deployment.
Wagons is open source software and waits for curious users and eager developers. Here at Puzzle, we used it in different projects. It works fine with a hundred different wagons as well as with only one or two. Give it a try today or fork it on Github.