What’s new in
4.1(Chennai.rb March meetup)
(@vysakh0)
Vysakh Sreenivasan
Just want to build cool apps for me and
others
- Rails app probably take around ~5
seconds to boot.
- 50 times a day (test, rails s, rails g...)
i.e You waste 5days in a YEAR
#1 Spring preloader
built in integration in Rails 4.1
keeps app running in the background
No booting every time you run a test,
rake task or migration
PARTY IN THOSE 5 DAYS
How do you load api keys in Rails?
- create .yml, .rb file and load or
- figaro gem or
- dotenv gem.
#2 config/secrets.yml
generated by 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
Config
How do you change the state of Order
- to pending.
- to completed.
- to refunded
Create separate columns?
- completed(:boolean) , refunded(:boolean)
Create an integer column?
- 0 -> pending
- 1 -> completed
- 2-> refunded
#3 Active Record enums
in Rails 4.1
# app/modes/order.rb
class Order < ActiveRecord::Base
# status is an integer column in orders table
enum status: {
pending: 0,
completed: 1,
refunded: 2
}
end
Now enum gives us
handy helper methods
@order.pending?
@order.completed!
@order.completed?
@order.refunded!
@order.refunded?
#4 Application Message Verifier
built in helper 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
USE CASES
- Password reset
- Invite links
- Oauth tokens
- and more
Show different templates to
phone/browsers/tablets ?
#5 Application Pack Variants
(Code example taken from
http://coherence.io/blog/2013/12/17/whats-new-in-rails-4-1.html )
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
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
#6 Application Mailer Previews
class NotifierPreview < ActionMailer::Preview
def welcome
Notifier.welcome(User.first)
end
end
# test/mailers/notifier_preview.rb
List of all mailer previews
http://localhost:3000/rails/mailers
What's new in Rails 4.1

What's new in Rails 4.1

  • 1.
  • 2.
    (@vysakh0) Vysakh Sreenivasan Just wantto build cool apps for me and others
  • 3.
    - Rails appprobably take around ~5 seconds to boot. - 50 times a day (test, rails s, rails g...) i.e You waste 5days in a YEAR
  • 4.
    #1 Spring preloader builtin integration in Rails 4.1
  • 5.
    keeps app runningin the background No booting every time you run a test, rake task or migration
  • 6.
  • 7.
    How do youload api keys in Rails? - create .yml, .rb file and load or - figaro gem or - dotenv gem.
  • 8.
  • 9.
    Just put yourapi 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
  • 10.
  • 11.
    How do youchange the state of Order - to pending. - to completed. - to refunded
  • 12.
    Create separate columns? -completed(:boolean) , refunded(:boolean) Create an integer column? - 0 -> pending - 1 -> completed - 2-> refunded
  • 13.
    #3 Active Recordenums in Rails 4.1
  • 14.
    # app/modes/order.rb class Order< ActiveRecord::Base # status is an integer column in orders table enum status: { pending: 0, completed: 1, refunded: 2 } end
  • 15.
    Now enum givesus handy helper methods
  • 16.
  • 18.
    #4 Application MessageVerifier built in helper in Rails 4.1
  • 19.
    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
  • 20.
    USE CASES - Passwordreset - Invite links - Oauth tokens - and more
  • 21.
    Show different templatesto phone/browsers/tablets ?
  • 23.
    #5 Application PackVariants (Code example taken from http://coherence.io/blog/2013/12/17/whats-new-in-rails-4-1.html )
  • 24.
    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
  • 25.
    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
  • 26.
  • 27.
    class NotifierPreview <ActionMailer::Preview def welcome Notifier.welcome(User.first) end end # test/mailers/notifier_preview.rb
  • 28.
    List of allmailer previews http://localhost:3000/rails/mailers