what's new in rails 5 - api mode & action cable overview
TRANSCRIPT
Render views outside of controllers
API Mode
OrdersController.render :show, assigns: { order: Order.last }
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"]
thread_mattr* a per thread module attribute
Active Support
module CurrentScope thread_mattr_accessor :user_permissions end
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'
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
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 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
// app/assets/javascripts/channels/messages.js
App.messages = App.cable.subscriptions .create(‘MessagesChannel', { received: function(data) { }
}
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?
Forever Frame
Long HTTP Polling where server sending chunks of data but
doesn’t terminate the connection.
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…