SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
Beautiful Code in Rails 3
         by
 Gregg Pollack


Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
Starting a New App
$ rails
Usage:
  rails APP_PATH [options]

Options:
  ‐O, [‐‐skip‐activerecord]  # Skip ActiveRecord files
  ‐r, [‐‐ruby=PATH]          # Path to the Ruby binary of your choice
                             # Default: /Users/greggpollack/.rvm/rubies/ruby‐
  ‐T, [‐‐skip‐testunit]      # Skip TestUnit files
      [‐‐dev]                # Setup the application with Gemfile pointing
                               to your rails checkout
  ‐J, [‐‐skip‐prototype]     # Skip Prototype files
  ‐G, [‐‐skip‐git]           # Skip Git ignores and keeps
  ‐m, [‐‐template=TEMPLATE]  # Path to an application template 
  ‐d, [‐‐database=DATABASE]  # Preconfigure for selected database 
      [‐‐edge]               # Setup the application with Gemfile
                             # pointing to Rails repository
 
$ rails test_app
      create                      $ ls script/
      create  README
      create  .gitignore
                                    
      ...
$ cd test_app/
                                    rails
$ rails
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short‐cut alias: "g")
                                                        c
 console     Start the Rails console (short‐cut alias: " ")
                                                        s
 server      Start the Rails server (short‐cut alias: " ")
 dbconsole   Start a console for the database specified in config/database.yml
            (short‐cut alias: "db")
In addition to those, there are:
 application  Generate the Rails application code
 destroy      Undo code generated with "generate"
 benchmarker  See how fast a piece of code runs
 profiler     Get profile information from a piece of code
 plugin       Install a plugin
 runner       Run a piece of code in the application environment

All commands can be run with ‐h for more information.
old scripts   new hotness

script/generate        rails g



script/console         rails c



 script/server         rails s



script/dbconsole       rails db
old scripts   new hotness

script/generate           r g



script/console            r c



 script/server            r s



script/dbconsole          r db

        alias r='rails'
New Router API
New Routing API



config/routes.rb
   TestApp::Application.routes.draw do |map|

      map.resources :posts

   end
New Routing API



config/routes.rb
   TestApp::Application.routes.draw do |map|

     resources :posts

   end
New Routing API
                        Rails 2
map.resources :posts do |post|
  post.resources :comments
end




                        Rails 3
resources :posts do
  resources :comments
end
New Routing API
                                                                                      Rails 2
map.resources :posts, :member => { :confirm => :post, :notify => :post } do |post|
  post.resources :comments, :member => { :preview => :post }, :collection => { :archived => :get }
end




                        Rails 3
       resources :posts do
         member do                                                               Rails 3
           post :confirm
                                                   resources :posts do
           get :notify
                                                     member do
         end
                                                       post :confirm
                                                       get :notify
          resources :comments do
                                                     end
            member do
              post :preview
                                                     resources :comments do
            end
                                                       post :preview, :on => :member
                                                       get :archived, :on => :collection
           collection do
                                                     end
             get :archived
                                                   end
           end
         end
       end
Rails 2
map.connect 'login', :controller => 'session', :action => 'new'




                      Rails 3
match 'login' => 'session#new'




Named Route         login_path
                                                    Rails 2
map.login 'login', :controller => 'session', :action => 'new'




                      Rails 3
match 'login' => 'session#new', :as => :login
New Routing API
                            Rails 2
map.root :controller => "users"




                    Rails 3
root :to => "users#index"




Legacy Route                           Rails 2
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'




                                      Rails 3
match ':controller(/:action(/:id(.:format)))'

               (commented out by default)
For more information
             http://guides.rails.info/routing.html
Beautiful Code in Rails 3


Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
                                  #Rails3OMGPonies!
