SlideShare a Scribd company logo
Rails 3.1
Asset Pipeline

    by Eric Allam
a               presentation
Table of Contents
01   The pain of assets in 3.0
02   Asset Pipeline to the rescue
03   New Stuff
04   Deployment story




Asset Pipeline                      !"#$%&'(&)*+%,
Bad Organization

                                                                  stylesheets



       ROOT/public                                                javascripts



                                                                     images



   Asset Pipeline                                                                     !"#$%&'(&)*+%,



One of the big pains in how Rails pre 3.1 handles assets is organizational. You have one
place to put all your assets, one place to put stylesheets, one place to put javascripts, and
one place to put images. But what ends up happening?
application code




                                 third party code




   Asset Pipeline                                   !"#$%&'(&)*+%,



Big Ass Folders
Mixing 3rd party code
How do you update?
Mixing abstractions
be like extracting rails into your project
lowercase d dependencies
  ‣ application.html.erb

      <%= stylesheet_link_tag 'default',
        'index' %>
    <%= javascript_include_tag 'jquery',
        'rails', 'application' %>




   Asset Pipeline                          !"#$%&'(&)*+%,



ad hoc dependency declaration
Table of Contents
01 The pain of assets in 3.0

02 Asset Pipeline to the rescue

03   New Stuff
04   Deployment story




Asset Pipeline                    !"#$%&'(&)*+%,
Assets as First Class Citizens
 ‣ Better Opinions
 ‣ Empty Files and Folders
 ‣ Generators
 ‣ Capital-D Dependencies




Asset Pipeline                   !"#$%&'(&)*+%,
Better Opinions

          app/assets                                              stylesheets



            lib/assets                                             javascripts



      vendor/assets                                                  images



   Asset Pipeline                                                                !"#$%&'(&)*+%,



Now assets are in 3 places.

app/assets holds the code you write for this specific application

lib/assets holds the generic library code you write for this application

vendor/assets holds third party code
app/assets




          localhost:3000/assets/application.js


   Asset Pipeline                                                                    !"#$%&'(&)*+%,



Now, when you generate a new rails 3.1 app, you get two assets, application.js and
application.css in app/assets.

And now that 3.1 ships with jQuery by default, where is that?
lib/assets




    localhost:3000/assets/nagivation.js


Asset Pipeline                            !"#$%&'(&)*+%,
vendor/assets




        localhost:3000/assets/ie6_screen.css


   Asset Pipeline                              !"#$%&'(&)*+%,



3rd party
Capital-D Dependencies
   ‣ app/assets/javascripts/application.js

        //= require jquery
        //= require jquery_ujs



                                                     sprockets



   Asset Pipeline                                                   !"#$%&'(&)*+%,



looks like ruby require

require inlines the code when application.js is served, in order.

But where are these files?
Capital-D Dependencies
   ‣ Gemfile

         gem 'jquery-rails'

   https://github.com/rails/jquery-rails
                                                           //= require jquery
                                                           //= require jquery_ujs




   Asset Pipeline                                                                  !"#$%&'(&)*+%,



So we’ve declared our dependency on jquery and jquery_ujs in our application.js file, but
where are these files?

Well, now gems can have their own assets, and rails 3.1 ships with this jquery-rails gem in
your Gemfile, which provides jquery and jquery_ujs files necessary to work with the
framework.

Notice the familiar layout for assets, vendor/assets/javascripts.
Don’t like jQuery?
   ‣ Gemfile

       # gem 'jquery-rails'
     gem 'prototype'

   ‣ app/assets/javascripts/application.js

     //= require prototype




   Asset Pipeline                                                                       !"#$%&'(&)*+%,



Now its also very easy to switch back to prototype, or some other javascript library.
Including assets
   ‣ application.html.erb

       <%= stylesheet_link_tag 'application' %>
     <%= javascript_include_tag 'application' %>



     (function( window, undefined ) {
       // all of jquery here
     })( jQuery );

     (function() {
       // application code here
     }).call(this);




   Asset Pipeline                                                                !"#$%&'(&)*+%,



