what's new in rails 4

63
What's new in Rails 4 Lucas Caton

Upload: lucas-caton

Post on 10-May-2015

5.574 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: What's new in rails 4

What's new in Rails 4

Lucas Caton

Page 3: What's new in rails 4

4

Page 4: What's new in rails 4

June 25, 2013Rails 4.0: Final version released!

Page 5: What's new in rails 4

Ruby 1.8.7Ruby 1.9.2Ruby 1.9.3Ruby 2.0.0

Page 6: What's new in rails 4

RubyGems 2.x

Page 7: What's new in rails 4

ThreadSafety

Page 8: What's new in rails 4

memcache-client

dalli=> 20% faster=> ThreadSafe=> Easier to integrate with NewRelic RPM

Page 9: What's new in rails 4

ActiveRecordActiveModel4

Page 10: What's new in rails 4

#where

Post.find_all_by_title('Rails 4')

Post.where(title: 'Rails 4')

Page 11: What's new in rails 4

#!nd_or_*

Post.find_or_initialize_by_title('Rails 4')

Post.find_or_initialize_by(title:'Rails4')

Page 12: What's new in rails 4

#update

@post.update_attributes(post_params)

@post.update(post_params)

Page 13: What's new in rails 4

#update_columns

@post.update_attribute(post_params)

@post.update_columns(post_params)

Skip validations!

Page 14: What's new in rails 4

#all

@posts = Post.scoped

@posts = Post.all

ActiveRecord::Relation

Page 15: What's new in rails 4

Scopes

scope :sold, where(state: 'sold')default_scope where(state: 'available')

scope :sold, ->{ where(state: 'sold') }default_scope ->{ where(state: 'available') }

Page 16: What's new in rails 4

class User < ActiveRecord::Base def visible_posts case role when 'Country Manager' Post.where(country: country) when 'Reviewer' Post.published when 'Bad User' end endend

Page 17: What's new in rails 4

class User < ActiveRecord::Base def visible_posts case role when 'Country Manager' Post.where(country: country) when 'Reviewer' Post.published when 'Bad User' [] end endend

Page 18: What's new in rails 4

@posts = current_user.visible_posts

if @posts.any? @posts.recentelse []end

Page 19: What's new in rails 4

#noneclass User < ActiveRecord::Base def visible_posts case role when 'Country Manager' Post.where(country: country) when 'Reviewer' Post.published when 'Bad User' Post.none end endend

Page 20: What's new in rails 4

#notif author Post.where('author != ?', author)else Post.where('author IS NOT NULL')end

Page 21: What's new in rails 4

#notif author Post.where('author != ?', author)else Post.where('author IS NOT NULL')end

Post.where.not(author: author)

Page 22: What's new in rails 4

#order

User.order('created_at DESC')User.order(:name, 'created_at DESC')

User.order(created_at: :desc)User.order(:name, created_at: :desc)

Page 23: What's new in rails 4

ActiveModel4

Page 24: What's new in rails 4

ActiveModelclass SupportTicket include ActiveModel::Conversion include ActiveModel::Validations extend ActiveModel::Naming extend ActiveModel::Translation

attr_accessor :title, :description

validates_presence_of :title validates_presence_of :descriptionend

Page 25: What's new in rails 4

ActiveModel

class SupportTicket include ActiveModel::Model

attr_accessor :title, :description

validates_presence_of :title validates_presence_of :descriptionend

Page 26: What's new in rails 4

Strong Parameters4

Page 27: What's new in rails 4

class User < ActiveRecord::Base attr_accessible :nameend

# Controllerdef update if @user.update_attributes(params[:user]) redirect_to @user, notice: 'Updated' endend

Page 28: What's new in rails 4

Strong Parametersclass User < ActiveRecord::Baseend

# Controllerdef update user_params = params.require(:user).permit(:name)

end

Page 29: What's new in rails 4

Strong Parametersclass User < ActiveRecord::Baseend

# Controllerdef update user_params = params.require(:user).permit(:name)

if @user.update(user_params) redirect_to @user, notice: 'Updated' endend

Page 30: What's new in rails 4

Routes4

Page 31: What's new in rails 4

match '/items/:id/purchase', to: 'items#purchase'

Page 32: What's new in rails 4

