Nick Sieger-Exploring Rails 3 Through Choices

91
Rails 3 Through Choices Nick Sieger [email protected] Sunday, March 21, 2010

description

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.

Transcript of Nick Sieger-Exploring Rails 3 Through Choices

Page 1: Nick Sieger-Exploring Rails 3 Through Choices

Rails 3 Through Choices

Nick [email protected]

Sunday, March 21, 2010

Page 2: Nick Sieger-Exploring Rails 3 Through Choices

Nick

Sunday, March 21, 2010

Page 3: Nick Sieger-Exploring Rails 3 Through Choices

@

Sunday, March 21, 2010

Page 4: Nick Sieger-Exploring Rails 3 Through Choices

EY Cloud

Sunday, March 21, 2010

Page 5: Nick Sieger-Exploring Rails 3 Through Choices

EY Open Source

Rubinius Ruby 1.8.6

JRubyRails

Sunday, March 21, 2010

Page 6: Nick Sieger-Exploring Rails 3 Through Choices

Topic

Sunday, March 21, 2010

Page 7: Nick Sieger-Exploring Rails 3 Through Choices

3

Sunday, March 21, 2010

Page 8: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 9: Nick Sieger-Exploring Rails 3 Through Choices

Confession

Sunday, March 21, 2010

Page 10: Nick Sieger-Exploring Rails 3 Through Choices

I’m not a web developer

Sunday, March 21, 2010

Page 11: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 12: Nick Sieger-Exploring Rails 3 Through Choices

HTML

CSSJavaScript

Ruby

JSON

XML

SQL

HTTPTCP/IP

DNS

Apache

NginxFCGI Memcached

Sunday, March 21, 2010

Page 13: Nick Sieger-Exploring Rails 3 Through Choices

HTML

CSSJavaScript

Ruby

JSON

XML

SQL

HTTPTCP/IP

DNS

Apache

NginxFCGI Memcached

Sunday, March 21, 2010

Page 14: Nick Sieger-Exploring Rails 3 Through Choices

=Sunday, March 21, 2010

Page 15: Nick Sieger-Exploring Rails 3 Through Choices

Rubyin 2010

Sunday, March 21, 2010

Page 16: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 17: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 18: Nick Sieger-Exploring Rails 3 Through Choices

Ruby VMs

Sunday, March 21, 2010

Page 19: Nick Sieger-Exploring Rails 3 Through Choices

Ruby 1.8almost seven years old!

Sunday, March 21, 2010

Page 20: Nick Sieger-Exploring Rails 3 Through Choices

Ruby 1.9.2August 2010

Sunday, March 21, 2010

Page 21: Nick Sieger-Exploring Rails 3 Through Choices

JRuby 1.5April 2010

Sunday, March 21, 2010

Page 22: Nick Sieger-Exploring Rails 3 Through Choices

Rubinius 1.0Q2 2010

Sunday, March 21, 2010

Page 23: Nick Sieger-Exploring Rails 3 Through Choices

Ruby 2.0Let the prognostication begin

Sunday, March 21, 2010

Page 24: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 25: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 26: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 27: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 28: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 29: Nick Sieger-Exploring Rails 3 Through Choices

iSunday, March 21, 2010

Page 30: Nick Sieger-Exploring Rails 3 Through Choices

paralyzed bychoice

Sunday, March 21, 2010

Page 31: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 32: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 33: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 34: Nick Sieger-Exploring Rails 3 Through Choices

opinionated software

Sunday, March 21, 2010

Page 35: Nick Sieger-Exploring Rails 3 Through Choices

making choices

Sunday, March 21, 2010

Page 36: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 37: Nick Sieger-Exploring Rails 3 Through Choices

6 years later...

Sunday, March 21, 2010

Page 38: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 39: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 40: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010

Page 41: Nick Sieger-Exploring Rails 3 Through Choices

Rails 3highlights

Sunday, March 21, 2010

Page 42: Nick Sieger-Exploring Rails 3 Through Choices

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

Page 43: Nick Sieger-Exploring Rails 3 Through Choices

Options$ rails -hUsage: 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

Page 44: Nick Sieger-Exploring Rails 3 Through Choices

Script

script/serverscript/generatescript/console

script/rails sscript/rails gscript/rails c

rails serverrails generaterails console

Sunday, March 21, 2010

Page 45: Nick Sieger-Exploring Rails 3 Through Choices

