In the last 3 modules of the book, you have built a complete modular Rails application.
BlastCRM is only composed of three modules, but we hope you have learned a lot while building them. Since we built a “web” application, it should be available online, right? There are a few steps before we can actually push our application to staging/production. Before we do that, we will show you how to work with your modular application, and how to maintain it.
The first thing we will do in this module, is to learn how to work with a growing set of engines. We will learn how to deal with the source code, and how to manage our modules as gems. Finally, we will push BlastCRM to Heroku.
Until now, we’ve kept the source code of our modules inside the parent application. This wasn’t a problem while creating our first engines and we didn’t care about distribution. But now we want our modules to be easily re-used and shared between different applications. Let’s see how we can do this.
In Chapter cha:01_03_basics we explained that an engine is a “fat-free” Ruby on Rails application. However, the fact that it has a
gemspec means that we can package it as a gem! By encapsulating the source code of each of our modules inside a gem, it becomes extremely easy to share them!
To achieve this, we would first have to push all our packaged modules to a gem server, like RubyGems. Then, all we would have to do is put them inside an application’s
Gemfile, and run
bundle install. This would also mean that we need to version our engines (which we already have), and give each module a version like
1.0.0, and then update it to
1.0.1 when we change something, and so on, and so forth.
There are a lot of reasons for packaging your engines as gems. First of all, when building a modular application, you don’t actually have that many choices. To manage your module/engine source code, either you package it as a gem or you use
git subtree is a bit of a pain to work with, and we don’t recommend it; we found it quite unproductive and boring… Using gems, and simply changing a version number, is much easier, as we will soon see.
Here are the main reasons to encapsulate your engines as gems:
Let’s see how easy it is to make a gem by packaging one of our modules as a gem. Navigate inside the Tasks module folder and run the following command:
gem build blast_tasks.gemspec
This will generate a new file named
blast_tasks-0.0.1.gem. That’s it! We got ourselves a gem!
If you now want to make your gem public on RubyGems, and make it available to everyone, you can simply run:
gem push blast_tasks-0.0.1.gem
It is probably not going to work if someone else pushed a gem with the same name and version, so you should try with your own modules!
The version is still
0.0.1, as we didn’t give it a proper version yet. We will fix this in the next part after we’ve talked about how you should version your engines.