match '/items/:id/purchase', to: 'items#purchase'XSS Attack (Cross-site Scripting)

post '/items/:id/purchase', to: 'items#purchase'match '/items/:id/purchase', to: 'items#purchase', via: :postmatch '/items/:id/purchase', to: 'items#purchase', via: :all

Page 33: What's new in rails 4

PATCH Method for HTTPAbstract

Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource

modi!cation. The existing HTTP PUT method only allows a complete replacement of a document. This proposal

adds a new HTTP method, PATCH, to modify an existing HTTP resource.

http://tools.ietf.org/html/rfc5789

Page 34: What's new in rails 4

$ rake routes

items GET /items(.:format) items#index POST /items(.:format) items#createnew_item GET /items/new(.:format) items#newedit_item GET /items/:id/edit(.:format) items#edititem GET /items/:id(.:format) items#show PUT /items/:id(.:format) items#update PATCH /items/:id(.:format) items#update DELETE /items/:id(.:format) items#destroy

PATCH HTTP verb

Page 35: What's new in rails 4

Action Controller4

Page 36: What's new in rails 4

#before_action

before_filter :set_user, only: [:update]

before_action :set_user, only: [:update]

Page 37: What's new in rails 4

Encrypted cookie storedin the browser

Page 38: What's new in rails 4

Flash messages

<p id="notice"><%= flash[:notice] %></p>

<p id="notice"><%= notice %></p>

Page 39: What's new in rails 4

Streaming

Page 40: What's new in rails 4

class FooController < ActionController::Base include ActionController::Live

def index 100.times { # Client will see this as it's written response.stream.write "hello world\n" sleep 1 } response.stream.close endend

Page 41: What's new in rails 4

Views4

Page 42: What's new in rails 4

collections helpers<% @owners.each do |owner| %> <%= radio_button_tag :owner_id, owner.id %> <%= owner.name %><% end %>

collection_radio_buttons(:item, :owner_id, @owners, :id, :name)

collection_check_boxes(:item, :owner_id, @owners, :id, :name)

Page 43: What's new in rails 4

date !elds

<%= f.date_field :return_date %>

<input id="item_return_date" name="item[return_date]" type="date">

Page 44: What's new in rails 4

date !elds<%= f.date_field :return_date %>

Page 45: What's new in rails 4

ETags

Page 46: What's new in rails 4

First request

Page 47: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

First request

Page 48: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

headers['ETag'] = Digest::MD5.hexdigest(body)

First request

Page 49: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

304 Not Modified

Page 50: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Client caches response Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

304 Not Modified

Page 51: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Client caches response Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

304 Not Modified

Page 52: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Client caches response Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

1. Render body2. Create ETag3. Compare ETag4. If ETags match then bodynot included in response

304 Not Modified

Page 53: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Client caches response Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

1. Render body2. Create ETag3. Compare ETag4. If ETags match then bodynot included in response

304 Not Modified

Page 54: What's new in rails 4

1. Render body2. Create ETag3. Body & ETag includedin response

200 Successheaders['ETag']

328ebd07ec6a48ad7f70e44045f184a8

First request

Client caches response Second requestheaders['If-None-Match']328ebd07ec6a48ad7f70e44045f184a8

Client reads responsefrom cache

1. Render body2. Create ETag3. Compare ETag4. If ETags match then bodynot included in response

304 Not Modified

Page 55: What's new in rails 4

TurboLinks

aka. full body pjax

Page 57: What's new in rails 4

turbolinks

gem 'turbolinks'

//= require turbolinks

Gem!le

app/assets/javascripts/application.js

Page 58: What's new in rails 4

turbolinks

gem 'turbolinks'gem 'jquery-turbolinks'

//= require turbolinks//= require jquery.turbolinks

Gem!le

app/assets/javascripts/application.js

Page 59: What's new in rails 4

turbolinks

Page 61: What's new in rails 4

LinksRails Guides - Upgrading from Rails 3.2 to Rails 4.0:http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-rails-4-0

Rails Casts - Upgrading to Rails 4http://railscasts.com/episodes/415-upgrading-to-rails-4

Code School - Learn Rails 4 best practiceshttp://www.codeschool.com/courses/rails-4-zombie-outlaws

Page 62: What's new in rails 4

Thank you!

Page 63: What's new in rails 4

?