What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group December 2010
Upcoming SlideShare
Loading in...5
×
 

What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group December 2010

on

  • 3,437 views

The third talk I gave in December in Milwaukee at Bucketworks. See my blog at blog.mattgauger.com for more info and full blog posts.

The third talk I gave in December in Milwaukee at Bucketworks. See my blog at blog.mattgauger.com for more info and full blog posts.

Statistics

Views

Total Views
3,437
Views on SlideShare
2,872
Embed Views
565

Actions

Likes
1
Downloads
21
Comments
0

7 Embeds 565

http://blog.mattgauger.com 519
http://coderwall.com 25
http://mathias.github.com 9
http://localhost 7
http://posterous.com 2
http://www.netvibes.com 2
http://liveweb.archive.org 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group December 2010 What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group December 2010 Presentation Transcript

  • What’s new and great inRails 3
  • Don’t worry! All the links Text shown will bepresented at the end of the slides and on my blog.
  • There’s a lot to talk about, so
  • This is a longpresentation
  • I wanted to do something different thanlist the changes in Rails 3
  • Allow me to introduce you to someone.
  • This is Robin
  • “Robin would very much like to be akick-ass web developer, but has a hardtime keeping up with the buzzwords.” -- Flickr description
  • He’s a webdeveloper
  • He uses Rails 2.3 at his job,
  • where they make state-of-the-art TODO list web applications (and fight crime)
  • His boss
  • Batman
  • Wants the team to use Rails 3.
  • Because he heard itis the new hotness.
  • Batman tries to stay hip with the “web stuff”
  • Robin is already fairly familiar with Rails
  • In this case, he’s going to be starting from scratch
  • While it is possible to upgrade from Rails 2.x toRails 3, we’re not going to.
  • This is merely a learningexercise, so we’re skippingthe BDD/TDD for now, too.
  • Robin keeps up with a few Rails blogs,
  • And he’s read that rvmis what everyone is using
  • rvm is a way to install and use different versions ofRuby on the same machine
  • Robin uses Homebrewon his Mac, so he types:
  • $ brew install rvm
  • Sidenote:My recommendation would be to start with Cinderella if you’re on a Mac
  • What comes with Cinderella?
  • • mysql, postgres, redis, memcached andmongodb•ruby (1.8.7) with rails and sinatra viarvm gemsets. (& install Ruby 1.9 with rvm)•python (2.7) with pip.•node.js (0.2.0) with npm.•erlang (R13B04) environment.... and brew, and probably a lot more bynow.
  • With rvm, Robin can do this:
  • $ rvm install 1.9.2$ rvm use 1.9.2
  • $ rvm listrvm rubies ruby-1.8.7-p248 [ x86_64 ]=> ruby-1.9.2-p0 [ x86_64 ]
  • $ ruby -vruby 1.9.2p0 (2010-08-18 revision29036) [x86_64-darwin10.4.1]
  • So now Robin isrolling on Ruby 1.9.2
  • Next, he needs a gemset
  • A gemset is a collection ofgems, in this case that we’re associating with a project
  • Robin knows this comes in later, when he startsusing bundler in his app
  • $ rvm gemset create totodo$ rvm ruby-1.9.2-p0@totodo
  • Now any gems he installs will be on Ruby 1.9.2
  • And more importantly,will be kept with his app.
  • Robin installs Rails 3.0.3
  • $ gem install rails -v 3.0.3
  • And the Heroku gem
  • $ gem install heroku
  • He creates a new app called totodo (it’s a todo list!)
  • # Create a new Rails 3 app:$ rails new totodo$ cd totodo/
  • And grabs gems thatRails needs with Bundler
  • # Use Bundler to install gems:$ bundle install
  • Let’s look at Bundler
  • In Rails 2, Robinwould have to do this:
  • # Edit environment.rb:config.gem "haml"config.gem "sass"# Then do$ rake gems:install
  • In Rails 3 with Bundler:
  • # Edit the Gemfile:source "http://rubygems.org"gem "haml"gem "sass"# Then run:$ bundle install
  • We can also usegroups for gems:
  • # Gemfile:source "http://rubygems.org"gem "haml"gem "sass"group :test,:development do gem "webrat" gem "rspec"end# Then go run:$ bundle
  • Now webrat and rspec will be installed and used, butonly in the environments listed
  • Moving on, we setup git and heroku
  • # Set up git repo:$ git init .$ git add *$ git commit -m “Initial commit ofbare Rails 3 project.”# Create a new Heroku app:$ heroku create$ git push heroku master
  • Robin is all set to develop
  • He’s is looking at the files in his newly created project,
  • and noticing some differences
  • config/application.rb(replaces config/environment.rb)
  • require File.expand_path(../boot, __FILE__)require rails/all# If you have a Gemfile, require the gems listed there, including any gems# youve limited to :test, :development, or :production.Bundler.require(:default, Rails.env) if defined?(Bundler)module Totodo class Application < Rails::Application # [With a lot of comments cleaned up] # Configure generators values. config.generators do |g| g.orm :active_record g.template_engine :haml g.test_framework :rspec, :fixture => true end # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] endend
  • The important bit is
  • # Robin is going to use haml and rspec:# Configure generators values.config.generators do |g| g.orm :active_record g.template_engine :haml g.test_framework :rspecend
  • Next,
  • Robin needs a data model for what he’s storing
  • Which is a TODO list item
  • What makes up aTODO list item?
  • •ID•Title (Text)•Due Date (Datetime)•Is it done? (Boolean)
  • Robin wants to use $ script/generatewhich he comfortable with
  • But there isn’t ascript/generate in Rails 3
  • # Inside his totodo project folder:$ ls -al script/total 8drwxr-xr-x 3 robin staff 102 Dec 20 01:53 .drwxr-xr-x 21 robin staff 714 Dec 20 01:55 ..-rwxr-xr-x 1 robin staff 295 Dec 20 01:53 rails
  • Instead, everything goes through the rails script
  • # Remember doing these commands?$ script/generate something$ script/server$ script/console
  • # Replaced with:$ rails generate something$ rails g something #shorter form$ rails server$ rails s # shorter form$ rails console
  • What Robin wants is a scaffold
  • Again, he’s just learning Rails 3 here, so a scaffold will be fine.
  • $ rails generate scaffold Todo title:string due_date:datetime done:boolean
  • $ rails generate scaffold Todo title:string due_date:datetime done:boolean invoke active_record create db/migrate/20101220080933_create_todos.rb create app/models/todo.rb invoke test_unit create test/unit/todo_test.rb create test/fixtures/todos.yml route resources :todos invoke scaffold_controller create app/controllers/todos_controller.rb invoke erb create app/views/todos create app/views/todos/index.html.erb create app/views/todos/edit.html.erb create app/views/todos/show.html.erb create app/views/todos/new.html.erb create app/views/todos/_form.html.erb[and lots more output than this]
  • That’s a lot ofgenerated files
  • Robin wants to knowwhether migrations are different in Rails 3
  • So he looks in
  • db/migrate/20101220080933_create_todos.rb
  • class CreateTodos < ActiveRecord::Migration def self.up create_table :todos do |t| t.string :title t.datetime :due_date t.boolean :done t.timestamps end end def self.down drop_table :todos endend
  • That’s very similar towhat Robin is used to.
  • Robin runs the migration.
  • $ rake db:migrate(in /Users/robin/Projects/totodo)== CreateTodos: migrating ==================================-- create_table(:todos) -> 0.0019s== CreateTodos: migrated (0.0020s) =========================
  • Now, Robin wants to hook upthe routes to his new controller
  • He’s read that routes in Rails 3 are different,
  • but he hasn’t seen them yet
  • He’s pleased to findthat they’re simple:
  • # in config/routes.rb our scaffold placed:Totodo::Application.routes.draw do resources :todosend
  • Robin deletes thepublic/index.html file and maps the root path:
  • Totodo::Application.routes.draw do root :to => todos#index resources :todosend
  • And runs his dev server withthat nifty `rails s` command
  • Robin wants validations on his model
  • A ToDo list itemshould have a title
  • The syntax haschanged a little
  • class Todo < ActiveRecord::Base validates :title, :presence => trueend
  • Robin has a very,very basic app started
  • And a long way to go with Rails 3
  • Robin has a little bit oftime during his lunch break
  • So he reads up on the Merb project
  • 2006:The Merb project was started
  • At its start, Merb justserves ERb templates from Mongrel
  • Merb focuses on essentials
  • Componentmodularity
  • Extensible APIs
  • ORM/JS framework agnostic
  • December 2008:The Rails team announcesRoR and Merb will merge
  • Rails 3 is the result ofmerging Merb and RoR
  • And represents asignificant rewrite of most of Rails
  • Robin has stumbledonto a blog post by
  • Yehuda Katz
  • Summarizing theMerb+RoR merger back in 2008
  • Rails will become more modular
  • Rails will have better performance
  • Rails 3 will have a defined API, and atest suite for that API
  • Rails will allow things like DataMapper or Sequel to be first-class ORMs
  • Rails 3 will continue to embrace Rack
  • Reinvigorated, Robinjumps back into dev
  • As he begins to modifyhis controller, he runs into
  • ARel
  • (ActiveRelation)
  • Arel provides anSQL abstraction
  • Using relational algebra
  • Old and busted:
  • @users = User.find(:all, :conditions => {:approved => true})
  • New Hotness:
  • @users = User.where(:approved => true)
  • The Rails 2.x-style .find() method queries the DBimmediately and returns an array
  • The Arel-style .where() method returns anActiveRecord::Relation
  • What can we do with an ActiveRelation?
  • Add additionalparameters to the query
  • @users = User.where(:approved => true)# If a params[:order] was passed in,# add that condition to the query:@users = @users.order(params[:order])@users.each do |u| ...end
  • @users = User.where(:approved => true)# If a params[:order] was passed in,# add that condition to the query:@users = @users.order(params[:order])@users.each do |u| # Query runs here! ...end
  • We can even chain the last page of code together:
  • User.where(:approved => true).order(params[:order])
  • Rails 2:named_scope method
  • Rails 3:scope keyword
  • # Rails 2:class User < ActiveRecord::Base named_scope :approved, :conditions => {:approved => true}end# Rails 3:class User < ActiveRecord::Base scope :approved, where(:approved => true)end
  • There’s much, much more to Arel. Read the documentation.
  • Next up, Robin discovers that he can match any Rack app to a route
  • Totodo::Application.routes do match "/home", :to => HomeAppend
  • In fact, this is a lot likethe routes we saw earlier
  • Totodo::Application.routes.draw do root :to => todos#index match "/home", :to => HomeAppend
  • todos#indexhere is really a full fledgedRack application, too.
  • Routes are namespaced inside your app.
  • # Instead of:ActionController::Routing::Routes.draw do |map| map.resources :todosend# You do:Totodo::Application.routes do resources :todosend
  • The i18n gem brings in better i18n support, but Robin isn’tgoing to get into that right now.
  • How has ourController changed?
  • A simplified version of what our scaffold created would be:
  • class TodosController < ApplicationController respond_to :html, :xml, :js # GET /todos # GET /todos.xml def index respond_with(@todos = Todo.all) endend
  • As you can see, respond_to defines the formats the Controller responds to
  • And responds_with isthe data to send back
  • Our scaffold has alreadycreated the methods for our Todos controller
  • Robin pushes the app to Heroku
  • $ git push heroku master[... lots of output ...]-----> Launching... done http://deep-mountain-360.heroku.com/deployed to HerokuTo git@heroku.com:deep-mountain-360.git * [new branch] master -> master
  • Where can Robin go to learn more?
  • Robin can work through theRails Tutorial book for Rails 3: http://railstutorial.org/book
  • Which is a great resource. Highly recommended!
  • Robin is now equipped to fight crime, writeTODO lists, and learn more about Rails 3!
  • His boss wants to give him a little congrats
  • “POW”
  • Thank you!
  • See me after for free Github stickers :-)
  • Media used:http://www.flickr.com/photos/geirarne/43102913/(CC Attribution-NonCommercial-ShareAlike 2.0)http://www.flickr.com/photos/redraspus/2883658175(CC Attribution-NonCommercial-ShareAlike 2.0)http://www.flickr.com/photos/patolucas/2862975260/(CC Attribution-ShareAlike 2.0 Generic)
  • Links:https://github.com/atmos/cinderellahttp://rvm.beginrescueend.com/http://yehudakatz.com/2008/12/23/rails-and-merb-merge/http://railsforzombies.orghttp://railstutorial.org/bookhttp://rubyonrails.org/screencasts/rails3/https://github.com/brynary/arel