In your application.html.erb, you would define which stylesheets and javascripts you would
need.
Generators
     ‣ rails generate assets Post




     ‣ rails generate scaffold User




   Asset Pipeline                                                                     !"#$%&'(&)*+%,



Another way rails 3.1 makes assets into a first class citizen is through generators.

So now that we have a place to put resource specific asset code, how do we include these in
our app?
Dependency Directives
   ‣ app/assets/javascripts/application.js

     //= require jquery
     //= require jquery_ujs
     //= require_tree .


             require app/assets/javascripts/**/*


   Asset Pipeline                                                            !"#$%&'(&)*+%,



require_tree will require all files in javascripts, and any nested folders.
More Directives
     ‣ require
     ‣ require_tree .
     ‣ require_self
     ‣ require_directory ‘posts’
     ‣ include ‘file’
     ‣ depend_on ‘file.png’



   Asset Pipeline                                                                       !"#$%&'(&)*+%,



require_self takes the code from the current file and inlines it in place, helps when requiring
dependencies after require_self that depend on the current file

require_directory is similar to require_tree, but it wont traverse nested directories

include works like require, but will always insert the content even if its been required already

depend_on will declare a dependency that doesn’t get required, but is used for caching
purposes * will go into caching stuff more later
Table of Contents
01 The pain of assets in 3.0
02 Asset Pipeline to the rescue

03 New Stuff

04 Deployment story




Asset Pipeline                    !"#$%&'(&)*+%,
CoffeeScript and Sass
   ‣ Gemfile

     group :assets do
       gem 'sass-rails'
       gem 'coffee-script'
     end


                                            Default in 3.1



   Asset Pipeline                                                                 !"#$%&'(&)*+%,



The asset group is required by default in development and test environments, but not in
production.
CoffeeScript and Sass
   ‣ app/assets/javascripts/application.js.coffee
        jQuery ($) ->

        $('.truncate-more-link').live 'click', (e) ->
          $this = $ this
          # do more stuff here


   ‣ app/assets/stylesheets/application.css.scss

     $blue: #3bbfce;
     $margin: 16px;

     .content-navigation {
       border-color: $blue;
       color: darken($blue, 9%);
     }




   Asset Pipeline                                                                   !"#$%&'(&)*+%,



Before they are served to the browser, the asset pipeline passes these files through their
respective preprocessors.
Generators
     ‣ rails generate assets Review




     ‣ rails generate scaffold Comment




   Asset Pipeline                                                                  !"#$%&'(&)*+%,



Now generators work with coffeescript and sass and generate the appropriate files
Chaining Preprocessors
   ‣ app/assets/stylesheets/application.css.scss.erb
     $blue: #3bbfce
     $margin: 16px

     .content-navigation
       background: url(<%= asset_path(‘background.png’) %>)
       color: darken($blue, 9%)




     $blue: #3bbfce
     $margin: 16px

     .content-navigation
       background: url(/assets/background-374e12d5b46ffa3d2f92a11dbae9b06a.png)
       color: darken($blue, 9%)




   Asset Pipeline                                                                   !"#$%&'(&)*+%,



Before they are served to the browser, the asset pipeline passes these files through their
respective preprocessors.
Preprocessors




                 https://github.com/rtomayko/tilt


Asset Pipeline                                      !"#$%&'(&)*+%,
Table of Contents
01 The pain of assets in 3.0
02 Asset Pipeline to the rescue
03 New Stuff
04 Deployment story




