Nick Sieger-Exploring Rails 3 Through Choices

2,661 views

Published on

One of the most eagerly anticipated aspects of the fast-approaching Rails 3 release is its inherent modularity, and how that modularity gives the application developer more choice. We'll start with a tour of some of the headlining differences between Rails 2 and 3, and then put Rails 3's internal architecture to the test by demonstrating how to plug in some non-standard standard components, including an example of how to wire in a Java library using JRuby.

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,661
On SlideShare
0
From Embeds
0
Number of Embeds
40
Actions
Shares
0
Downloads
76
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Nick Sieger-Exploring Rails 3 Through Choices

  1. 1. Rails 3 Through Choices Nick Sieger nsieger@engineyard.com Sunday, March 21, 2010
  2. 2. Nick Sunday, March 21, 2010
  3. 3. @ Sunday, March 21, 2010
  4. 4. EY Cloud Sunday, March 21, 2010
  5. 5. EY Open Source Rails JRuby Rubinius Ruby 1.8.6 Sunday, March 21, 2010
  6. 6. Topic Sunday, March 21, 2010
  7. 7. 3 Sunday, March 21, 2010
  8. 8. Sunday, March 21, 2010
  9. 9. Confession Sunday, March 21, 2010
  10. 10. I’m not a web developer Sunday, March 21, 2010
  11. 11. Sunday, March 21, 2010
  12. 12. HTML JSON JavaScript SQL CSS XML Ruby HTTP DNS TCP/IP Nginx Apache FCGI Memcached Sunday, March 21, 2010
  13. 13. HTML JSON JavaScript SQL CSS HTTP DNS Ruby XML TCP/IP Nginx Apache FCGI Memcached Sunday, March 21, 2010
  14. 14. = Sunday, March 21, 2010
  15. 15. Ruby in 2010 Sunday, March 21, 2010
  16. 16. Sunday, March 21, 2010
  17. 17. Sunday, March 21, 2010
  18. 18. Ruby VMs Sunday, March 21, 2010
  19. 19. Ruby 1.8 almost seven years old! Sunday, March 21, 2010
  20. 20. Ruby 1.9.2 August 2010 Sunday, March 21, 2010
  21. 21. JRuby 1.5 April 2010 Sunday, March 21, 2010
  22. 22. Rubinius 1.0 Q2 2010 Sunday, March 21, 2010
  23. 23. Ruby 2.0 Let the prognostication begin Sunday, March 21, 2010
  24. 24. Sunday, March 21, 2010
  25. 25. Sunday, March 21, 2010
  26. 26. Sunday, March 21, 2010
  27. 27. Sunday, March 21, 2010
  28. 28. Sunday, March 21, 2010
  29. 29. i Sunday, March 21, 2010
  30. 30. paralyzed by choice Sunday, March 21, 2010
  31. 31. Sunday, March 21, 2010
  32. 32. Sunday, March 21, 2010
  33. 33. Sunday, March 21, 2010
  34. 34. opinionated software Sunday, March 21, 2010
  35. 35. making choices Sunday, March 21, 2010
  36. 36. Sunday, March 21, 2010
  37. 37. 6 years later... Sunday, March 21, 2010
  38. 38. Sunday, March 21, 2010
  39. 39. Sunday, March 21, 2010
  40. 40. Sunday, March 21, 2010
  41. 41. Rails 3 highlights Sunday, March 21, 2010
  42. 42. Rails command $ rails newapp create create README create .gitignore create Rakefile create config.ru create Gemfile create app create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/models create app/views/layouts create config ... Sunday, March 21, 2010
  43. 43. Options $ rails -h Usage: rails APP_PATH [options] Options: ... -O, [--skip-activerecord] # Skip ActiveRecord files -T, [--skip-testunit] # Skip TestUnit files -J, [--skip-prototype] # Skip Prototype files Sunday, March 21, 2010
  44. 44. Script script/rails s script/rails g script/server script/rails c script/generate script/console rails server rails generate rails console Sunday, March 21, 2010
  45. 45. Layout Sunday, March 21, 2010
  46. 46. Classes class MyController < ApplicationController end class MyModel < ActiveRecord::Base end Sunday, March 21, 2010
  47. 47. Unobtrusive JavaScript Sunday, March 21, 2010
  48. 48. JavaScript: R2 link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild (f); f.method = 'POST'; f.action = this.href; var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild (m);f.submit(); };return false;">Delete Image</a> Sunday, March 21, 2010
  49. 49. JavaScript: R3 link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete <a href="/images/9" rel="nofollow" data-method="delete" data-confirm="Are you sure?">Delete Image</a> Sunday, March 21, 2010
  50. 50. XSS Safety Sunday, March 21, 2010
  51. 51. XSS Safety: R2 <%= h safe_value %> <%= unsafe_value %> Sunday, March 21, 2010
  52. 52. XSS Safety: R3 <%= safe_value %> <%= raw unsafe_value %> Sunday, March 21, 2010
  53. 53. Routing Sunday, March 21, 2010
  54. 54. Routing: R2 map.connect 'products/:id', :controller => 'catalog', :action => 'view' map.resources :products do |products| products.resources :comments products.resources :sales, :collection => { :recent => :get } end Sunday, March 21, 2010
  55. 55. Routing: R3 match 'products/:id' => 'catalog#view' resources :products do resources :comments resources :sales do get :recent, :on => :collection end end Sunday, March 21, 2010
  56. 56. More • Cleaner ActionMailer • ActiveRecord lazy queries • ... Sunday, March 21, 2010
  57. 57. First Online RailsConf http://bit.ly/online-railsconf-slides Sunday, March 21, 2010
  58. 58. APIs as Glue Sunday, March 21, 2010
  59. 59. Railties Sunday, March 21, 2010
  60. 60. The Extension API for Rails Sunday, March 21, 2010
  61. 61. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ Sunday, March 21, 2010
  62. 62. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionView ActionController ActiveRecord Sunday, March 21, 2010
  63. 63. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionController ActionView ActiveRecord Sunday, March 21, 2010
  64. 64. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionController ActionView ActiveRecord Sunday, March 21, 2010
  65. 65. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionDispatch AbstractController ActionController ActionView ActiveRecord Sunday, March 21, 2010
  66. 66. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionDispatch View AbstractController Context ActionController ActionView ActiveRecord Sunday, March 21, 2010
  67. 67. Decoupling http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/ ActionDispatch View ActiveModel AbstractController Context ActionController ActionView ActiveRecord Sunday, March 21, 2010
  68. 68. Encapsulate what varies Sunday, March 21, 2010
  69. 69. Railties: Configuration encapsulated Sunday, March 21, 2010
  70. 70. Railtie Elements Initialization Generators Rake Tasks Logging Events Sunday, March 21, 2010
  71. 71. Example require 'rails/railtie' class MemCacheClusterRailtie < Rails::Railtie initializer :setup_mem_cache_cluster do |app| app.config.cache_store = [ :mem_cache_store, 'cache1:11211', 'cache2:11211'] end end Sunday, March 21, 2010
  72. 72. Rails’ Railties ActionController:: ActionDispatch:: Railtie Railtie ActiveRecord:: ActionMailer:: Railtie Railtie Sunday, March 21, 2010
  73. 73. Rails::Railtie Rails::Application Sunday, March 21, 2010
  74. 74. Init: Rails 2 def process Rails.configuration = configuration check_ruby_version install_gem_spec_stubs set_load_path add_gem_load_paths require_frameworks set_autoload_paths add_plugin_load_paths load_environment preload_frameworks initialize_encoding initialize_database initialize_cache initialize_framework_caches initialize_logger initialize_framework_logging initialize_dependency_mechanism Sunday, March 21, 2010
  75. 75. load_application_initializers # the framework is now fully initialized Init: Rails 2 after_initialize # Setup database middleware after initializers have run initialize_database_middleware # Prepare dispatcher callbacks and run 'prepare' callbacks prepare_dispatcher # Routing must be initialized after plugins to allow the former to extend the routes initialize_routing # Observers are loaded after plugins in case Observers or observed models are modified by plugins. load_observers # Load view path cache load_view_paths # Load application classes load_application_classes # Disable dependency loading during request cycle disable_dependency_loading # Flag initialized Rails.initialized = true end Sunday, March 21, 2010
  76. 76. Init: Rails 3 class Rails::Application def initialize! run_initializers(self) self end end Sunday, March 21, 2010
  77. 77. rails newapp --skip-activerecord ActiveRecord::Railtie is not loaded No AR-related config or generators Sunday, March 21, 2010
  78. 78. Use Railtie to: • Modify configuration/init • Add Rake tasks • Add generators Sunday, March 21, 2010
  79. 79. Rack • Deserves a talk by itself • HTTP request pipeline API • See Ryan Tomayko’s slides from Online RailsConf Sunday, March 21, 2010
  80. 80. ActiveModel Sunday, March 21, 2010
  81. 81. codifies contract between controller and model Sunday, March 21, 2010
  82. 82. Contract class Model def to_model; ...; end def to_key; ...; end def to_param; ...; end def valid?; ...; end def persisted?; ...; end def self.model_name; ...; end def errors; ...; end end Sunday, March 21, 2010
  83. 83. class ModelLintTest < Test::Unit::TestCase def setup @model = Model.new end include ActiveModel::Lint::Tests end Sunday, March 21, 2010
  84. 84. Validations class SimpleModel include ActiveModel::Validations # => Model#valid?, Model#errors # plus validation framework end Sunday, March 21, 2010
  85. 85. class IceCream include ActiveModel::Validations attr_accessor :flavour validates_presence_of :flavour end ic = IceCream.new ic.valid? # => false ic.errors.full_messages # => ["Flavour can't be blank"] ic.flavour = "vanilla" ic.valid? # => true Sunday, March 21, 2010
  86. 86. AMo Modules ActiveModel::Callbacks - before/after hooks ActiveModel::Dirty - dirty attribute tracking ActiveModel::Errors - #errors ActiveModel::Serialization - #to_xml, #to_json, etc. ActiveModel::Translation - I18n for model attributes ActiveModel::Validations - Full validations framework See ActiveModel RDoc comments for more info Sunday, March 21, 2010
  87. 87. Putting it together Sunday, March 21, 2010
  88. 88. JRuby 3 Sunday, March 21, 2010
  89. 89. Explore Rails 3 for yourself! Sunday, March 21, 2010
  90. 90. Photo Credits • http://www.flickr.com/photos/henrybloomfield/2615837191/ • http://www.flickr.com/photos/mountainbread/2528179567/ • http://www.flickr.com/photos/aai/3712515230/ • http://www.flickr.com/photos/mikeschinkel/2703438152/ Sunday, March 21, 2010
  91. 91. Sunday, March 21, 2010

×