Use Padrino and sprockets with sprockets-helpers

March 30, 2013

Thanks to @arthur_chiu and his post on how to integrate sprockets in Padrino with sprockets-helpers, i’ve create my version :D:D:D:D 1) Create padrino-sprockets in config/sprockets.rb

module Padrino::Sprockets def self.registered(base) base.set :sprockets, ::Sprockets::Environment.new(Padrino.root)

# Add folder
base.sprockets.append_path 'assets/javascripts'
base.sprockets.append_path 'assets/stylesheets'
base.sprockets.append_path 'assets/images'
base.set :digest_assets, true

# compress file
if PADRINO_ENV == 'development'
  base.sprockets.js_compressor = Uglifier.new(mangle: true)
  base.sprockets.css_compressor = YUI::CssCompressor.new
end

# sprockets-helpers
base.helpers Sprockets::Helpers
# We can configure \`sprockets-helpers\` to find the correct assets for us.
Sprockets::Helpers.configure do |config|
  manifest_path      = File.join(Padrino.root,'public','assets','manifest.json')
  config.environment = base.sprockets
  config.prefix      = '/assets'
  config.manifest    = Sprockets::Manifest.new(base.sprockets, manifest_path)
  config.digest      = true
  config.public\_path = base.public\_folder
end

# call sprockets :D
if PADRINO_ENV == 'development'
  base.get '/assets/*splat' do
    env\['PATH_INFO'\].gsub!(%r{A/?assets}, '')
    settings.sprockets.call(env)
  end
end

end end

In this file you need add all your asset’s folder.. 2) Load this file at startup application: config/boot.rb

# after this line Bundler.require(:default, PADRINO_ENV)

# add this require Padrino.root(‘config/sprockets’)

3) register Padrino::Sprockets in your application

Module Foo class App < Padrino::Application register Padrino::Sprockets

end end

4) Use js,css sprockets version in you layout, es: layout.slim

link href= stylesheet_path(‘app’) media=“screen” type=“text/css” rel=“stylesheet”

instead

= stylesheet_link_tag :app

5) Use Rakefile to create asstes to pre-compilation :D:D

PADRINO_ENV = ENV[‘PADRINO_ENV’] ||= ENV[‘RACK_ENV’] ||= ‘development’ unless defined?(PADRINO_ENV)

require ‘bundler/setup’ require ‘padrino-core/cli/rake’

require ‘sprockets/../rake/sprocketstask’

Bundler.require(PADRINO_ENV) require File.expand_path(“../config/boot.rb”, __FILE__)

PadrinoTasks.use(:database) PadrinoTasks.use(:minirecord) PadrinoTasks.init

# Use fake application module FakeSprock class App < Padrino::Application register Padrino::Sprockets end end

SPROCKETS = FakeSprock::App.sprockets

# or your real app name # SPROCKETS = RealAppName::App.sprockets

Rake::SprocketsTask.new do |t| manifest_path = File.join(Padrino.root,‘public’,‘assets’) t.environment = SPROCKETS t.output = File.join(Padrino.root,‘public’,‘assets’) t.manifest = Sprockets::Manifest.new(SPROCKETS, manifest_path) t.assets = %w[app.js app.css *.png *.gif *.jpg] end

and call it with

rake assets

To conitnue use stylesheet_link_tag and javascript_include_tag, need to close this issue Thanks again at @padrino-core. Special tanks: DAddYE @arthur_chiu


comments powered by Disqus