Asset Pipeline                    !"#$%&'(&)*+%,
rake assets:precompile
   ‣ app/assets/stylesheets/application.css.scss.erb
      $blue: #3bbfce

      .content-navigation
        background: url(<%= asset_path(‘background.png’) %>)
        color: darken($blue, 9%)



  ‣   public/assets/application-e317be572968fcf8892b5c767f776d82.css

      .content-navigation {
        background: url(/assets/background-92a96fe5e422523a2de56c9f0defe51b.png);
        color: #2ca2af;
      }




   Asset Pipeline                                                                  !"#$%&'(&)*+%,



All assets get compiled into public/assets, where they can easily be served by your webserver
or a CDN.
Precompile
‣ config/deploy.rb
   before :"deploy:symlink", :"deploy:assets";

 desc "Compile assets"
 task :assets do
   run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec
 rake assets:precompile"
 end




Asset Pipeline                                            !"#$%&'(&)*+%,
Caching

      <script src="/javascripts/application.js?1292365692">
      </script>




      <script src="/assets/
      application-374e12d5b46ffa3d2f92a11dbae9b06a.js">
      </script>




                                                      depend_on

   Asset Pipeline                                                                    !"#$%&'(&)*+%,



Rails used to append a deploy timestamp to all asset paths to reset the cache.

With the asset pipeline, there is no deploy timestamp. Instead, rails will include an MD5 hash
of the contents of the file in the filename itself.

So now the cache stays fresh until the file actually changes, so if you are deploying a lot, but
not changing the asset files, then you should see some nice performance improvements.
Compression
   ‣ config/environments/production.rb

     # Compress both stylesheets and JavaScripts
     config.assets.compress = true




     gem 'uglifier'                               requires js runtime




   Asset Pipeline                                                                 !"#$%&'(&)*+%,



Both Mac OS X and Windows ship with javascript runtimes

If you are deploying to heroku you can use therubyracer-heroku, which includes a build of
nodejs specifically for heroku, but it will bloat your heroku slug size by 30MB
Rails 3.1 Asset Pipeline
Any questions?




Asset Pipeline             !"#$%&'(&)*+%,

More Related Content

What's hot

Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Dethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.jsDethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.js
Jay Harris
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.js
codeofficer
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
Javier Eguiluz
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdevFrank Rousseau
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin development
Caldera Labs
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
Mark Menard
 
Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
Wen-Tien Chang
 
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
Amazon Web Services
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
Ignacio Martín
 
api-platform: the ultimate API platform
api-platform: the ultimate API platformapi-platform: the ultimate API platform
api-platform: the ultimate API platform
Stefan Adolf
 
Refactor Dance - Puppet Labs 'Best Practices'
Refactor Dance - Puppet Labs 'Best Practices'Refactor Dance - Puppet Labs 'Best Practices'
Refactor Dance - Puppet Labs 'Best Practices'
Gary Larizza
 
Amazon Web Services for PHP Developers
Amazon Web Services for PHP DevelopersAmazon Web Services for PHP Developers
Amazon Web Services for PHP Developers
Jeremy Lindblom
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to Services
Craig Kerstiens
 
WebGUI Developers Workshop
WebGUI Developers WorkshopWebGUI Developers Workshop
WebGUI Developers Workshop
Plain Black Corporation
 
Ember - introduction
Ember - introductionEmber - introduction
Ember - introduction
Harikrishnan C
 
AWS Elastic Beanstalk
AWS Elastic BeanstalkAWS Elastic Beanstalk
AWS Elastic Beanstalk
Amazon Web Services
 

What's hot (20)

Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Dethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.jsDethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.js
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.js
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
Curso rails
Curso railsCurso rails
Curso rails
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin development
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
 
Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
 
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
Becoming a Command Line Expert with the AWS CLI (TLS304) | AWS re:Invent 2013
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
api-platform: the ultimate API platform
api-platform: the ultimate API platformapi-platform: the ultimate API platform
api-platform: the ultimate API platform
 
Refactor Dance - Puppet Labs 'Best Practices'
Refactor Dance - Puppet Labs 'Best Practices'Refactor Dance - Puppet Labs 'Best Practices'
Refactor Dance - Puppet Labs 'Best Practices'
 
