what's new in rails 5 - api mode & action cable overview

53
Rails 5

Upload: maxim-veksler

Post on 07-Apr-2017

148 views

Category:

Technology


3 download

TRANSCRIPT

Rails 5

Agenda

• New in Rails 5

• Action Cable

New in Rails 5: API Mode

API Mode

Project generator for backend only

ActiveModel validators return type of failure

API Mode

API exceptions return in JSON form

API Mode

Render views outside of controllers

API Mode

OrdersController.render :show, assigns: { order: Order.last }

Improvements in Active Support

ArrayInquirer provides friendlier way to check

contents in an array

Active Support

pets = [:cat, 'rabbit']).inquiry

> pets.cat? #=> true

pluck method is now added to Enumerable

Active Support

users = [{id: 1, name: 'Max'}, {id: 2, name: 'Morris'}]

users.pluck(:name) => ["Max", "Morris"]

without method is now added to Enumerable

Active Support

vehicles = ['Car', 'Bike', 'Truck', 'Bus']

vehicles.without("Car", "Bike") => ["Truck", "Bus"]

#positive? #negative?

Active Support

4.negative? => false

country_zones helper

Active Support

ActiveSupport::TimeZone.country_zones(‘fr’)

(GMT+01:00) Paris

thread_mattr* a per thread module attribute

Active Support

module CurrentScope thread_mattr_accessor :user_permissions end

New stuff in Active Record

OR support in Relations

Active Record

LEFT OUTER JOIN support in Relations

Active Record

BI-Directional DESTORY

Active Record

Schema Migration Comments

Active Record

class CreateProducts < ActiveRecord::Migration[5.0] def change create_table :products do |t| t.float :msrp, comment: 'Maximum Retail Price'

Cool stuff in rails 5

nested Parameter filtering

config.filter_parameters += ["credit_card.code"]

Cool stuff in rails 5: Security

New has_secure_token helper for Active Record

class User < ApplicationRecord has_secure_token end

>> user.token => 'qjCbex522DfVEVd5ysUWppWQ'

Cool stuff in rails 5: Security

Weak ETags by default

Cool stuff in rails 5: Performance

No more rake.. everything under the rails command

Cool stuff in rails 5: Developer Experience

Bonus: 1 new “BUG”

Record update allowed to not touch

updated_at timestamp

1 new “BUG”

Let’s talk about

Action Cable

Action Cable

• Rails for WebSocket backend • ActionCable JavaScript for front-end

Fullstack solution

Action CableWebSocket backend

• Deeply integrated into the rails environment • Same request/response paradigm as controllers

• Build to be distributed

Action CableWTF is WebSocket ?

Bi-directional always established TCP real

time a few more buzz words based

communication channel for browsers.

Action CableWTF is WebSocket ?

Complimentary to the stateless

nature of HTTP

Action CableAction Cable Backend Implementation

Action CableAction Cable Backend Implementation

Action CableRoutes

Rails.application.routes.draw do

# Serve websocket cable requests in-process mount ActionCable.server => '/cable'

Action CableConnection

module ApplicationCable class Connection < ActionCable::Connection::Base end end

Action CableChannel

# app/channels/messages_channel.rb

class MessagesChannel < ApplicationCable::Channel def subscribed stream_from 'messages' end end

Action CableBroadcast

# app/controllers/messages_controller.rb

class MessagesController < ApplicationController def create if message.save ActionCable.server.broadcast 'messages', message: message.content, user: message.user.username

Action CableFrontend - Fighting AJAX

Action CableFrontend

this.App = {};

App.cable = ActionCable.createConsumer();

Action CableFrontend

// app/assets/javascripts/channels/messages.js

App.messages = App.cable.subscriptions .create(‘MessagesChannel', { received: function(data) { }

}

What came before WebSocket?

What came before WebSocket?

Goal: realtime browser push

What came before WebSocket?

DHTML

VBScript and JavaScript to make small changes in page, and

refresh for the “big” stuff

What came before WebSocket?

Cross Frame Communication

Have an iFrame reloading in the background, read data from it.

What came before WebSocket?

HTTP Polling

Hit the server a frequent intervals for new data.

What came before WebSocket?

LiveConnect

Java Applets #FML

What came before WebSocket?

Forever Frame

Long HTTP Polling where server sending chunks of data but

doesn’t terminate the connection.

What came before WebSocket?

AJAX

XMLHttpRequest because Microsoft wanted to build Outlook

Web.

What came before WebSocket?

HTTP Long-Polling

Client waits for message, server sends message then closes. Then

client opens new connection.

What came before WebSocket?XHR Streaming

Server maintains connection, buffer continues to grow. Client

eventually closes connection and reopens.

What came before WebSocket?

and a few others.

script tag, htmlfile, ActiveX Object, XHR multipart-replace…

Thank you for playing. FIN

Coming next Swift for the backend