SlideShare une entreprise Scribd logo
1  sur  30
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             !"#$%&'(&)*+%,

Contenu connexe

Tendances

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.jsJay Harris
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.jscodeofficer
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricksJavier 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 developmentCaldera Labs
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecordMark Menard
 
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 2013Amazon 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 WebpackIgnacio Martín
 
api-platform: the ultimate API platform
api-platform: the ultimate API platformapi-platform: the ultimate API platform
api-platform: the ultimate API platformStefan 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 DevelopersJeremy Lindblom
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to ServicesCraig Kerstiens
 

Tendances (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
 

Similaire à Rails 3.1 Asset Pipeline

RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive 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 RubyFabio 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 chaosMatteo Papadopoulos
 
Understanding asset pipeline plugin
Understanding asset pipeline pluginUnderstanding asset pipeline plugin
Understanding asset pipeline pluginRailsCarma
 
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 RubyFabio Akita
 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsRORLAB
 
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails DevsDiacode
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScriptbensmithett
 
Curing Webpack Cancer
Curing Webpack CancerCuring Webpack Cancer
Curing Webpack CancerNeel 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 ArangoDBArangoDB Database
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalystsvilen.ivanov
 

Similaire à 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
 

Dernier

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Dernier (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

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 !"#$%&'(&)*+%,