Sprockets integration into Padrino application
Posted: Apr 16, 2015
For one of my projects I decided to use Padrino instead of Rails. I worked with Sinatra, therefore I thought that Padrino would work for me as well. Actually, I guess even Sinatra matches my needs, but start of a project with Sinatra is slower, because you need to setup a lot of stuffs to start active development.
Anyway, for my application I needed Sprockets to get assets pipeline. May be I could use something else for that, but when you start a new project you have to pick up technologies which you know (or at least some of them). Padrino doesn’t provide a standard way to integrate Sprockets and it is perfectly fine. I tried a few gems mentioned here some of them partially worked and some of them didn’t work as I expected. Since Sprockets can be used with any Rack application I decided to integrate it without any gems.
What I did:
Create a new application to serve assets
Mount the new app
# config/apps.rb Padrino.mount( 'MyApp::Assets', app_file: Padrino.root('app/assets_app.rb') ).to('/assets')
Note: this code should go before mounting of your main app.
Create new helper methods for assets
Add Sprockets rake tasks to compile assets
To avoid duplications in appending paths to assets I moved that code to own class, actually that class finds asset paths in all gems and adds to sprockets.
Now in my layouts I use:
Everything works as I need. Since I don’t need big flexibility here, I am satisfied by this result without any additional gems.
I am ok to use additional gems for such kind of challenges, but usually, they aren’t maintainable enough. When you use barely maintainable gems, you may run into the issue with dependencies of that gems. They may depend on obsolete versions of gems and it means you won’t be able to upgrade some of your dependencies. Therefore, if you can do integration without any additional gems, it is better to go this way.
UPD: The demo code is here.