SlideShare ist ein Scribd-Unternehmen logo
1 von 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             !"#$%&'(&)*+%,

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Kürzlich hochgeladen (20)

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 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 !"#$%&'(&)*+%,