28
1 WWW.LINAGORA.COM WWW.LINAGORA.COM Les nouveautés de Rails 3 Simon COURTOIS Expert en Open-Source [email protected]

Les nouveautés de Rails 3

Embed Size (px)

DESCRIPTION

Présentation donnée lors du salon Solutions Linux 2011.Animée par Simon Courtois, Expert en Open Source

Citation preview

Page 1: Les nouveautés de Rails 3

1 WWW.LINAGORA.COMWWW.LINAGORA.COM

Les nouveautés de Rails 3Simon COURTOIS

Expert en [email protected]

Page 2: Les nouveautés de Rails 3

2 /25

rails myapp

Rails 2

rails new myapp

Rails 3

./script/generate rails g

./script/console rails c

./script/server

./script/dbconsole

rails s

rails db

La commande rails

Page 3: Les nouveautés de Rails 3

3 /25

• --skip-activerecord, -O

• --skip-test-unit, -T

• --skip-prototype, -J

• --skip-git, -G ???

.bundledb/*.sqlite3log/*.logtmp/

.gitignore

La commande rails

Page 4: Les nouveautés de Rails 3

4 /25

Rails::Initializer.run do |config| ... config.gem 'haml' config.gem 'coderay', :version => '~>0.9.7' ...end

Rails 2

Rails 3source 'http://rubygems.org'gem 'rails', '3.0.6'gem 'haml'gem 'coderay', '~> 0.9.7'group :development, :test do gem 'cucumber-rails'end

Gemfile

config/environment.rb

bundle install

Bundler

Page 5: Les nouveautés de Rails 3

5 /25

ActiveRelation

@articles = Article.find(:all, :conditions => {:published => true})

@articles = Article.where(:published => true)

➡ requête immédiate➡ retourne un tableau d’articles

➡ pas de requête➡ retourne un objet ActiveRecord::Relation

Rails 2

Rails 3

Page 6: Les nouveautés de Rails 3

6 /25

ActiveRelation

@articles = Article.where(:published => true)

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

@articles.each do |article| ...end

requête effectuée

Page 7: Les nouveautés de Rails 3

7 /25

ActiveRelation

@articles = Article.where(:published => true)

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

@articles.each do |article| ...end

Page 8: Les nouveautés de Rails 3

8 /25

ActiveRelation

@articles = Article.where(:published => true).order(params[:order])

@articles.each do |article| ...end

Page 9: Les nouveautés de Rails 3

9 /25

ActiveRelation

articles = Article.order(params[:order])

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

Page 10: Les nouveautés de Rails 3

10/25

ActiveRelation

@published = articles.published@unpublished = articles.unpublished

class Article < ActiveRecord::Base named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false}end

class Article < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false)end

Rails 2

Rails 3

Page 11: Les nouveautés de Rails 3

11/25

ActiveRelation

where(:conditions)

having(:conditions)

select

group

order

limit

offset

joins

includes(:include)

lock

readonly

from

all

first

last

Page 12: Les nouveautés de Rails 3

12/25

ActiveRelation

Article.find(:all, :conditions => {:author => “Bob”}, :includes => :comments, :order => “title”, :limit => 10)

Rails 2

Article.where(:author => “Bob”).includes(:comments).order(“title”).limit(10).all

Rails 3

Page 13: Les nouveautés de Rails 3

13/25

ActiveController

class ArticlesController < 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 format.xml { render :xml => @user } end endend

class ArticlesController < Applic... respond_to :html, :xml

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

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

Rails 2 Rails 3

flash[:notice] = “Article created”redirect_to @article

redirect_to @article, :notice => “Article created”

Page 14: Les nouveautés de Rails 3

14/25

Le routing

ActionController::Routing::Routes.draw do |map| map.resources :articlesend

Myapp::Application.routes.draw do |map| resources :articlesend

Rails 2 Rails 3

Page 15: Les nouveautés de Rails 3

15/25

Le routing

ActionController::Routing::Routes.draw do |map| map.resources :articles, :member => { :preview => :post }, :collection => { :archived => :get }end

Myapp::Application.routes.draw do |map| resources :articles do member do post :preview end

collection do get :archived end endend

Rails 2 Rails 3

Myapp::Application.routes.draw do |map| resources :articles do post :preview, :on => :member get :archived, :on => :collection endend

Page 16: Les nouveautés de Rails 3

16/25

Le routing

ActionController::Routing::Routes.draw do |map| map.resources :articles do |article| article.resources :comments endend

Myapp::Application.routes.draw do |map| resources :articles do resources :comments endend

Rails 2 Rails 3

ActionController::Routing::Routes.draw do |map| map.connect “login”, :controller => “session”, :action => “new”end

Myapp::Application.routes.draw do |map| match “login” => “session#new”end

Page 17: Les nouveautés de Rails 3

17/25

Le routing

ActionController::Routing::Routes.draw do |map| map.login “login”, :controller => “session”, :action => “new”end

Myapp::Application.routes.draw do |map| match “login” => “session#new”, :as => :loginend

Rails 2 Rails 3

ActionController::Routing::Routes.draw do |map| map.root :controller => “articles”, :action => “index”end

Myapp::Application.routes.draw do |map| root :to => “users#index”end

ActionController::Routing::Routes.draw do |map| map.connect “:controller/:action/:id” map.connect “:controller/:action/:id.:format”end

Myapp::Application.routes.draw do |map| match “:controller(/:action(/:id(.:format)))”end

Page 18: Les nouveautés de Rails 3

18/25

Le routing

Myapp::Application.routes.draw do |map| match “/articles(/:year(/:month(/:day)))” => “articles#index”end

Rails 2

Rails 3

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

Page 19: Les nouveautés de Rails 3

19/25

Le routing

Myapp::Application.routes.draw do |map| match “/articles/:year” => “articles#index”, :via => :getend

Rails 2

Rails 3

ActionController::Routing::Routes.draw do |map| map.connect ‘/articles/:year’, :controller => “articles”, :action => “index”, :conditions => { :method => :get }end

Myapp::Application.routes.draw do |map| get “/articles/:year” => “articles#index”end

Page 20: Les nouveautés de Rails 3

20/25

Le routing

Myapp::Application.routes.draw do |map| match “signin”, :to => redirect(“/login”) match “linagora”, :to => redirect(“http://www.linagora.com”)end

Rails 3

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.rb

Page 21: Les nouveautés de Rails 3

21/25

XSS et Unobstrusive JS

<%= @article.title %> # Non sécurisé

<%=h @article.title %> # Sécurisé

<%= @article.title %> # Sécurisé

<%=raw @article.title %> # Non sécurisé

Rails 2 Rails 3

<%= link_to_remote “Show”, :url => @article %> <%= link_to “Show”, :remote => true %>

<a href=”#” onclick=”new Ajax.Request(‘/articles/1’,{asynchronous:true,evalScripts:true,parameters:‘authenticity_token=’+encodeURIComponent(‘A9s...3cf’)}); return false;”>Show</a>

<a href=”/articles/1” data-remote=”true”>Show</a>

<% remote_form_for(@article) do |f| %> <%= form_for(@article, :remote => true) do |f| %>

<form action=”/articles” class=”new_post” id=”new_post” method=”post” onSubmit=”new Ajax.Request(‘/articles’, {asynchronous:true,evalScripts:true,parameters:Form.serialize(this)}); return false;”>

<form action=”/articles” class=”new_post” id=”new_post” method=”post” data-remote=”true”>

Page 22: Les nouveautés de Rails 3

22/25

XSS et Unobstrusive JS

<%= link_to “Delete”, @article, :method => :delete %>

Rails 2

Rails 3

<a href="#" title="Delete" onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = 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>

<a href=”/articles/1” data-method=”delete” rel=”nofollow”>Delete</a>

Page 23: Les nouveautés de Rails 3

23/25

XSS et Unobstrusive JS

<%= link_to “Delete”, @article, :method => :delete, :confirm => “Are you sure ?” %>

Rails 2

Rails 3

<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 = 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>

<a href=”/articles/1” data-method=”delete” data-confirm=”Are you sure ?” rel=”nofollow”>Delete</a>

Page 24: Les nouveautés de Rails 3

24/25

Prochaine étape

Rails 3.1

• jQuery

• CoffeeScript

• Sass

Page 25: Les nouveautés de Rails 3

25/25

Prochaine étape

Rails 3.1

• jQuery

• CoffeeScript

• Sass

number = 42 if true

square = (x) -> x * x

alert "Hello" if number?

list = [1, 2, 3, 4]

squares = (square num for num in list)

var number, square, list, squares, num;

if (true) { number = 42;}square = function(x) { return x * x;};if (typeof number !== "undefined" && number !== null) { 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;})();

Page 26: Les nouveautés de Rails 3

26/25

Prochaine étape

Rails 3.1

• jQuery

• CoffeeScript

• Sass

table.hl margin: 2em 0 td.ln text-align: right

li font: family: serif weight: bold size: 1.2em

table.hl { margin: 2em 0;}table.hl td.ln { text-align: right;}

li { font-family: serif; font-weight: bold; font-size: 1.2em;}

Page 27: Les nouveautés de Rails 3

27/25

Prochaine étape

Rails 3.1

• jQuery

• CoffeeScript

• Sass

table.hl margin: 2em 0 td.ln text-align: right

li font: family: serif weight: bold size: 1.2em

table.hl { margin: 2em 0;}table.hl td.ln { text-align: right;}

li { font-family: serif; font-weight: bold; font-size: 1.2em;}

Page 28: Les nouveautés de Rails 3

28 WWW.LINAGORA.COMWWW.LINAGORA.COM

Merci de votre attention

Contact :LINAGORA - Siège social

80, rue Roque de Fillol92800 PUTEAUX

FRANCETél. : 0 810 251 251 (tarif local)

Fax : +33 (0)1 46 96 63 64Mail : [email protected] : www.linagora.com