29
What’s new in 4.1 (Chennai.rb March meetup)

What's new in Rails 4.1

  • View
    1.335

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: What's new in Rails 4.1

What’s new in

4.1(Chennai.rb March meetup)

Page 2: What's new in Rails 4.1

(@vysakh0)

Vysakh SreenivasanJust want to build cool apps for me and

others

Page 3: What's new in Rails 4.1

- Rails app probably take around ~5 seconds to boot.

- 50 times a day (test, rails s, rails g...)

i.e You waste 5 days in a YEAR

Page 4: What's new in Rails 4.1

#1 Spring preloaderbuilt in integration in Rails 4.1

Page 5: What's new in Rails 4.1

keeps app running in the background

No booting every time you run a test, rake task or migration

Page 6: What's new in Rails 4.1

PARTY IN THOSE 5 DAYS

Page 7: What's new in Rails 4.1

How do you load api keys in Rails?

- create .yml, .rb file and load or

- figaro gem or

- dotenv gem.

Page 8: What's new in Rails 4.1

#2 config/secrets.ymlgenerated by Rails 4.1

Page 9: What's new in Rails 4.1

Just put your api keys in secrets.yml# config/secrets.yml

development: facebook_consumer_key: “random skdfjks” facebook_consumer_secret: “its a secret”

# config/secrets.yml

development: facebook_consumer_key: “random key” facebook_consumer_secret: “its a secret”

# config/initializers/devise.rb …..

config.omniauth :facebook, Rails.application.secrets.facebook_consumer_key , Rails.application.secrets.facebook_consumer_secret

…..

Just use it like this

Page 10: What's new in Rails 4.1

Config

Page 11: What's new in Rails 4.1

How do you change the state of Order

- to pending.- to completed.- to refunded

Page 12: What's new in Rails 4.1

Create separate columns?- completed(:boolean) , refunded(:boolean)

Create an integer column?- 0 -> pending- 1 -> completed- 2-> refunded

Page 13: What's new in Rails 4.1

#3 Active Record enumsin Rails 4.1

Page 14: What's new in Rails 4.1

# app/modes/order.rbclass Order < ActiveRecord::Base# status is an integer column in orders table enum status: { pending: 0, completed: 1, refunded: 2 }end

Page 15: What's new in Rails 4.1

Now enum gives ushandy helper methods

Page 16: What's new in Rails 4.1

@order.pending?

@order.completed!

@order.completed?

@order.refunded!

@order.refunded?

Page 17: What's new in Rails 4.1
Page 18: What's new in Rails 4.1

#4 Application Message Verifier

built in helper in Rails 4.1

Page 19: What's new in Rails 4.1

irb> @user.id # => 1

irb> user_token = Rails.application.message_verifier(“remember_me”). generate(@user.id) => "BAhpBg==--859750a419640d18d7c24e3166541a9405a42786"

irb> get_back_user_id = Rails.application.message_verifier(“remember_me”). verify(user_token)

=> 1

Page 20: What's new in Rails 4.1

USE CASES- Password reset

- Invite links

- Oauth tokens

- and more

Page 21: What's new in Rails 4.1

Show different templates to

phone/browsers/tablets ?

Page 22: What's new in Rails 4.1
Page 23: What's new in Rails 4.1

#5 Application Pack Variants(Code example taken from

http://coherence.io/blog/2013/12/17/whats-new-in-rails-4-1.html )

Page 24: What's new in Rails 4.1

class ApplicationController < ActionController::Base

before_action :detect_device_variant

private

def detect_device_variant

case request.user_agent

when /iPad/i

request.variant = :tablet

when /iPhone/i

request.variant = :phone

end

end

end

Page 25: What's new in Rails 4.1

class PostController < ApplicationController

def show

@post = Post.find(params[:id])

respond_to do |format|

format.json

format.html # /app/views/posts/show.html.erb

format.html.phone # /app/views/posts/show.html+phone.erb

format.html.tablet do

@show_edit_link = false

end

end

end

end

Page 26: What's new in Rails 4.1

#6 Application Mailer Previews

Page 27: What's new in Rails 4.1

class NotifierPreview < ActionMailer::Preview def welcome Notifier.welcome(User.first) endend

# test/mailers/notifier_preview.rb

Page 28: What's new in Rails 4.1

List of all mailer previews

http://localhost:3000/rails/mailers

Page 29: What's new in Rails 4.1