Layout

Sunday, March 21, 2010

Page 46: Nick Sieger-Exploring Rails 3 Through Choices

Classes

class MyController < ApplicationControllerend

class MyModel < ActiveRecord::Baseend

Sunday, March 21, 2010

Page 47: Nick Sieger-Exploring Rails 3 Through Choices

Unobtrusive JavaScript

Sunday, March 21, 2010

Page 48: Nick Sieger-Exploring Rails 3 Through Choices

JavaScript: R2link_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

Page 49: Nick Sieger-Exploring Rails 3 Through Choices

JavaScript: R3

<a href="/images/9" rel="nofollow" data-method="delete" data-confirm="Are you sure?">Delete Image</a>

link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete

Sunday, March 21, 2010

Page 50: Nick Sieger-Exploring Rails 3 Through Choices

XSS Safety

Sunday, March 21, 2010

Page 51: Nick Sieger-Exploring Rails 3 Through Choices

XSS Safety: R2

<%= h safe_value %>

<%= unsafe_value %>

Sunday, March 21, 2010

Page 52: Nick Sieger-Exploring Rails 3 Through Choices

XSS Safety: R3

<%= safe_value %>

<%= raw unsafe_value %>

Sunday, March 21, 2010

Page 53: Nick Sieger-Exploring Rails 3 Through Choices

Routing

Sunday, March 21, 2010

Page 54: Nick Sieger-Exploring Rails 3 Through Choices

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

Page 55: Nick Sieger-Exploring Rails 3 Through Choices

Routing: R3

match 'products/:id' => 'catalog#view'

resources :products do resources :comments resources :sales do get :recent, :on => :collection endend

Sunday, March 21, 2010

Page 56: Nick Sieger-Exploring Rails 3 Through Choices

More

• Cleaner ActionMailer

• ActiveRecord lazy queries

• ...

Sunday, March 21, 2010

Page 57: Nick Sieger-Exploring Rails 3 Through Choices

First OnlineRailsConf

http://bit.ly/online-railsconf-slides

Sunday, March 21, 2010

Page 58: Nick Sieger-Exploring Rails 3 Through Choices

APIs as Glue

Sunday, March 21, 2010

Page 59: Nick Sieger-Exploring Rails 3 Through Choices

Railties

Sunday, March 21, 2010

Page 60: Nick Sieger-Exploring Rails 3 Through Choices

The Extension APIfor Rails

Sunday, March 21, 2010

Page 61: Nick Sieger-Exploring Rails 3 Through Choices

Decouplinghttp://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 62: Nick Sieger-Exploring Rails 3 Through Choices

Decoupling

ActionView

ActionControllerActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 63: Nick Sieger-Exploring Rails 3 Through Choices

Decoupling

ActionViewActionController ActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 64: Nick Sieger-Exploring Rails 3 Through Choices

Decoupling

ActionViewActionController ActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 65: Nick Sieger-Exploring Rails 3 Through Choices

ActionDispatch

AbstractController

Decoupling

ActionViewActionController ActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 66: Nick Sieger-Exploring Rails 3 Through Choices

ActionDispatch

AbstractController

Decoupling

ActionViewActionController

View Context

ActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 67: Nick Sieger-Exploring Rails 3 Through Choices

ActionDispatch

AbstractController

Decoupling

ActionView

ActiveModel

ActionController

View Context

ActiveRecord

http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/

Sunday, March 21, 2010

Page 68: Nick Sieger-Exploring Rails 3 Through Choices

Encapsulatewhat varies

Sunday, March 21, 2010

Page 69: Nick Sieger-Exploring Rails 3 Through Choices

Railties:Configuration encapsulated

Sunday, March 21, 2010

Page 70: Nick Sieger-Exploring Rails 3 Through Choices

Initialization Generators

Rake Tasks Logging Events

Railtie Elements

Sunday, March 21, 2010

Page 71: Nick Sieger-Exploring Rails 3 Through Choices

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'] endend

Sunday, March 21, 2010

Page 72: Nick Sieger-Exploring Rails 3 Through Choices

Rails’ Railties

ActionController::Railtie

ActionDispatch::Railtie

ActiveRecord::Railtie

ActionMailer::Railtie

Sunday, March 21, 2010

Page 73: Nick Sieger-Exploring Rails 3 Through Choices

Rails::Railtie

Rails::Application

Sunday, March 21, 2010

