Rails 2.3 and Rack - NHRuby Feb 2009

1,113 views
1,053 views

Published on

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

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

No Downloads
Views
Total views
1,113
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Rails 2.3 and Rack - NHRuby Feb 2009

  1. 1. Rails 2.3 Brian Turnbull http://brianturnbull.com
  2. 2. Templates rails app rails app’ template rails app -m template.rb rails app -m http://example.com/template rake rails:template LOCATION=template.rb
  3. 3. Templates # template.rb gem quot;hpricotquot; rake quot;gems:installquot; run quot;rm public/index.htmlquot; generate :scaffold, quot;person name:stringquot; route quot;map.root :controller => 'people'quot; rake quot;db:migratequot; git :init git :add => quot;.quot; git :commit => quot;-a -m 'Initial commit'quot; Pratik Naik — http://m.onkey.org/2008/12/4/rails-templates
  4. 4. Templates Jeremy McAnally’s Template Library http://github.com/jeremymcanally/rails-templates/tree/master
  5. 5. 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/
  6. 6. 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/
  7. 7. Nested Transactions User.transaction do User.create(:username => 'Admin') User.transaction(:requires_new => true) do User.create(:username => 'Regular') raise ActiveRecord::Rollback end end User.find(:all) # => Returns only Admin http://guides.rubyonrails.org/2_3_release_notes.html
  8. 8. Nested Attributes class Book < ActiveRecord::Base has_one :author has_many :pages accepts_nested_attributes_for :author, :pages end http://guides.rubyonrails.org/2_3_release_notes.html
  9. 9. Nested Forms class Book < ActiveRecord::Base has_many :authors accepts_nested_attributes_for :authors end <% 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
  10. 10. Dynamic and Default Scopes ## id Integer ## customer_id Integer ## status String ## entered_at DateTime class 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 => quot;status = 'open'quot;) Order.scoped_by_customer_id(12).scoped_by_status(quot;openquot;) http://guides.rubyonrails.org/2_3_release_notes.html
  11. 11. Other Changes Multiple Conditions for Callbacks HTTP Digest Authentication Lazy Loaded Sessions Localized Views and More! http://guides.rubyonrails.org/2_3_release_notes.html
  12. 12. Rails 2.3
  13. 13. Rails Metal
  14. 14. SPEED Simplicity
  15. 15. Metal Endpoint ## app/metal/hello_metal.rb class HelloMetal def self.call(env) if env[quot;PATH_INFOquot;] =~ /^/hello/metal/ [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hello, Metal!quot;]] else [404, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Not Foundquot;]] end end end http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
  16. 16. Equivalent Controller ## app/controllers/hello_rails_controller.rb class HelloRailsController < ApplicationController def show headers['Content-Type'] = 'text/plain' render :text => 'Hello, Rails!' end end http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
  17. 17. 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
  18. 18. Rack Object.call(env) [status, headers, response]
  19. 19. Metal Endpoint ## app/metal/hello_metal.rb class HelloMetal def self.call(env) if env[quot;PATH_INFOquot;] =~ /^/hello/metal/ [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hello, Metal!quot;]] else [404, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Not Foundquot;]] end end end
  20. 20. Rack Middleware in Rails Rails Dispatcher call(env) [s,h,r] Middleware call(env) [s,h,r] Middleware call(env) [s,h,r] Web Server
  21. 21. Rack Middleware in Rails % rake middleware use Rack::Lock use ActionController::Failsafe use ActionController::Session::CookieStore use Rails::Rack::Metal use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache run ActionController::Dispatcher.new
  22. 22. Rack Middleware in Rails % rake middleware use Rack::Lock use ActionController::Failsafe use ActionController::Session::CookieStore use Rails::Rack::Metal use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache run ActionController::Dispatcher.new
  23. 23. Django > Rails? ## lib/middleware/django_middleware.rb class 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] end end ## config/environment.rb config.middleware.use DjangoMiddleware
  24. 24. http://github.com/bturnbull/bturnbull-metal-demo

×