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 deployments so that remotely cached pages referencing the old compiled assets still work for the life of the cached page.

Live Compilation :

In some circumstances we may wish to use live compilation. In this mode all requests for assets in the pipeline are handled by Sprockets directly.

To enable this option set:

config.assets.compile = true

On the first request the assets are compiled and cached as outlined in development above, and the manifest names used in the helpers are altered to include the MD5 hash.

Sprockets also sets the Cache-Control HTTP header to max-age=31536000. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.

This mode uses more memory, performs more poorly than the default and is not recommended.

If you are deploying a production application to a system without any pre-existing JavaScript runtimes, you may want to add one to your Gemfile: group :production do

gem ‘therubyracer’

end

Railscarma has been implementing Ruby on Rails from its nascent stages for development, training, deploying and contributing back to the Rails Community and provide best Ruby on Rails Development Services. RailsCarma provide end to end Ruby on Rails services including, consulting, architecture, building, management and extension to companies around the globe. You can also hire Ruby on Rails developers with an easy to hire process.Contact Us to know more.

Read More : 

Get in touch with us.

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish