public
Last active — forked from sgruhier/0_instructions.txt

Sprocket 2 in Rails 3.0.x ala Rails 3.1

  • Download Gist
0_instructions.txt
1 2 3 4 5 6 7 8 9 10
Some brief instructions on how to use Sprocket 2 in Rails to get CoffeeScript
powered JS and SASS powered CSS with YUI compression all via the magic of rack.
 
This stuff will be native in Rails 3.1 and the layout of the files on the
filesystem will be different but this guide will get you working with it
while we wait for all that to finalize.
 
Ignore the number prefixes on each file. This is just to ensure proper order in the Gist.
 
It's based on eric1234 gist https://gist.github.com/911003. ijust made it 3.1 compliant in terms of convention
1_Gemfile.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
gem 'coffee-script'
gem 'yui-compressor', :require => 'yui/compressor'
gem 'sass'
gem 'json' # sprocket dependency for Ruby 1.8 only
gem 'sprockets', '2.0.0.beta5'#, :git => 'git://github.com/sstephenson/sprockets.git'
 
# Sprockets needs to build JS file. On mac it uses apple java compiler, on production server (linux for example) you need to
# install this gem
group :production do
gem "therubyracer"
end
2_asset_server.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Config a Sprockets::Environment to mount as a Rack end-point. I like to use a subclass
# as it allows the config to be easily reusable. Since I use the same instance for
# all mount points I make it a singleton class. I just add this as an initializer to my
# project since it is really just configuration.
class AssetServer < Sprockets::Environment
include Singleton
 
def initialize
super Rails.root.join('app', 'assets')
paths << 'javascripts' << 'stylesheets'
if Rails.env.production?
self.js_compressor = YUI::JavaScriptCompressor.new :munge => true, :optimize => true
self.css_compressor = YUI::CssCompressor.new
end
end
 
end
3_routes.rb
Ruby
1 2 3 4 5
# Mount the rack end-point for JavaScript and CSS.
MyApp::Application.routes.draw do
# Add this line
mount AssetServer.instance => '/assets'
end
4_application.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Put this in your assets/javascripts/application.js directory and call /assets/application.js in your browser
# You need to have jquery and jquery_ujs files (jquery_ujs is sometimes named rails.js)
#
# In your layout just add
#
# <%= javascript_include_tag "/assets/application" %>
#
# In Rails 3.1 you could even do
#
# <%= javascript_include_tag "application" %>
#
# THEN REMOVE THOSE COMMENTS
 
//= require jquery
//= require jquery_ujs
//= require_tree .
5_your_file.js.coffee
CoffeeScript
1 2
# Put this in your app/assets/javascripts directory, it will be included when you call /assets/application.js in your browser
alert 'hello world'
7_application.css
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Put this in your app/assets/stylesheets directory and call /assets/application.css in your browser
// In your layout just add
//
// <%= stylesheet_link_tag "/assets/application" %>
//
// In Rails 3.1 you could even do
//
// <%= stylesheet_link_tag "application" %>
//
// THEN REMOVE THOSE COMMENTS
 
/*
*= require_tree .
*/
7_your_file.css.scss
Sass
1 2
// Put this in your app/assets/stylesheets directory and call /assets/application.css in your browser
body {margin: 2px + 5px}
8_conclusion.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Sprockets 2 has a lot more under the hood but this gets you started.
A few things not covered:
 
1. Anything supported by Tilt can be used as a template engine
(not just Sass and CoffeeScript).
2. Although Sass has native abilities to include other files, Sprockets 2
gives the ability to all formats through special comments like:
// =require "foo"
It's special commands can be fairly powerful (like requiring an entire
directory or tree). NOTE: Use the comment character relevant for the
language. So coffescript should be:
# =require 'foo.js'
Then you can create 'foo.js.coffee' and when served it will be as one
file.
3. Sprockets 2 has the ability to pre-compile the assets for maximum speed.
Also useful when the deployment environment doesn't support a template
language (like CoffeeScript).

It doesn't work :'( please can you give some details abouts the route set ? and explain in deep the part 4 and 7

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.