Your SlideShare is downloading. ×
0
Les nouveautés de Rails 3     Simon COURTOIS   Expert en Open-Source   scourtois@linagora.com             1              W...
La commande rails                      Rails 2             Rails 3                rails myapp          rails new myapp    ...
La commande rails        •   --skip-activerecord, -O    .gitignore        •   --skip-test-unit, -T                        ...
Bundler        Rails 2 Rails::Initializer.run do |config|   ... config.gem haml config.gem coderay, :version => ~>0.9.7 .....
ActiveRelation                 Rails 2         @articles = Article.find(:all, :conditions => {:published => true})        ...
ActiveRelation             @articles = Article.where(:published => true)             if params[:order]               @arti...
ActiveRelation             @articles = Article.where(:published => true)             @articles = @articles.order(params[:o...
ActiveRelation             @articles = Article.where(:published => true).order(params[:order])             @articles.each ...
ActiveRelation                 articles = Article.order(params[:order])                 @published   = articles.where(:pub...
ActiveRelation                  @published   = articles.published                  @unpublished = articles.unpublished    ...
ActiveRelation           where(:conditions)    all           having(:conditions)   first           select                l...
ActiveRelation             Rails 2        Article.find(:all, :conditions => {:author => “Bob”}, :includes => :comments,   ...
ActiveController                     Rails 2                                      Rails 3class ArticlesController < Applic...
Le routing                    Rails 2                                       Rails 3ActionController::Routing::Routes.draw ...
Le routing                     Rails 2                                        Rails 3ActionController::Routing::Routes.dra...
Le routing                     Rails 2                                       Rails 3 ActionController::Routing::Routes.dra...
Le routing                      Rails 2                                           Rails 3  ActionController::Routing::Rout...
Le routing        Rails 2   ActionController::Routing::Routes.draw do |map|     map.connect ‘/articles/:year/:month/:day’,...
Le routing         Rails 2    ActionController::Routing::Routes.draw do |map|      map.connect ‘/articles/:year’, :control...
Le routing                                            Rails 3        Myapp::Application.routes.draw do |map|          matc...
XSS et Unobstrusive JS                   Rails 2                                                  Rails 3 <%= @article.tit...
XSS et Unobstrusive JS                         <%= link_to “Delete”, @article, :method => :delete %>                     <...
XSS et Unobstrusive JS                         <%= link_to “Delete”, @article, :method => :delete,                        ...
Prochaine étape  Rails 3.1        •   jQuery        •   CoffeeScript        •   Sass24/25
Prochaine étape                           number = 42 if true                           square = (x) -> x * x             ...
Prochaine étape                           table.hl                             margin: 2em 0                             t...
Prochaine étape                           table.hl                             margin: 2em 0                             t...
Merci de votre attention                                               Cont act :                               LINAGORA -...
Upcoming SlideShare
Loading in...5
×

Les nouveautés de Rails 3

1,587

Published on

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

Animée par Simon Courtois, Expert en Open Source

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

  • Be the first to like this

No Downloads
Views
Total Views
1,587
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Les nouveautés de Rails 3"

  1. 1. Les nouveautés de Rails 3 Simon COURTOIS Expert en Open-Source scourtois@linagora.com 1 WWW.LINAGORA.COM
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. ActiveRelation @articles = Article.where(:published => true) @articles = @articles.order(params[:order]) @articles.each do |article| ... end7 /25
  8. 8. ActiveRelation @articles = Article.where(:published => true).order(params[:order]) @articles.each do |article| ... end8 /25
  9. 9. ActiveRelation articles = Article.order(params[:order]) @published = articles.where(:published => true) @unpublished = articles.where(:published => false)9 /25
  10. 10. 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
  11. 11. ActiveRelation where(:conditions) all having(:conditions) first select last group order limit offset joins includes(:include) lock readonly from11/25
  12. 12. 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
  13. 13. 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
  14. 14. Le routing Rails 2 Rails 3ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map| map.resources :articles resources :articlesend end14/25
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. Prochaine étape Rails 3.1 • jQuery • CoffeeScript • Sass24/25
  25. 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
  26. 26. 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
  27. 27. 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
  28. 28. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×