New ActionController Syntax
Regular Syntax
 class UsersController < ApplicationController

  def index
    @users = User.all

    respond_to do |format|
      format.html
      format.xml { render :xml => @users.to_xml }
    end
  end

   def show
     @user = User.find(params[:id])

     respond_to do |format|
       format.html # show.html.erb
       format.xml { render :xml => @user }
     end
   end
 ...
New ActionController Syntax
Improved Syntax
 class UsersController < ApplicationController
   respond_to :html, :xml, :json

  def index
    @users = User.all
    respond_with(@users)
  end

   def show
     @user = User.find(params[:id])
     respond_with(@user)
   end
 ...
Mikel L indsaar
                  ActionMailer
New ActionMailer Syntax


                                           Rails 2
$script/generate mailer UserMailer welcome forgot_password

                 create  app/models/user_mailer.rb




                                        Rails 3
$r g mailer UserMailer welcome forgot_password

                 create  app/mailers/user_mailer.rb
New ActionMailer Syntax

                                           Rails 2
def welcome(user, subdomain)
  subject 'Welcome to TestApp'
  recipients user.email
  from 'admin@testapp.com'

  body :user => user, :subdomain => subdomain
end


              UserMailer.deliver_welcome(user, subdomain)

                                           Rails 3
def welcome(user, subdomain)
  @user = user
  @subdomain = subdomain

  mail(:from => "admin@testapp.com",
       :to => user.email,
       :subject => "Welcome to TestApp")
end


               UserMailer.welcome(user, subdomain).deliver
New ActionMailer Syntax

                                                                  Rails 3
class UserMailer < ActionMailer::Base

 default :from => "admin@testapp.com"

  def welcome(user, subdomain)
    @user = user
    @subdomain = subdomain

      attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf")

    mail(:to => @user.email, :subject => "Welcome to TestApp") do |format|
      format.html { render 'other_html_welcome' }
      format.text { render 'other_text_welcome' }
    end
  end

end


                                      welcome.text.erb
                        Defaults      welcome.html.erb
Nic k Kallen
               ActiveRelation




                replaces the internal ad-hoc query generation with
                query generation based on relational algebra.
ActiveRelation
                                          Rails 2
@posts = Post.find(:all, :conditions => {:published => true})


           immediately queries the db
           returns an Array of Posts


                                       Rails 3
@posts = Post.where(:published => true)


           doesn’t query the db
           returns an ActiveRecord::Relation
ActiveRelation


@posts = Post.where(:published => true)

if params[:order]
  @posts = @posts.order(params[:order])
end

@posts.each do |p|
    ...
                                  Que
end                                       ry r
                                              uns
                                                    here
           Lazy Loading
We can refactor!
@posts = Post.where(:published => true)

if params[:order]
  @posts = @posts.order(params[:order])
end


@posts = Post.where(:published => true)

@posts = @posts.order(params[:order])



@posts = Post.where(:published => true).order(params[:order])
ActiveRelations can be Shared
@posts = Post.where(:published => true).order(params[:order])



posts = Post.order(params[:order])

@published = posts.where(:published => true)
@unpublished = posts.where(:published => false)


    This is obviously a bad example (should be using named routes)

@published = Post.published
@unpublished = Post.unpublished
ActiveRelation
@published = Post.published
@unpublished = Post.unpublished

                                                   Rails 2
class Post < ActiveRecord::Base
  default_scope :order => 'title'

  named_scope :published, :conditions => {:published => true}
  named_scope :unpublished, :conditions => {:published => false}
end


                                                   Rails 3
class Post < ActiveRecord::Base
  default_scope order('title')

  scope :published, where(:published => true)
  scope :unpublished, where(:published => false)
end
ActiveRelation
    New Finder Methods
where(:conditions)
having(:conditions)
select
group
order
limit
offset
joins
includes(:include)
lock
readonly
from
ActiveRelation


                                                        Rails 2
Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments,
          :order => "title", :limit => 10)




                                                        Rails 3
Post.where(:author => "Joe").include(:comments).order(:title).limit(10)


 Remember, this version doesn’t do the query immediately
