Sha256: b6b5177a8c57723f09a90d01ab8bb75d41831d0dad0e9574c82d20a504404df1

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

require "ember/rails/assets/javascript"

module Ember
  module Rails
    module Assets
      class Middleware < Struct.new(:app)
        def call env
          @status, @headers, @body = app.call(env)
          return [@status, @headers, @body] unless html?

          response = Rack::Response.new([], @status, @headers)
          @body.each do |fragment|
            response.write inject(fragment)
          end
          @body.close if @body.respond_to?(:close)

          response.headers.delete("Content-Length") # some downstream middleware will recalculate
          response.finish
        end

        private

        def html?
          @headers["Content-Type"] =~ /html/
        end

        def inject response
          key = "ASSETS"
          path = ActionView::Base.assets_manifest.path
          markup = Javascript.new(key, path).render
          response.gsub(%r{</head>}, "<script>#{markup}</script></head>")
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ember-rails-assets-0.2.3 lib/ember/rails/assets/middleware.rb
ember-rails-assets-0.2.2 lib/ember/rails/assets/middleware.rb