Page 74: Nick Sieger-Exploring Rails 3 Through Choices

Init: Rails 2def 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 initialize_whiny_nils

initialize_time_zone initialize_i18n

initialize_framework_settings initialize_framework_views

initialize_metal

add_support_load_paths

check_for_unbuilt_gems

load_gems load_plugins

# pick up any gems that plugins depend on add_gem_load_paths load_gems check_gem_dependencies

# bail out if gems are missing - note that check_gem_dependencies will have # already called abort() unless $gems_rake_task is set return unless gems_dependencies_loaded

load_application_initializers

# the framework is now fully initialized 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 = trueend

Sunday, March 21, 2010

Page 75: Nick Sieger-Exploring Rails 3 Through Choices

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 initialize_whiny_nils

initialize_time_zone initialize_i18n

initialize_framework_settings initialize_framework_views

initialize_metal

add_support_load_paths

check_for_unbuilt_gems

load_gems load_plugins

# pick up any gems that plugins depend on add_gem_load_paths load_gems check_gem_dependencies

# bail out if gems are missing - note that check_gem_dependencies will have # already called abort() unless $gems_rake_task is set return unless gems_dependencies_loaded

load_application_initializers

# the framework is now fully initialized 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 = trueend

Sunday, March 21, 2010

Page 76: Nick Sieger-Exploring Rails 3 Through Choices

Init: Rails 3

class Rails::Application def initialize! run_initializers(self) self endend

Sunday, March 21, 2010

Page 77: Nick Sieger-Exploring Rails 3 Through Choices

rails newapp --skip-activerecord

ActiveRecord::Railtie is not loaded

No AR-related config or generators

Sunday, March 21, 2010

Page 78: Nick Sieger-Exploring Rails 3 Through Choices

• Modify configuration/init

• Add Rake tasks

• Add generators

Use Railtie to:

Sunday, March 21, 2010

Page 79: Nick Sieger-Exploring Rails 3 Through Choices

Rack

• Deserves a talk by itself

• HTTP request pipeline API

• See Ryan Tomayko’s slides from Online RailsConf

Sunday, March 21, 2010

Page 80: Nick Sieger-Exploring Rails 3 Through Choices

ActiveModel

Sunday, March 21, 2010

Page 81: Nick Sieger-Exploring Rails 3 Through Choices

codifies contractbetween

controllerand model

Sunday, March 21, 2010

Page 82: Nick Sieger-Exploring Rails 3 Through Choices

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; ...; endend

Contract

Sunday, March 21, 2010

Page 83: Nick Sieger-Exploring Rails 3 Through Choices

class ModelLintTest < Test::Unit::TestCase def setup @model = Model.new end

include ActiveModel::Lint::Testsend

Sunday, March 21, 2010

Page 84: Nick Sieger-Exploring Rails 3 Through Choices

Validations

class SimpleModel include ActiveModel::Validations

# => Model#valid?, Model#errors # plus validation frameworkend

Sunday, March 21, 2010

Page 85: Nick Sieger-Exploring Rails 3 Through Choices

class IceCream include ActiveModel::Validations attr_accessor :flavour validates_presence_of :flavourend

ic = IceCream.newic.valid? # => falseic.errors.full_messages # => ["Flavour can't be blank"]

ic.flavour = "vanilla"ic.valid? # => true

Sunday, March 21, 2010

Page 86: Nick Sieger-Exploring Rails 3 Through Choices

ActiveModel::Callbacks - before/after hooksActiveModel::Dirty - dirty attribute trackingActiveModel::Errors - #errorsActiveModel::Serialization - #to_xml, #to_json, etc.ActiveModel::Translation - I18n for model attributesActiveModel::Validations - Full validations framework

See ActiveModel RDoc comments for more info

AMo Modules

Sunday, March 21, 2010

Page 87: Nick Sieger-Exploring Rails 3 Through Choices

Putting it together

Sunday, March 21, 2010

Page 88: Nick Sieger-Exploring Rails 3 Through Choices

3

JRuby

Sunday, March 21, 2010

Page 89: Nick Sieger-Exploring Rails 3 Through Choices

Explore Rails 3for yourself!

Sunday, March 21, 2010

Page 90: Nick Sieger-Exploring Rails 3 Through Choices

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

Page 91: Nick Sieger-Exploring Rails 3 Through Choices

Sunday, March 21, 2010