nick sieger-exploring rails 3 through choices

Post on 10-May-2015

2.424 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

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

Rails 3 Through Choices

Nick Siegernsieger@engineyard.com

Sunday, March 21, 2010

Nick

Sunday, March 21, 2010

@

Sunday, March 21, 2010

EY Cloud

Sunday, March 21, 2010

EY Open Source

Rubinius Ruby 1.8.6

JRubyRails

Sunday, March 21, 2010

Topic

Sunday, March 21, 2010

3

Sunday, March 21, 2010

Sunday, March 21, 2010

Confession

Sunday, March 21, 2010

I’m not a web developer

Sunday, March 21, 2010

Sunday, March 21, 2010

HTML

CSSJavaScript

Ruby

JSON

XML

SQL

HTTPTCP/IP

DNS

Apache

NginxFCGI Memcached

Sunday, March 21, 2010

HTML

CSSJavaScript

Ruby

JSON

XML

SQL

HTTPTCP/IP

DNS

Apache

NginxFCGI Memcached

Sunday, March 21, 2010

=Sunday, March 21, 2010

Rubyin 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Ruby VMs

Sunday, March 21, 2010

Ruby 1.8almost seven years old!

Sunday, March 21, 2010

Ruby 1.9.2August 2010

Sunday, March 21, 2010

JRuby 1.5April 2010

Sunday, March 21, 2010

Rubinius 1.0Q2 2010

Sunday, March 21, 2010

Ruby 2.0Let the prognostication begin

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

iSunday, March 21, 2010

paralyzed bychoice

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

opinionated software

Sunday, March 21, 2010

making choices

Sunday, March 21, 2010

Sunday, March 21, 2010

6 years later...

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Sunday, March 21, 2010

Rails 3highlights

Sunday, March 21, 2010

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

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

Script

script/serverscript/generatescript/console

script/rails sscript/rails gscript/rails c

rails serverrails generaterails console

Sunday, March 21, 2010

Layout

Sunday, March 21, 2010

Classes

class MyController < ApplicationControllerend

class MyModel < ActiveRecord::Baseend

Sunday, March 21, 2010

Unobtrusive JavaScript

Sunday, March 21, 2010

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

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

XSS Safety

Sunday, March 21, 2010

XSS Safety: R2

<%= h safe_value %>

<%= unsafe_value %>

Sunday, March 21, 2010

XSS Safety: R3

<%= safe_value %>

<%= raw unsafe_value %>

Sunday, March 21, 2010

Routing

Sunday, March 21, 2010

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

Routing: R3

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

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

Sunday, March 21, 2010

More

• Cleaner ActionMailer

• ActiveRecord lazy queries

• ...

Sunday, March 21, 2010

First OnlineRailsConf

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

Sunday, March 21, 2010

APIs as Glue

Sunday, March 21, 2010

Railties

Sunday, March 21, 2010

The Extension APIfor Rails

Sunday, March 21, 2010

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

Sunday, March 21, 2010

Decoupling

ActionView

ActionControllerActiveRecord

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

Sunday, March 21, 2010

Decoupling

ActionViewActionController ActiveRecord

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

Sunday, March 21, 2010

Decoupling

ActionViewActionController ActiveRecord

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

Sunday, March 21, 2010

ActionDispatch

AbstractController

Decoupling

ActionViewActionController ActiveRecord

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

Sunday, March 21, 2010

ActionDispatch

AbstractController

Decoupling

ActionViewActionController

View Context

ActiveRecord

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

Sunday, March 21, 2010

ActionDispatch

AbstractController

Decoupling

ActionView

ActiveModel

ActionController

View Context

ActiveRecord

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

Sunday, March 21, 2010

Encapsulatewhat varies

Sunday, March 21, 2010

Railties:Configuration encapsulated

Sunday, March 21, 2010

Initialization Generators

Rake Tasks Logging Events

Railtie Elements

Sunday, March 21, 2010

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

Rails’ Railties

ActionController::Railtie

ActionDispatch::Railtie

ActiveRecord::Railtie

ActionMailer::Railtie

Sunday, March 21, 2010

Rails::Railtie

Rails::Application

Sunday, March 21, 2010

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

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

Init: Rails 3

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

Sunday, March 21, 2010

rails newapp --skip-activerecord

ActiveRecord::Railtie is not loaded

No AR-related config or generators

Sunday, March 21, 2010

• Modify configuration/init

• Add Rake tasks

• Add generators

Use Railtie to:

Sunday, March 21, 2010

Rack

• Deserves a talk by itself

• HTTP request pipeline API

• See Ryan Tomayko’s slides from Online RailsConf

Sunday, March 21, 2010

ActiveModel

Sunday, March 21, 2010

codifies contractbetween

controllerand model

Sunday, March 21, 2010

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

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

include ActiveModel::Lint::Testsend

Sunday, March 21, 2010

Validations

class SimpleModel include ActiveModel::Validations

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

Sunday, March 21, 2010

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

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

Putting it together

Sunday, March 21, 2010

3

JRuby

Sunday, March 21, 2010

Explore Rails 3for yourself!

Sunday, March 21, 2010

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

Sunday, March 21, 2010

top related