yasuoza diary

web and life

Sprockets with Padrino without plugin

One day I was searching to integrate sprockets to my padrino application, I found following article to make it. Sprockets & Bower in my Padrino please. In the article he explained to integrate sprockets to padrino using sprockets-helpers, through rack.

I was so excited because the content is what I wanted to do. But few hours after, I realized the way he explained works good for development and production environment, but not for test environment since in test environment, /assets is not mounted to padrino itself. So, testing with javascript like data-confirm testing will fail because javascript(and other assets too) is not loaded!

I began to search how to mount(or insert) sprockets middleware in padrino applicaiton again and found the following way to solve the issue.

The difference found way and previous article way is just how to insert sprockets middleware. So, Gemfile, config/sprockets.rb and Rakefile are same. app/app.rb differs little bit, and lib/sprockets_initializer.rb will be newly created.

lib/sprockets_initializer.rb is used to register to use Sprockets::Server for assets request handler.

lib/sprockets_initializer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module SprocketsInitializer
  def self.registered(app)
    # Make it lazy
    options[:asset_prefix] = -> { Sprockets::Helpers.prefix }

    app.use ::SprocketsInitializer::Rack, options
  end

  class Rack
    def initialize(app, options={})
      @app = app
      @asset_prefix = options[:asset_prefix].call
    end

    def call(env)
      if env['PATH_INFO'].match(@asset_prefix)
        env['PATH_INFO'].gsub!(@asset_prefix, '')
        return PadrinoSprocekts.environment.call(env)
      end

      @app.call(env)
    end
  end
end

In app/app.rb register it only for not production environment because in production environment, I want to let application read assets from compiled assets path.

app/app.rb
1
2
3
4
5
6
7
8
9
10
module Web
  class App < Padrino::Application
    register Padrino::Rendering
    register Padrino::Mailer
    register Padrino::Helpers
    register SprocketsInitializer unless Padrino.env == :production
    helpers Sprockets::Helpers

  end
end

If you want, delete mounted sprockets from config.ru

config.ru
1
2
3
require File.expand_path("../config/boot.rb", __FILE__)

run Padrino.application

After changed configuration like above, you can test your application previous style.

1
$ rake spec

Thank you for reading!

Comments