Amazon Web Services for PHP Developers
Amazon Web Services for PHP DevelopersAmazon Web Services for PHP Developers
Amazon Web Services for PHP Developers
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to Services
 
WebGUI Developers Workshop
WebGUI Developers WorkshopWebGUI Developers Workshop
WebGUI Developers Workshop
 
Ember - introduction
Ember - introductionEmber - introduction
Ember - introduction
 
AWS Elastic Beanstalk
AWS Elastic BeanstalkAWS Elastic Beanstalk
AWS Elastic Beanstalk
 
termUserGroups
termUserGroupstermUserGroups
termUserGroups
 

Similar to Rails 3.1 Asset Pipeline

RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
Rory Gianni
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
Rory Gianni
 
Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
akankshita satapathy
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
Alive Kuo
 
Integrating Browserify with Sprockets
Integrating Browserify with SprocketsIntegrating Browserify with Sprockets
Integrating Browserify with SprocketsSpike Brehm
 
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fabio Akita
 
Web Frontend development: tools and good practices to (re)organize the chaos
Web Frontend development: tools and good practices to (re)organize the chaosWeb Frontend development: tools and good practices to (re)organize the chaos
Web Frontend development: tools and good practices to (re)organize the chaos
Matteo Papadopoulos
 
Sprockets
SprocketsSprockets
Understanding asset pipeline plugin
Understanding asset pipeline pluginUnderstanding asset pipeline plugin
Understanding asset pipeline plugin
RailsCarma
 
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyConsegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Fabio Akita
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on Rails
RORLAB
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
Daniel Cukier
 
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails Devs
Diacode
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
bensmithett
 
Curing Webpack Cancer
Curing Webpack CancerCuring Webpack Cancer
Curing Webpack Cancer
Neel Shah
 
FOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDBFOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDB
ArangoDB Database
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalystsvilen.ivanov
 

Similar to Rails 3.1 Asset Pipeline (20)

RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
Integrating Browserify with Sprockets
Integrating Browserify with SprocketsIntegrating Browserify with Sprockets
Integrating Browserify with Sprockets
 
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
 
Web Frontend development: tools and good practices to (re)organize the chaos
Web Frontend development: tools and good practices to (re)organize the chaosWeb Frontend development: tools and good practices to (re)organize the chaos
Web Frontend development: tools and good practices to (re)organize the chaos
 
Sprockets
SprocketsSprockets
Sprockets
 
Understanding asset pipeline plugin
Understanding asset pipeline pluginUnderstanding asset pipeline plugin
Understanding asset pipeline plugin
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyConsegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on Rails
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails Devs
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
Curing Webpack Cancer
Curing Webpack CancerCuring Webpack Cancer
Curing Webpack Cancer
 
FOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDBFOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDB
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalyst
 

Recently uploaded

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

