• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Les nouveautés de Rails 3
 

Les nouveautés de Rails 3

on

  • 1,694 views

Présentation donnée lors du salon Solutions Linux 2011.

Présentation donnée lors du salon Solutions Linux 2011.

Animée par Simon Courtois, Expert en Open Source

Statistics

Views

Total Views
1,694
Views on SlideShare
1,634
Embed Views
60

Actions

Likes
0
Downloads
7
Comments
0

2 Embeds 60

http://www.linagora.com 45
http://linagora.com 15

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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

    Les nouveautés de Rails 3 Les nouveautés de Rails 3 Presentation Transcript

    • Les nouveautés de Rails 3 Simon COURTOIS Expert en Open-Source scourtois@linagora.com 1 WWW.LINAGORA.COM
    • La commande rails Rails 2 Rails 3 rails myapp rails new myapp ./script/generate rails g ./script/console rails c ./script/server rails s ./script/dbconsole rails db2 /25
    • La commande rails • --skip-activerecord, -O .gitignore • --skip-test-unit, -T .bundle • --skip-prototype, -J db/*.sqlite3 log/*.log tmp/ • --skip-git, -G ???3 /25
    • Bundler Rails 2 Rails::Initializer.run do |config| ... config.gem haml config.gem coderay, :version => ~>0.9.7 ...end config/environment.rb Rails 3 source http://rubygems.orggem rails, 3.0.6gem hamlgem coderay, ~> 0.9.7group :development, :test do gem cucumber-rails end Gemfile bundle install4 /25
    • ActiveRelation Rails 2 @articles = Article.find(:all, :conditions => {:published => true}) ¬ requête immédiate ¬ retourne un tableau d’articles Rails 3 @articles = Article.where(:published => true) ¬ pas de requête ¬ retourne un objet ActiveRecord::Relation5 /25
    • ActiveRelation @articles = Article.where(:published => true) if params[:order] @articles = @articles.order(params[:order]) end @articles.each do |article| ... end requête effectuée6 /25
    • ActiveRelation @articles = Article.where(:published => true) @articles = @articles.order(params[:order]) @articles.each do |article| ... end7 /25
    • ActiveRelation @articles = Article.where(:published => true).order(params[:order]) @articles.each do |article| ... end8 /25
    • ActiveRelation articles = Article.order(params[:order]) @published = articles.where(:published => true) @unpublished = articles.where(:published => false)9 /25
    • ActiveRelation @published = articles.published @unpublished = articles.unpublished class Article < ActiveRecord::Base named_scope :published, :conditions => {:published => true} Rails 2 named_scope :unpublished, :conditions => {:published => false} end class Article < ActiveRecord::Base scope :published, where(:published => true) Rails 3 scope :unpublished, where(:published => false) end10/25
    • ActiveRelation where(:conditions) all having(:conditions) first select last group order limit offset joins includes(:include) lock readonly from11/25
    • ActiveRelation Rails 2 Article.find(:all, :conditions => {:author => “Bob”}, :includes => :comments, :order => “title”, :limit => 10) Rails 3 Article.where(:author => “Bob”).includes(:comments).order(“title”).limit(10).all12/25
    • ActiveController Rails 2 Rails 3class ArticlesController < ApplicationController class ArticlesController < Applic... def index respond_to :html, :xml @users = User.all def index respond_to do |format| @users = User.all format.html respond_with(@users) format.xml { render :xml => @users.to_xml } end end end def show @user = User.find(params[:id]) def show respond_with(@user) @user = User.find(params[:id]) end end respond_to do |format| format.html format.xml { render :xml => @user } end endendflash[:notice] = “Article created” redirect_to @article,redirect_to @article :notice => “Article created”13/25
    • Le routing Rails 2 Rails 3ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.resources :articles resources :articlesend end14/25
    • Le routing Rails 2 Rails 3ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.resources :articles, resources :articles do :member => { :preview => :post }, member do :collection => { :archived => :get } post :previewend end collection do get :archived end end end Myapp::Application.routes.draw do |map| resources :articles do post :preview, :on => :member get :archived, :on => :collection end end15/25
    • Le routing Rails 2 Rails 3 ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.resources :articles do |article| resources :articles do article.resources :comments resources :comments end end end end ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.connect “login”, match “login” => “session#new” :controller => “session”, end :action => “new” end16/25
    • Le routing Rails 2 Rails 3 ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.login “login”, match “login” => “session#new”, :as => :login :controller => “session”, end :action => “new” end ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.root :controller => “articles”, root :to => “users#index” :action => “index” end end ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.connect “:controller/:action/:id” match “:controller(/:action(/:id(.:format)))” map.connect “:controller/:action/:id.:format” end end17/25
    • Le routing Rails 2 ActionController::Routing::Routes.draw do |map| map.connect ‘/articles/:year/:month/:day’, :controller => “articles”, :action => “index” map.connect ‘/articles/:year/:month’, :controller => “articles”, :action => “index” map.connect ‘/articles/:year’, :controller => “articles”, :action => “index” end Rails 3 Myapp::Application.routes.draw do |map| match “/articles(/:year(/:month(/:day)))” => “articles#index” end18/25
    • Le routing Rails 2 ActionController::Routing::Routes.draw do |map| map.connect ‘/articles/:year’, :controller => “articles”, :action => “index”, :conditions => { :method => :get } end Rails 3 Myapp::Application.routes.draw do |map| match “/articles/:year” => “articles#index”, :via => :get end Myapp::Application.routes.draw do |map| get “/articles/:year” => “articles#index” end19/25
    • Le routing Rails 3 Myapp::Application.routes.draw do |map| match “signin”, :to => redirect(“/login”) match “linagora”, :to => redirect(“http://www.linagora.com”) end Myapp::Application.routes.draw do |map| get “hello” => proc { |env| [200, {}, “Hello World !”] } get “rack_app” => MyCoolRackApp end RAILS_ROOT/lib/my_cool_rack_app.rb20/25
    • XSS et Unobstrusive JS Rails 2 Rails 3 <%= @article.title %> # Non sécurisé <%= @article.title %> # Sécurisé <%=h @article.title %> # Sécurisé <%=raw @article.title %> # Non sécurisé <%= link_to_remote “Show”, :url => @article %> <%= link_to “Show”, :remote => true %><a href=”#” onclick=”new <a href=”/articles/1” data-remote=”true”>Show</a>Ajax.Request(‘/articles/1’,{asynchronous:true,evalScripts:true,parameters:‘authenticity_token=’+encodeURIComponent(‘A9s...3cf’)}); return false;”>Show</a><% remote_form_for(@article) do |f| %> <%= form_for(@article, :remote => true) do |f| %><form action=”/articles” class=”new_post” <form action=”/articles” class=”new_post”id=”new_post” method=”post” onSubmit=”new id=”new_post” method=”post” data-remote=”true”>Ajax.Request(‘/articles’,{asynchronous:true,evalScripts:true,parameters:Form.serialize(this)}); return false;”> 21/25
    • XSS et Unobstrusive JS <%= link_to “Delete”, @article, :method => :delete %> <a href="#" title="Delete" onclick="var f = document.createElement(form); f.style.display = none; this.parentNode.appendChild(f); f.method = POST; f.action = Rails 2 this.href;var s = document.createElement(input); s.setAttribute(type, hidden); s.setAttribute(name, authenticity_token); s.setAttribute(value, XXX...XXX); f.appendChild(s);f.submit(); return false;">Delete</a> Rails 3 <a href=”/articles/1” data-method=”delete” rel=”nofollow”>Delete</a>22/25
    • XSS et Unobstrusive JS <%= link_to “Delete”, @article, :method => :delete, :confirm => “Are you sure ?” %> <a href="#" title="Delete" onclick="if (confirm(“Are you sure ?”)) {var f = document.createElement(form); f.style.display = none; this.parentNode.appendChild(f); f.method = POST; f.action = Rails 2 this.href;var s = document.createElement(input); s.setAttribute(type, hidden); s.setAttribute(name, authenticity_token); s.setAttribute(value, XXX...XXX); f.appendChild(s);f.submit();} return false;">Delete</a> Rails 3 <a href=”/articles/1” data-method=”delete” data-confirm=”Are you sure ?” rel=”nofollow”>Delete</a>23/25
    • Prochaine étape Rails 3.1 • jQuery • CoffeeScript • Sass24/25
    • Prochaine étape number = 42 if true square = (x) -> x * x alert "Hello" if number? list = [1, 2, 3, 4] Rails 3.1 squares = (square num for num in list) var number, square, list, squares, num; • jQuery if (true) { number = 42; } • CoffeeScript square = function(x) { return x * x; }; if (typeof number !== "undefined" && number !== null) { • Sass } alert("Hello"); squares = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(square(num)); } return _results; })();25/25
    • Prochaine étape table.hl margin: 2em 0 td.ln text-align: right Rails 3.1 li font: family: serif weight: bold jQuery size: 1.2em • • CoffeeScript table.hl { margin: 2em 0; } table.hl td.ln { • Sass } text-align: right; li { font-family: serif; font-weight: bold; font-size: 1.2em; }26/25
    • Prochaine étape table.hl margin: 2em 0 td.ln text-align: right Rails 3.1 li font: family: serif weight: bold jQuery size: 1.2em • • CoffeeScript table.hl { margin: 2em 0; } table.hl td.ln { • Sass } text-align: right; li { font-family: serif; font-weight: bold; font-size: 1.2em; }27/25
    • Merci de votre attention Cont act : LINAGORA - Siège social 80, rue Roque de Fillol 92800 PUTEAUX FRANCE Tél. : 0 810 251 251 (t arif local) Fax : +33 (0)1 46 96 63 64 Mail : info@linagora.com Web : www.linagora.com 28 WWW.LINAGORA.COM