Beautiful Code in Rails 3


Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
                                  #Rails3OMGPonies!
Use of external libraries



    ActiveRecord                            ActiveRelation



      ActionView                                 Erubis



Erubis is a fast, secure, and very extensible implementation of ERB
Cross-Site Scripting (XSS)




            Rails 2                         Rails 2
<%= @post.body %>                <%= h @post.body %>

     (unsafe)                           (safe)

                 Rails 3                         Rails 3
<%= raw @post.body %>               <%= @post.body %>

      (unsafe)                            (safe)
Adopting Unobtrusive Javascript

HTML 5 custom data attributes                                   data-*
Custom data attributes are intended to store custom data private to the page or
  application, for which there are no more appropriate attributes or elements




     data-remote



     data-method



    data-confirm



    data-disable-with
Adopting Unobtrusive Javascript

                                        Rails 2
<%= link_to_remote 'Show', :url => post %>

          <a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true,
          evalScripts:true, parameters:'authenticity_token=' +
          encodeURIComponent('9sk..44d')}); return false;">Show</a>




                                             Rails 3
<%= link_to 'Show', post, :remote => true %>

           <a href="/posts/1" data-remote="true">Show</a>
Adopting Unobtrusive Javascript

                                    Rails 2
 <% remote_form_for(@post) do |f| %>


              <form action="/posts" class="new_post" id="new_post" method="post"
              onsubmit="new Ajax.Request('/posts', {asynchronous:true,
              evalScripts:true, parameters:Form.serialize(this)}); return false;">




                                                   Rails 3
  <% form_for(@post, :remote => true) do |f| %>

<form action="/posts" class="new_post" data-remote="true" id="new_post" method="post">
Adopting Unobtrusive Javascript

                                  Rails 2        Rails 3
 <%= link_to 'Destroy', post, :method => :delete %>

<a href="/posts/1" onclick="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);var s = document.createElement('input');
s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value',
'9skdJ0k+l9/q3PWToz6MtfyiB2gcyhnKubeGV6WFL44='); f.appendChild(s);f.submit();return false;">Destroy</a>




<a href="/posts/1" data-method="delete" rel="nofollow">Destroy</a>
Adopting Unobtrusive Javascript

                                                                             Rails 2 Rails 3
 <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>


 <a href="/posts/1" 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);var s = document.createElement('input');
 s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value',
 '9skdJ0k+l9/q3PWToz6MtfyiB2gcyhnKubeGV6WFL44='); f.appendChild(s);f.submit(); };return false;">Destroy</a>