Rails 3.1 Asset Pipeline

  • 1. Rails 3.1 Asset Pipeline by Eric Allam a presentation
  • 2. Table of Contents 01 The pain of assets in 3.0 02 Asset Pipeline to the rescue 03 New Stuff 04 Deployment story Asset Pipeline !"#$%&'(&)*+%,
  • 3. Bad Organization stylesheets ROOT/public javascripts images Asset Pipeline !"#$%&'(&)*+%, One of the big pains in how Rails pre 3.1 handles assets is organizational. You have one place to put all your assets, one place to put stylesheets, one place to put javascripts, and one place to put images. But what ends up happening?
  • 4. application code third party code Asset Pipeline !"#$%&'(&)*+%, Big Ass Folders Mixing 3rd party code How do you update? Mixing abstractions be like extracting rails into your project
  • 5. lowercase d dependencies ‣ application.html.erb <%= stylesheet_link_tag 'default', 'index' %> <%= javascript_include_tag 'jquery', 'rails', 'application' %> Asset Pipeline !"#$%&'(&)*+%, ad hoc dependency declaration
  • 6. Table of Contents 01 The pain of assets in 3.0 02 Asset Pipeline to the rescue 03 New Stuff 04 Deployment story Asset Pipeline !"#$%&'(&)*+%,
  • 7. Assets as First Class Citizens ‣ Better Opinions ‣ Empty Files and Folders ‣ Generators ‣ Capital-D Dependencies Asset Pipeline !"#$%&'(&)*+%,
  • 8. Better Opinions app/assets stylesheets lib/assets javascripts vendor/assets images Asset Pipeline !"#$%&'(&)*+%, Now assets are in 3 places. app/assets holds the code you write for this specific application lib/assets holds the generic library code you write for this application vendor/assets holds third party code
  • 9. app/assets localhost:3000/assets/application.js Asset Pipeline !"#$%&'(&)*+%, Now, when you generate a new rails 3.1 app, you get two assets, application.js and application.css in app/assets. And now that 3.1 ships with jQuery by default, where is that?
  • 10. lib/assets localhost:3000/assets/nagivation.js Asset Pipeline !"#$%&'(&)*+%,
  • 11. vendor/assets localhost:3000/assets/ie6_screen.css Asset Pipeline !"#$%&'(&)*+%, 3rd party
  • 12. Capital-D Dependencies ‣ app/assets/javascripts/application.js //= require jquery //= require jquery_ujs sprockets Asset Pipeline !"#$%&'(&)*+%, looks like ruby require require inlines the code when application.js is served, in order. But where are these files?
  • 13. Capital-D Dependencies ‣ Gemfile gem 'jquery-rails' https://github.com/rails/jquery-rails //= require jquery //= require jquery_ujs Asset Pipeline !"#$%&'(&)*+%, So we’ve declared our dependency on jquery and jquery_ujs in our application.js file, but where are these files? Well, now gems can have their own assets, and rails 3.1 ships with this jquery-rails gem in your Gemfile, which provides jquery and jquery_ujs files necessary to work with the framework. Notice the familiar layout for assets, vendor/assets/javascripts.
  • 14. Don’t like jQuery? ‣ Gemfile # gem 'jquery-rails' gem 'prototype' ‣ app/assets/javascripts/application.js //= require prototype Asset Pipeline !"#$%&'(&)*+%, Now its also very easy to switch back to prototype, or some other javascript library.
  • 15. Including assets ‣ application.html.erb <%= stylesheet_link_tag 'application' %> <%= javascript_include_tag 'application' %> (function( window, undefined ) { // all of jquery here })( jQuery ); (function() { // application code here }).call(this); Asset Pipeline !"#$%&'(&)*+%, In your application.html.erb, you would define which stylesheets and javascripts you would need.
  • 16. Generators ‣ rails generate assets Post ‣ rails generate scaffold User Asset Pipeline !"#$%&'(&)*+%, Another way rails 3.1 makes assets into a first class citizen is through generators. So now that we have a place to put resource specific asset code, how do we include these in our app?
  • 17. Dependency Directives ‣ app/assets/javascripts/application.js //= require jquery //= require jquery_ujs //= require_tree . require app/assets/javascripts/**/* Asset Pipeline !"#$%&'(&)*+%, require_tree will require all files in javascripts, and any nested folders.
  • 18. More Directives ‣ require ‣ require_tree . ‣ require_self ‣ require_directory ‘posts’ ‣ include ‘file’ ‣ depend_on ‘file.png’ Asset Pipeline !"#$%&'(&)*+%, require_self takes the code from the current file and inlines it in place, helps when requiring dependencies after require_self that depend on the current file require_directory is similar to require_tree, but it wont traverse nested directories include works like require, but will always insert the content even if its been required already depend_on will declare a dependency that doesn’t get required, but is used for caching purposes * will go into caching stuff more later
  • 19. Table of Contents 01 The pain of assets in 3.0 02 Asset Pipeline to the rescue 03 New Stuff 04 Deployment story Asset Pipeline !"#$%&'(&)*+%,
  • 20. CoffeeScript and Sass ‣ Gemfile group :assets do gem 'sass-rails' gem 'coffee-script' end Default in 3.1 Asset Pipeline !"#$%&'(&)*+%, The asset group is required by default in development and test environments, but not in production.
  • 21. CoffeeScript and Sass ‣ app/assets/javascripts/application.js.coffee jQuery ($) -> $('.truncate-more-link').live 'click', (e) -> $this = $ this # do more stuff here ‣ app/assets/stylesheets/application.css.scss $blue: #3bbfce; $margin: 16px; .content-navigation { border-color: $blue; color: darken($blue, 9%); } Asset Pipeline !"#$%&'(&)*+%, Before they are served to the browser, the asset pipeline passes these files through their respective preprocessors.
  • 22. Generators ‣ rails generate assets Review ‣ rails generate scaffold Comment Asset Pipeline !"#$%&'(&)*+%, Now generators work with coffeescript and sass and generate the appropriate files
  • 23. Chaining Preprocessors ‣ app/assets/stylesheets/application.css.scss.erb $blue: #3bbfce $margin: 16px .content-navigation background: url(<%= asset_path(‘background.png’) %>) color: darken($blue, 9%) $blue: #3bbfce $margin: 16px .content-navigation background: url(/assets/background-374e12d5b46ffa3d2f92a11dbae9b06a.png) color: darken($blue, 9%) Asset Pipeline !"#$%&'(&)*+%, Before they are served to the browser, the asset pipeline passes these files through their respective preprocessors.
  • 24. Preprocessors https://github.com/rtomayko/tilt Asset Pipeline !"#$%&'(&)*+%,
  • 25. Table of Contents 01 The pain of assets in 3.0 02 Asset Pipeline to the rescue 03 New Stuff 04 Deployment story Asset Pipeline !"#$%&'(&)*+%,
  • 26. rake assets:precompile ‣ app/assets/stylesheets/application.css.scss.erb $blue: #3bbfce .content-navigation background: url(<%= asset_path(‘background.png’) %>) color: darken($blue, 9%) ‣ public/assets/application-e317be572968fcf8892b5c767f776d82.css .content-navigation { background: url(/assets/background-92a96fe5e422523a2de56c9f0defe51b.png); color: #2ca2af; } Asset Pipeline !"#$%&'(&)*+%, All assets get compiled into public/assets, where they can easily be served by your webserver or a CDN.
  • 27. Precompile ‣ config/deploy.rb before :"deploy:symlink", :"deploy:assets"; desc "Compile assets" task :assets do run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile" end Asset Pipeline !"#$%&'(&)*+%,
  • 28. Caching <script src="/javascripts/application.js?1292365692"> </script> <script src="/assets/ application-374e12d5b46ffa3d2f92a11dbae9b06a.js"> </script> depend_on Asset Pipeline !"#$%&'(&)*+%, Rails used to append a deploy timestamp to all asset paths to reset the cache. With the asset pipeline, there is no deploy timestamp. Instead, rails will include an MD5 hash of the contents of the file in the filename itself. So now the cache stays fresh until the file actually changes, so if you are deploying a lot, but not changing the asset files, then you should see some nice performance improvements.
  • 29. Compression ‣ config/environments/production.rb # Compress both stylesheets and JavaScripts config.assets.compress = true gem 'uglifier' requires js runtime Asset Pipeline !"#$%&'(&)*+%, Both Mac OS X and Windows ship with javascript runtimes If you are deploying to heroku you can use therubyracer-heroku, which includes a build of nodejs specifically for heroku, but it will bloat your heroku slug size by 30MB
  • 30. Rails 3.1 Asset Pipeline Any questions? Asset Pipeline !"#$%&'(&)*+%,