Rails 4 at Austin on Rails

2,249 views

Published on

Slides from my presentation at Austin on Rails in Austin, TX on Rails 4 on June 25, 2013. This was originally a Keynote presentation that I apparently can't upload directly to Slideshare without exporting as PPT (seriously, guys, wtf) so ignore any display issues.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,249
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
10
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Rails 4 dropped today, out on rubygems.org now
  • Rails 5.x - weblog.rubyonrails.org, dhh
  • ActiveResource: ORM for REST Session Store: - generally a bad thing to use anyway Observers: - used as a dumping ground for persistence ops, better as callbacks - used to expire/invalidate cache, which is no longer needed Page/Action Caching - going to totally change (more on that later)
  • Threading in Ruby = more complex than this talk, no time GIL = prevents more than one Ruby thread from executing at a time even with multiple existing threads, keeps memory from getting crazy AKA GVL = Global VM Lock JRuby/Rubinius = no GIL
  • Defaults with Rails 4, no need to add them Depends on browser whether or not they’ll do any good Will never stop a determined attacker
  • Tested on a 3.2 app with activerecord-postgres-hstore gem
  • Not tested, no need to use it yet
  • Not tested, no need to use it yet
  • use default scope for ordered at asc for first/last functionality
  • Some report +100% speed increase (time / 2) Depends on how much CSS/JS you have
  • People who bitch about new defaults in rails constantly amuse me
  • Background Queuing: API for resque etc. to hook into - removed due to being considered “half baked” Async Action Mailer - depends on background queuing API where.like/not like - DHH says: odd mismatch of SQL and ruby with % sign opens door to expand DSL into really weird things (greater_or_equal_than) fairly uncommon query, doesn’t need optimization
  • Rails 4 at Austin on Rails

    1. 1. Engine Yard - www.engineyard.com Rails 4Rails 4 Changes and New FeaturesChanges and New Features Changes and New FeaturesChanges and New Features J. Austin Hughey Field Application Engineer Engine Yard @jaustinhughey @openhackatx @engineyard
    2. 2. 2 Engine Yard - www.engineyard.com • Multiple separations and deprecations • HTTP semantics changes • More security features • Lots of cool PostgreSQL integration OverviewOverview
    3. 3. 3 Engine Yard - www.engineyard.com • Ruby 1.9.3 minimum – 2.0 recommended – Rails 5.x will require >= 2.0 so might as well upgrade now • Many deprecated items are now separate gems – May not be compatible with Rails 4.1 – Use only as a bridge; make sure to get rid of uses of the old stuff • PATCH verb (instead of PUT) – PATCH :update, article: { author: “foo”, title: “bar”, body: “blah” } • “Strong Parameters” • Thread Safe by default ChangesChanges
    4. 4. 4 QuickTime™ and a GIF decompressor are needed to see this picture. Engine Yard - www.engineyard.com Saying goodbye...Saying goodbye...
    5. 5. 5 Engine Yard - www.engineyard.com – vendor/plugins - use gems instead – ActiveResource • https://github.com/rails/activeresource – Hash-based/dynamic finder methods • https://github.com/rails/activerecord-deprecated_finders – ActiveRecord::SessionStore • https://github.com/rails/activerecord-session_store – Observers • https://github.com/rails/rails-observers – Page and Action Caching • https://github.com/rails/actionpack-action_caching • https://github.com/rails/actionpack-page_caching Removed in 4.0Removed in 4.0
    6. 6. 6 Engine Yard - www.engineyard.com PATCH
    7. 7. 7 Engine Yard - www.engineyard.com • HTTP says that a PUT request represents a complete representation of a resource. • Ergo, we’ve been using PUT wrong. We rarely pass a whole resource to a controller on edits - just the changed bits. • Solution: use PATCH instead. PATCH sends up just what’s changed. HTTP PATCHHTTP PATCH
    8. 8. 8 Engine Yard - www.engineyard.com • config.thread_safe is on by default • Still should try a truly threaded interpreter/server • JRuby/Rubinius + Puma, Passenger Enterprise THREAD SAFETYTHREAD SAFETY
    9. 9. 9 Engine Yard - www.engineyard.com • Click to edit Master text styles STRONG PARAMETERS
    10. 10. 10 Engine Yard - www.engineyard.com Strong ParametersStrong Parameters • Before:
    11. 11. 11 Engine Yard - www.engineyard.com • After:
    12. 12. 12 Engine Yard - www.engineyard.com Strong ParametersStrong Parameters • Why is this better? – Puts sanitization focus on user input vector - the controller – Frees up the developer to work with the data model uninhibited • Criticisms: – Breaks the idea that you should be able to throw ANYTHING at an object and it knows what to do with it. – Nested attributes can be a pain in the rear.
    13. 13. 13 Engine Yard - www.engineyard.com Encrypted CookiesEncrypted Cookies • New cookie store: “encrypted_cookie_store” • Now the default in Rails 4 • Encrypts cookies before being sent to the client, decrypts received cookies • Prevents user tampering • Not a complete security solution. • MIGHT annoy the NSA. Image credit: Electronic Frontier Foundation - eff.org
    14. 14. 14 Engine Yard - www.engineyard.com Default HeadersDefault Headers config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'X-Content-Type-Options' => 'nosniff' } Include default headers with each response coming from Rails.
    15. 15. 15 Engine Yard - www.engineyard.com THE ELEPHANT IN THE ROOMTHE ELEPHANT IN THE ROOM
    16. 16. 16 Engine Yard - www.engineyard.com Rails <3 PostgreSQLRails <3 PostgreSQL • Rails 4 includes support for PostgreSQL datatypes: – hstore – arrays – INET – CIDR – MACADDR – uuid
    17. 17. 17 Engine Yard - www.engineyard.com PostgreSQL hstorePostgreSQL hstore • CREATE EXTENSION hstore; – Or enable_extension "hstore" in migrations • Like serialized columns, but more efficient (not a text field) • GIST or GIN indexes – Read the PostgreSQL docs to figure out which is right for you • Querying is a little weird – User.where(“preferences @> ‘theme=>black’”) • Available in 3.2 through activerecord-postgres-hstore gem
    18. 18. 18 Engine Yard - www.engineyard.com PostgreSQL ArrayPostgreSQL Array create_table :foos do |t| t.integer :int_array, array: true t.string :string_array, array: true end foo = Foo.new foo.int_array = [1, 2, 3, 4, 5] foo.save
    19. 19. 19 Engine Yard - www.engineyard.com INET, CIDR, MACADDRINET, CIDR, MACADDR create_table :networks do |t| t.cidr :cidr_address t.inet :ip_address t.macaddr :mac_address end • cidr, inet both come out as a native Ruby IPAddr object • macaddr treated as a string
    20. 20. 20 Engine Yard - www.engineyard.com Using a UUIDUsing a UUID • Enable the uuid-ossp extension • create_table :name, id: :uuid { |t| ... }
    21. 21. 21 Engine Yard - www.engineyard.com TURBOLINKSTURBOLINKS ZOOM ZOOM!
    22. 22. 22 Engine Yard - www.engineyard.com • CAVEAT EMPTOR: May break some of your javascript Various event listeners may need to be changed Speed improvement depends on how much JS/CSS you have TurbolinksTurbolinks • Swaps out <body> contents with what should’ve been rendered by the server • Avoids the need to reload all the CSS/JS again • On by default, easily disabled • Makes everything look faster
    23. 23. 23 Engine Yard - www.engineyard.com Disabling TurbolinksDisabling Turbolinks • Remove from Gemfile • Remove from application.js •bundle https://github.com/rails/turbolinks
    24. 24. 24 Engine Yard - www.engineyard.com CACHE MONEYCACHE MONEY
    25. 25. 25 Engine Yard - www.engineyard.com Cache DigestsCache Digests • Forget bumping version numbers in your cache. • On application start, computes MD5 sum of cache content and stores the sum as a key; when the content changes, the MD5 sum changes thus invalidating the cache. <% cache [‘v3’, comment] do %> My comment: <%= comment.body %> <% end %> <% cache comment do %> My comment: <%= comment.body %> <% end %> BEFORE AFTER
    26. 26. 26 Engine Yard - www.engineyard.com • Click to edit Master text styles TT FOR SCIENCE
    27. 27. 27 Engine Yard - www.engineyard.com New Default Test LocationsNew Default Test Locations Then Now test/units test/models test/units/helpers test/helpers test/functional test/controllers test/functional test/mailers
    28. 28. 28 Engine Yard - www.engineyard.com LIVE STREAMINGLIVE STREAMING
    29. 29. 29 Engine Yard - www.engineyard.com Is it live?Is it live? • Stream response to the browser • Needs multi-threaded application server – e.g. Puma, Thin, Passenger Enterprise – Putting it behind a non-GIL addled interpreter also advised • Not a lot of examples in the wild yet • May not work on IE. :-( class MyController < ApplicationController include ActionController::Live def index 100.times { response.stream.write "hello worldn" } response.stream.close endend Example from http://tenderlovemaking.com/2012/07/30/is-it-live.html
    30. 30. 30 Engine Yard - www.engineyard.com Stuff NOT ShippingStuff NOT Shipping • Background Queuing • Asynchronous ActionMailer • where.like / where.not_like
    31. 31. 31 Engine Yard - www.engineyard.com UpgradingUpgrading • PAY ATTENTION to deprecation warnings • Have a *really* good set of tests and as high coverage as possible • Take it in stages, by sprints • 3.2 -> 4.0 will be easiest upgrade path
    32. 32. Thank You

    ×