Understanding Asset Pipeline Plugin

Ruby on rails or simply Rails is a wonderful web application framework, written in Ruby. It provides services for a web page designing. It has won tech geeks votes for its faster services in terms of developing a website because of its user-friendly features, reduced complexity, and thus improving its efficiency allover. One such feature for RoR is The Asset Pipeline.

The asset pipeline is a very powerful feature that Rails offers, to solve a wide range of problems related to web designing.

It minimizes the manual works by compiling and processing all the stylesheets, javascript files, images etc whenever possible and is prepares for the use. Asset pipeline can improve the quality of the application in terms of its performance and flexibility.

The Asset Pipeline :

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB.

The asset pipeline is technically no longer a core feature of Rails 4, it has been extracted out of the framework into the sprockets-rails gem.

The asset pipeline is enabled by default.

We can disable the asset pipeline while creating a new application by passing the –skip-sprockets option.

rails new appname –skip-sprockets

Rails 4 automatically adds the sass-rails, coffee-rails and uglifier gems to your Gemfile, which are used by Sprockets for asset compression:

gem ‘sass-rails’

gem ‘uglifier’

gem ‘coffee-rails’

Using the –skip-sprockets option will prevent Rails 4 from adding sass-rails and uglifier to Gemfile, so if you later want to enable the asset pipeline you will have to add those gems to your Gemfile. Also, creating an application with the –skip-sprockets option will generate a slightly different config/application.rb file, with a require statement for the sprockets railtie that is commented-out. You will have to remove the comment operator on that line to later enable the asset pipeline:

# require “sprockets/railtie”

To set asset compression methods, set the appropriate configuration options in production.rb – config.assets.css_compressor for your CSS and config.assets.js_compressor for your JavaScript:

config.assets.css_compressor = :yui

config.assets.js_compressor = :uglifier

How to Use the Asset Pipeline :

In previous versions of Rails, all assets were located in subdirectories of public such as images, javascripts and stylesheets. With the asset pipeline, the preferred location for these assets is now the app/assets directory. Files in this directory are served by the Sprockets middleware.

Assets can still be placed in the public hierarchy. Any assets under public will be served as static files by the application or web server when config.serve_static_files is set to true. You should use app/assets for files that must undergo some pre-processing before they are served.

In production, Rails precompiles these files to public/assets by default. The precompiled copies are then served as static assets by the web server. The files in app/assets are never served directly in production.

Controller Specific Assets :

When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the coffee-rails gem is in the Gemfile) and a Cascading Style Sheet file (or SCSS file if sass-rails is in the Gemfile) for that controller. Additionally, when generating a scaffold, Rails generates the file scaffolds.css (or scaffolds.css.scss if sass-rails is in the Gemfile.)

For example, if you generate a ProjectsController, Rails will also add a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss. By default these files will be ready to use by your application immediately using the require_tree directive. See Manifest Files and Directives for more details on require_tree.

You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:

<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>

Precompiling Assets :

Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline.

Compiled assets are written to the location specified in config.assets.prefix. By default, this is the /assets directory.

You can call this task on the server during deployment to create compiled versions of your assets directly on the server. See the next section for information on compiling locally.

The rake task is:

$ RAILS_ENV=production bin/rake assets:precompile

Capistrano (v2.15.1 and above) includes a recipe to handle this in deployment. Add the following line to Capfile:

load ‘deploy/assets’

This links the folder specified in config.assets.prefix to shared/assets. If you already use this shared folder you’ll need to write your own deployment task.

It is important that this folder is shared between