Sharing Rails Assets using Git Submodules
- A Rails engine is a separate project in your text-editor, switching between these projects can be annoying.
- You need a lot of discipline to commit changes on the Rails engine, as you are not being forced to do this in your normal commit flow.
- For just sharing some assets, a Rails engine seems like overkill.
We looked further and figured out that Git submodules solve many of these issues. Basically, you just create a git repository with the
stylesheets folders and put your shared files in place, like this:
The next step is to add your shared assets repository as a git submodule to both of your Rails projects.
git submodule add firstname.lastname@example.org:yourname-here/shared-assets.git app/assets/shared
Rails needs to know that it has to look for the shared assets folder. You can do this by extending the assets paths in the application.rb file.
Working with the shared assets
Your shared assets are now in the
app/assets/shared directory in your Rails application. Changes within the submodule are tracked by your favorite Git client (although we had some issues with Github’s Mac client).
Sourcetree with the shared assets submodule
Sourcetree initializes submodules automatically and pulls the submodule while you pull your project. You can do it yourself manually with the following Git commands:
git submodule update --init
git submodule foreach git pull origin master
Git submodules prove to be handy while developing Moneybird. Style changes for multiple applications are now applied within minutes, while keeping a comfortable workflow.