<a href="/posts/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
Adopting Unobtrusive Javascript

                                                                       Rails 2
  <%= f.submit 'Create Post', :disable_with => "Please wait..." %>


                 <input id="post_submit" name="commit" onclick="if (window.hiddenCommit)
                 { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit =
                 document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value =
                 this.value;hiddenCommit.name =
                 this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue',
                 this.value);this.disabled = true;this.value='Please wait...';result =
                 (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) :
                 this.form.submit());if (result == false) { this.value =
                 this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit"
                 value="Create Post" />



                                                         Rails 3
  <%= f.submit :disable_with => "Please wait..." %>


<input data-disable-with="Please wait..."
                    id="post_submit" name="commit" type="submit" value="Create Post" />


             Notice, the button label
            has an intellegent default
Adopting Unobtrusive Javascript


HTML 5 custom data attributes

  data-remote



  data-method



  data-confirm



  data-disable-with
/public/stylesheets/rails.js
document.observe("dom:loaded", function() {

  $(document.body).observe("click", function(event) {

   var message = event.element().readAttribute('data-confirm');
   if (message) {
     // ... Do a confirm box
   }

   var element = event.findElement("a[data-remote=true]");
   if (element) {
     // ... Do the AJAX call
   }

   var element = event.findElement("a[data-method]");
   if (element) {
     // ... Create a form
   }
 });
jQuery in Rails?
http://github.com/rails/jquery-ujs




 $('a[data-confirm],input[data-confirm]').live('click', function () {
     // ... Do a confirm box
 });

 $('form[data-remote="true"]').live('submit', function (e) {
     // ... Do an AJAX call
 });
Deprecated Methods
   link_to_remote
   remote_form_for
   observe_field
   observe_form
   form_remote_tag
   button_to_remote
   submit_to_remote
   link_to_function
   periodically_call_remote


prototype_legacy_helper
      http://github.com/rails/prototype_legacy_helper
Beautiful Code in Rails 3


Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
                                  #Rails3OMGPonies!
Missing (that I didn’t have time for)
                            APIs

                                  Bundler
   http://railscasts.com/episodes/201-bundler



Making Generators with Thor
                  http://bit.ly/rails3generators
Creative Commons
            name                     author                                URL
rainbow of 80s toys       merwing✿little dear   http://www.flickr.com/photos/merwing/2152164258/

Old Loc                   Merlijn Hoek          http://www.flickr.com/photos/merlijnhoek/1040997599/

Notting Hill Gate         Eole                  http://www.flickr.com/photos/eole/942309733/

Britomart Train Station   EssjayNZ              http://www.flickr.com/photos/essjay/260511465/

Das Licht                 Small                 http://www.flickr.com/photos/small/62713023/

Metro Genova              opti mystic           http://www.flickr.com/photos/miiilio/2503634282/

Immobility Dilemna        gilderic              http://www.flickr.com/photos/gilderic/3528157964/

train station             nolifebeforecoffee    http://www.flickr.com/photos/nolifebeforecoffee/1803584805/

Mystical station          Jsome1                http://www.flickr.com/photos/jsome1/2226394415/

Railswaystation           Pieter Musterd        http://www.flickr.com/photos/piet_musterd/2233025691/

The Handover              MarkyBon              http://www.flickr.com/photos/markybon/152769885/

EN57                      magro_kr              http://www.flickr.com/photos/iks_berto/1328682171/
Presentation by:         If you need help with a Rails 3
                            project, feel free to give us a call




    http://envylabs.com
                               Come to our Rails 3 training at
                                     Railsconf 2010
    Gregg Pollack
                                   http://bit.ly/rails3ropes
      407-754-5517
 Gregg@EnvyLabs.com
                             We’re also available to run Rails 3
                             Training at your company, email:
     Ruby5 Podcast               Gregg@EnvyLabs.com
http://ruby5.envylabs.com

Weitere ähnliche Inhalte

Was ist angesagt?

REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101Samantha Geitz
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发shaokun
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2RORLAB
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwanTse-Ching Ho
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Ryan Weaver
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2RORLAB
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - IntroductionVagmi Mudumbai
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackIgnacio Martín
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your AppLuca Mearelli
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreSymfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreRyan Weaver
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2RORLAB
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCalderaLearn
 
The road to Ember.js 2.0
The road to Ember.js 2.0The road to Ember.js 2.0
The road to Ember.js 2.0Codemotion
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 Connecting Content Silos: One CMS, Many Sites With The WordPress REST API Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Connecting Content Silos: One CMS, Many Sites With The WordPress REST APICaldera Labs
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideVisual Engineering
 

Was ist angesagt? (20)

REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Getting Started-with-Laravel
Getting Started-with-LaravelGetting Started-with-Laravel
Getting Started-with-Laravel
 
Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Workshop 17: EmberJS parte II
Workshop 17: EmberJS parte IIWorkshop 17: EmberJS parte II
Workshop 17: EmberJS parte II
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your App
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreSymfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
The road to Ember.js 2.0
The road to Ember.js 2.0The road to Ember.js 2.0
The road to Ember.js 2.0
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 Connecting Content Silos: One CMS, Many Sites With The WordPress REST API Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 

Andere mochten auch

Skinny Controllers, Skinny Models
Skinny Controllers, Skinny ModelsSkinny Controllers, Skinny Models
Skinny Controllers, Skinny ModelsKazuhiro Sera
 
Comment répondre à la demande contraceptive?
Comment répondre à la demande contraceptive?Comment répondre à la demande contraceptive?
Comment répondre à la demande contraceptive?Didier Cosserat
 
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)Camille Roux
 
テストの運用について #m3dev
テストの運用について #m3devテストの運用について #m3dev
テストの運用について #m3devKazuhiro Sera
 
10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer ExperienceYuan Wang
 

Andere mochten auch (6)

Skinny Controllers, Skinny Models
Skinny Controllers, Skinny ModelsSkinny Controllers, Skinny Models
Skinny Controllers, Skinny Models
 
Comment répondre à la demande contraceptive?
Comment répondre à la demande contraceptive?Comment répondre à la demande contraceptive?
Comment répondre à la demande contraceptive?
 
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)
Pourquoi Ruby on Rails est génial? (d'un point de vue non technique)
 
テストの運用について #m3dev
テストの運用について #m3devテストの運用について #m3dev
テストの運用について #m3dev
 
HTML CSS Basics
HTML CSS BasicsHTML CSS Basics
HTML CSS Basics
 
10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience10 Insightful Quotes On Designing A Better Customer Experience
10 Insightful Quotes On Designing A Better Customer Experience
 

Ähnlich wie Rails 3 Beautiful Code

Les nouveautés de Rails 3
Les nouveautés de Rails 3Les nouveautés de Rails 3
Les nouveautés de Rails 3LINAGORA
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 
Rails3 for Rails2 developers
Rails3 for Rails2 developersRails3 for Rails2 developers
Rails3 for Rails2 developersalkeshv
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2RORLAB
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Prxibbar
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentNicolas Ledez
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
Rails 3 (beta) Roundup
Rails 3 (beta) RoundupRails 3 (beta) Roundup
Rails 3 (beta) RoundupWayne Carter
 
Ruby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewRuby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewLibin Pan
 
Carlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 EnCarlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 EnCasey Bradford
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Clinton Dreisbach
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortegaarman o
 
Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Plataformatec
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuLucas Renan
 

Ähnlich wie Rails 3 Beautiful Code (20)

Les nouveautés de Rails 3
Les nouveautés de Rails 3Les nouveautés de Rails 3
Les nouveautés de Rails 3
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 
Rails3 for Rails2 developers
Rails3 for Rails2 developersRails3 for Rails2 developers
Rails3 for Rails2 developers
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Merb
MerbMerb
Merb
 
Rail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendranRail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendran
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Rails 3 (beta) Roundup
Rails 3 (beta) RoundupRails 3 (beta) Roundup
Rails 3 (beta) Roundup
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Ruby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewRuby on Rails 2.1 What's New
Ruby on Rails 2.1 What's New
 
Carlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 EnCarlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 En
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
 

Kürzlich hochgeladen

All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 

Kürzlich hochgeladen (20)

All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 

Rails 3 Beautiful Code

  • 1. Beautiful Code in Rails 3 by Gregg Pollack Starting a new app New Router API ActionController - respond_with ActionMailer Syntax ActiveRelation (arel) ERB Strings Escaped Unobtrusive Javascript
  • 2. Starting a New App $ rails Usage:   rails APP_PATH [options] Options: ‐O, [‐‐skip‐activerecord]  # Skip ActiveRecord files   ‐r, [‐‐ruby=PATH]          # Path to the Ruby binary of your choice                              # Default: /Users/greggpollack/.rvm/rubies/ruby‐   ‐T, [‐‐skip‐testunit]      # Skip TestUnit files       [‐‐dev]                # Setup the application with Gemfile pointing                                to your rails checkout ‐J, [‐‐skip‐prototype]     # Skip Prototype files   ‐G, [‐‐skip‐git]           # Skip Git ignores and keeps   ‐m, [‐‐template=TEMPLATE]  # Path to an application template    ‐d, [‐‐database=DATABASE]  # Preconfigure for selected database        [‐‐edge]               # Setup the application with Gemfile                              # pointing to Rails repository  
  • 3. $ rails test_app       create   $ ls script/       create  README       create  .gitignore          ... $ cd test_app/   rails $ rails Usage: rails COMMAND [ARGS] The most common rails commands are:  generate    Generate new code (short‐cut alias: "g") c console     Start the Rails console (short‐cut alias: " ") s server      Start the Rails server (short‐cut alias: " ") dbconsole   Start a console for the database specified in config/database.yml             (short‐cut alias: "db") In addition to those, there are:  application  Generate the Rails application code  destroy      Undo code generated with "generate"  benchmarker  See how fast a piece of code runs  profiler     Get profile information from a piece of code  plugin       Install a plugin  runner       Run a piece of code in the application environment All commands can be run with ‐h for more information.
  • 4. old scripts new hotness script/generate rails g script/console rails c script/server rails s script/dbconsole rails db
  • 5. old scripts new hotness script/generate r g script/console r c script/server r s script/dbconsole r db alias r='rails'
  • 7. New Routing API config/routes.rb TestApp::Application.routes.draw do |map| map.resources :posts end
  • 8. New Routing API config/routes.rb TestApp::Application.routes.draw do |map| resources :posts end
  • 9. New Routing API Rails 2 map.resources :posts do |post| post.resources :comments end Rails 3 resources :posts do resources :comments end
  • 10. New Routing API Rails 2 map.resources :posts, :member => { :confirm => :post, :notify => :post } do |post| post.resources :comments, :member => { :preview => :post }, :collection => { :archived => :get } end Rails 3 resources :posts do member do Rails 3 post :confirm resources :posts do get :notify member do end post :confirm get :notify resources :comments do end member do post :preview resources :comments do end post :preview, :on => :member get :archived, :on => :collection collection do end get :archived end end end end
  • 11. Rails 2 map.connect 'login', :controller => 'session', :action => 'new' Rails 3 match 'login' => 'session#new' Named Route login_path Rails 2 map.login 'login', :controller => 'session', :action => 'new' Rails 3 match 'login' => 'session#new', :as => :login
  • 12. New Routing API Rails 2 map.root :controller => "users" Rails 3 root :to => "users#index" Legacy Route Rails 2 map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' Rails 3 match ':controller(/:action(/:id(.:format)))' (commented out by default)
  • 13. For more information http://guides.rails.info/routing.html
  • 14. Beautiful Code in Rails 3 Starting a new app New Router API ActionController - respond_with ActionMailer Syntax ActiveRelation (arel) ERB Strings Escaped Unobtrusive Javascript #Rails3OMGPonies!
  • 15. New ActionController Syntax Regular Syntax class UsersController < ApplicationController def index @users = User.all respond_to do |format| format.html format.xml { render :xml => @users.to_xml } end end def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @user } end end ...
  • 16. New ActionController Syntax Improved Syntax class UsersController < ApplicationController respond_to :html, :xml, :json def index @users = User.all respond_with(@users) end def show @user = User.find(params[:id]) respond_with(@user) end ...
  • 17. Mikel L indsaar ActionMailer
  • 18. New ActionMailer Syntax Rails 2 $script/generate mailer UserMailer welcome forgot_password create  app/models/user_mailer.rb Rails 3 $r g mailer UserMailer welcome forgot_password create  app/mailers/user_mailer.rb
  • 19. New ActionMailer Syntax Rails 2 def welcome(user, subdomain) subject 'Welcome to TestApp' recipients user.email from 'admin@testapp.com' body :user => user, :subdomain => subdomain end UserMailer.deliver_welcome(user, subdomain) Rails 3 def welcome(user, subdomain) @user = user @subdomain = subdomain mail(:from => "admin@testapp.com", :to => user.email, :subject => "Welcome to TestApp") end UserMailer.welcome(user, subdomain).deliver
  • 20. New ActionMailer Syntax Rails 3 class UserMailer < ActionMailer::Base default :from => "admin@testapp.com" def welcome(user, subdomain) @user = user @subdomain = subdomain attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf") mail(:to => @user.email, :subject => "Welcome to TestApp") do |format| format.html { render 'other_html_welcome' } format.text { render 'other_text_welcome' } end end end welcome.text.erb Defaults welcome.html.erb
  • 21. Nic k Kallen ActiveRelation replaces the internal ad-hoc query generation with query generation based on relational algebra.
  • 22. ActiveRelation Rails 2 @posts = Post.find(:all, :conditions => {:published => true}) immediately queries the db returns an Array of Posts Rails 3 @posts = Post.where(:published => true) doesn’t query the db returns an ActiveRecord::Relation
  • 23. ActiveRelation @posts = Post.where(:published => true) if params[:order] @posts = @posts.order(params[:order]) end @posts.each do |p| ... Que end ry r uns here Lazy Loading
  • 24. We can refactor! @posts = Post.where(:published => true) if params[:order] @posts = @posts.order(params[:order]) end @posts = Post.where(:published => true) @posts = @posts.order(params[:order]) @posts = Post.where(:published => true).order(params[:order])
  • 25. ActiveRelations can be Shared @posts = Post.where(:published => true).order(params[:order]) posts = Post.order(params[:order]) @published = posts.where(:published => true) @unpublished = posts.where(:published => false) This is obviously a bad example (should be using named routes) @published = Post.published @unpublished = Post.unpublished
  • 26. ActiveRelation @published = Post.published @unpublished = Post.unpublished Rails 2 class Post < ActiveRecord::Base default_scope :order => 'title' named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false} end Rails 3 class Post < ActiveRecord::Base default_scope order('title') scope :published, where(:published => true) scope :unpublished, where(:published => false) end
  • 27. ActiveRelation New Finder Methods where(:conditions) having(:conditions) select group order limit offset joins includes(:include) lock readonly from
  • 28. ActiveRelation Rails 2 Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10) Rails 3 Post.where(:author => "Joe").include(:comments).order(:title).limit(10) Remember, this version doesn’t do the query immediately
  • 29. Beautiful Code in Rails 3 Starting a new app New Router API ActionController - respond_with ActionMailer Syntax ActiveRelation (arel) ERB Strings Escaped Unobtrusive Javascript #Rails3OMGPonies!
  • 30. Use of external libraries ActiveRecord ActiveRelation ActionView Erubis Erubis is a fast, secure, and very extensible implementation of ERB
  • 31. Cross-Site Scripting (XSS) Rails 2 Rails 2 <%= @post.body %> <%= h @post.body %> (unsafe) (safe) Rails 3 Rails 3 <%= raw @post.body %> <%= @post.body %> (unsafe) (safe)
  • 32. Adopting Unobtrusive Javascript HTML 5 custom data attributes data-* Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements data-remote data-method data-confirm data-disable-with
  • 33. Adopting Unobtrusive Javascript Rails 2 <%= link_to_remote 'Show', :url => post %> <a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('9sk..44d')}); return false;">Show</a> Rails 3 <%= link_to 'Show', post, :remote => true %> <a href="/posts/1" data-remote="true">Show</a>
  • 34. Adopting Unobtrusive Javascript Rails 2 <% remote_form_for(@post) do |f| %> <form action="/posts" class="new_post" id="new_post" method="post" onsubmit="new Ajax.Request('/posts', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> Rails 3 <% form_for(@post, :remote => true) do |f| %> <form action="/posts" class="new_post" data-remote="true" id="new_post" method="post">
  • 35. Adopting Unobtrusive Javascript Rails 2 Rails 3 <%= link_to 'Destroy', post, :method => :delete %> <a href="/posts/1" onclick="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);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', '9skdJ0k+l9/q3PWToz6MtfyiB2gcyhnKubeGV6WFL44='); f.appendChild(s);f.submit();return false;">Destroy</a> <a href="/posts/1" data-method="delete" rel="nofollow">Destroy</a>
  • 36. Adopting Unobtrusive Javascript Rails 2 Rails 3 <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %> <a href="/posts/1" 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);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', '9skdJ0k+l9/q3PWToz6MtfyiB2gcyhnKubeGV6WFL44='); f.appendChild(s);f.submit(); };return false;">Destroy</a> <a href="/posts/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
  • 37. Adopting Unobtrusive Javascript Rails 2 <%= f.submit 'Create Post', :disable_with => "Please wait..." %> <input id="post_submit" name="commit" onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Please wait...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit" value="Create Post" /> Rails 3 <%= f.submit :disable_with => "Please wait..." %> <input data-disable-with="Please wait..." id="post_submit" name="commit" type="submit" value="Create Post" /> Notice, the button label has an intellegent default
  • 38. Adopting Unobtrusive Javascript HTML 5 custom data attributes data-remote data-method data-confirm data-disable-with
  • 39. /public/stylesheets/rails.js document.observe("dom:loaded", function() { $(document.body).observe("click", function(event) { var message = event.element().readAttribute('data-confirm'); if (message) { // ... Do a confirm box } var element = event.findElement("a[data-remote=true]"); if (element) { // ... Do the AJAX call } var element = event.findElement("a[data-method]"); if (element) { // ... Create a form } });
  • 40. jQuery in Rails? http://github.com/rails/jquery-ujs $('a[data-confirm],input[data-confirm]').live('click', function () { // ... Do a confirm box }); $('form[data-remote="true"]').live('submit', function (e) { // ... Do an AJAX call });
  • 41. Deprecated Methods link_to_remote remote_form_for observe_field observe_form form_remote_tag button_to_remote submit_to_remote link_to_function periodically_call_remote prototype_legacy_helper http://github.com/rails/prototype_legacy_helper
  • 42. Beautiful Code in Rails 3 Starting a new app New Router API ActionController - respond_with ActionMailer Syntax ActiveRelation (arel) ERB Strings Escaped Unobtrusive Javascript #Rails3OMGPonies!
  • 43. Missing (that I didn’t have time for) APIs Bundler http://railscasts.com/episodes/201-bundler Making Generators with Thor http://bit.ly/rails3generators
  • 44. Creative Commons name author URL rainbow of 80s toys merwing✿little dear http://www.flickr.com/photos/merwing/2152164258/ Old Loc Merlijn Hoek http://www.flickr.com/photos/merlijnhoek/1040997599/ Notting Hill Gate Eole http://www.flickr.com/photos/eole/942309733/ Britomart Train Station EssjayNZ http://www.flickr.com/photos/essjay/260511465/ Das Licht Small http://www.flickr.com/photos/small/62713023/ Metro Genova opti mystic http://www.flickr.com/photos/miiilio/2503634282/ Immobility Dilemna gilderic http://www.flickr.com/photos/gilderic/3528157964/ train station nolifebeforecoffee http://www.flickr.com/photos/nolifebeforecoffee/1803584805/ Mystical station Jsome1 http://www.flickr.com/photos/jsome1/2226394415/ Railswaystation Pieter Musterd http://www.flickr.com/photos/piet_musterd/2233025691/ The Handover MarkyBon http://www.flickr.com/photos/markybon/152769885/ EN57 magro_kr http://www.flickr.com/photos/iks_berto/1328682171/
  • 45. Presentation by: If you need help with a Rails 3 project, feel free to give us a call http://envylabs.com Come to our Rails 3 training at Railsconf 2010 Gregg Pollack http://bit.ly/rails3ropes 407-754-5517 Gregg@EnvyLabs.com We’re also available to run Rails 3 Training at your company, email: Ruby5 Podcast Gregg@EnvyLabs.com http://ruby5.envylabs.com