rails 2.3 and rack

24
Rails 2.3 Brian Turnbull http://brianturnbull.com

Upload: dmytro-shteflyuk

Post on 12-Nov-2014

1.233 views

Category:

Documents


2 download

DESCRIPTION

Overview of new features in Rails 2.3 followed by a deeper discussion of Rack, Rack Middleware, and creating Metal Endpoints.

TRANSCRIPT

Page 1: Rails 2.3 and Rack

Rails 2.3

Brian Turnbullhttp://brianturnbull.com

Page 2: Rails 2.3 and Rack

Templates

rails app

templaterails app’

rails app -m template.rb

rails app -m http://example.com/template

rake rails:template LOCATION=template.rb

Page 3: Rails 2.3 and Rack

Templates# template.rbgem "hpricot"rake "gems:install"run "rm public/index.html"generate :scaffold, "person name:string"route "map.root :controller => 'people'"rake "db:migrate"

git :initgit :add => "."git :commit => "-a -m 'Initial commit'"

Pratik Naik — http://m.onkey.org/2008/12/4/rails-templates

Page 4: Rails 2.3 and Rack

Templates

Jeremy McAnally’s Template Library

http://github.com/jeremymcanally/rails-templates/tree/master

Page 5: Rails 2.3 and Rack

Engines

Rails Plugins turned to Eleven

Share Models, Views, and Controllers with Host App

Share Routes with Host App

http://rails-engines.org/news/2009/02/02/engines-in-rails-2-3/

Page 6: Rails 2.3 and Rack

Engines

Not Yet Fully Baked

Manually Manage Migrations

Manually Merge Public Assets

http://rails-engines.org/news/2009/02/02/engines-in-rails-2-3/

Page 7: Rails 2.3 and Rack

Nested TransactionsUser.transaction do User.create(:username => 'Admin') User.transaction(:requires_new => true) do User.create(:username => 'Regular') raise ActiveRecord::Rollback endend

User.find(:all) # => Returns only Admin

http://guides.rubyonrails.org/2_3_release_notes.html

Page 8: Rails 2.3 and Rack

Nested Attributes

class Book < ActiveRecord::Base has_one :author has_many :pages

accepts_nested_attributes_for :author, :pagesend

http://guides.rubyonrails.org/2_3_release_notes.html

Page 9: Rails 2.3 and Rack

Nested Formsclass Book < ActiveRecord::Base has_many :authors accepts_nested_attributes_for :authorsend

<% form_for @book do |book_form| %> <div> <%= book_form.label :title, 'Book Title:' %> <%= book_form.text_field :title %> </div> <% book_form.fields_for :authors do |author_form| %> <div> <%= author_form.label :name, 'Author Name:' %> <%= author_form.text_field :name %> </div> <% end %> <% end %> <%= book_form.submit %><% end %>

http://guides.rubyonrails.org/2_3_release_notes.html

Page 10: Rails 2.3 and Rack

Dynamic and Default Scopes## id Integer## customer_id Integer## status String## entered_at DateTimeclass Order < ActiveRecord::Base belongs_to :customer default_scope :order => 'entered_at'end

Order.scoped_by_customer_id(12)Order.scoped_by_customer_id(12).find(:all, :conditions => "status = 'open'")Order.scoped_by_customer_id(12).scoped_by_status("open")

http://guides.rubyonrails.org/2_3_release_notes.html

Page 11: Rails 2.3 and Rack

Other Changes

Multiple Conditions for CallbacksHTTP Digest AuthenticationLazy Loaded SessionsLocalized Viewsand More!

http://guides.rubyonrails.org/2_3_release_notes.html

Page 12: Rails 2.3 and Rack

Rails 2.3

Page 13: Rails 2.3 and Rack

Rails Metal

Page 14: Rails 2.3 and Rack

SPEED

Simplicity

Page 15: Rails 2.3 and Rack

Metal Endpoint

## app/metal/hello_metal.rbclass HelloMetal def self.call(env) if env["PATH_INFO"] =~ /^\/hello\/metal/ [200, {"Content-Type" => "text/plain"}, ["Hello, Metal!"]] else [404, {"Content-Type" => "text/html"}, ["Not Found"]] end endend

http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m

Page 16: Rails 2.3 and Rack

Equivalent Controller

## app/controllers/hello_rails_controller.rbclass HelloRailsController < ApplicationController def show headers['Content-Type'] = 'text/plain' render :text => 'Hello, Rails!' endend

http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m

Page 17: Rails 2.3 and Rack

Sinatra!

require 'sinatra'Sinatra::Application.set(:run => false)Sinatra::Application.set(:environment => :production)

HelloSinatra = Sinatra::Application.new unless defined? HelloSinatra

get '/hello/sinatra' do response['Content-Type'] = 'text/plain' 'Hello, Sinatra!'end

http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m

Page 18: Rails 2.3 and Rack

Rack

Object.call(env)

[status, headers, response]

Page 19: Rails 2.3 and Rack

Metal Endpoint

## app/metal/hello_metal.rbclass HelloMetal def self.call(env) if env["PATH_INFO"] =~ /^\/hello\/metal/ [200, {"Content-Type" => "text/plain"}, ["Hello, Metal!"]] else [404, {"Content-Type" => "text/html"}, ["Not Found"]] end endend

Page 20: Rails 2.3 and Rack

call(env)

Web Server

Middleware

Middleware

Rails Dispatcher

call(env)

call(env) [s,h,r]

[s,h,r]

[s,h,r]

Rack Middleware in Rails

Page 21: Rails 2.3 and Rack

Rack Middleware in Rails% rake middlewareuse Rack::Lockuse ActionController::Failsafeuse ActionController::Session::CookieStoreuse Rails::Rack::Metaluse ActionController::RewindableInputuse ActionController::ParamsParseruse Rack::MethodOverrideuse Rack::Headuse ActiveRecord::QueryCacherun ActionController::Dispatcher.new

Page 22: Rails 2.3 and Rack

% rake middlewareuse Rack::Lockuse ActionController::Failsafeuse ActionController::Session::CookieStoreuse Rails::Rack::Metaluse ActionController::RewindableInputuse ActionController::ParamsParseruse Rack::MethodOverrideuse Rack::Headuse ActiveRecord::QueryCacherun ActionController::Dispatcher.new

Rack Middleware in Rails

Page 23: Rails 2.3 and Rack

Django > Rails?## lib/middleware/django_middleware.rbclass DjangoMiddleware def initialize(app) @app = app end

def call(env) status, headers, response = @app.call(env) new_response = [] response.each do |part| new_response << part.gsub(/Rails/, 'Django') end [status, headers, new_response] endend

## config/environment.rbconfig.middleware.use DjangoMiddleware

Page 24: Rails 2.3 and Rack

http://github.com/bturnbull/bturnbull-metal-demo