What's new in Rails 4.1

2,671 views

Published on

Published in: Technology
  • Be the first to comment

What's new in Rails 4.1

  1. 1. What’s new in 4.1(Chennai.rb March meetup)
  2. 2. (@vysakh0) Vysakh Sreenivasan Just want to build cool apps for me and others
  3. 3. - 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
  4. 4. #1 Spring preloader built in integration in Rails 4.1
  5. 5. keeps app running in the background No booting every time you run a test, rake task or migration
  6. 6. PARTY IN THOSE 5 DAYS
  7. 7. How do you load api keys in Rails? - create .yml, .rb file and load or - figaro gem or - dotenv gem.
  8. 8. #2 config/secrets.yml generated by Rails 4.1
  9. 9. 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
  10. 10. Config
  11. 11. How do you change the state of Order - to pending. - to completed. - to refunded
  12. 12. Create separate columns? - completed(:boolean) , refunded(:boolean) Create an integer column? - 0 -> pending - 1 -> completed - 2-> refunded
  13. 13. #3 Active Record enums in Rails 4.1
  14. 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. 15. Now enum gives us handy helper methods
  16. 16. @order.pending? @order.completed! @order.completed? @order.refunded! @order.refunded?
  17. 17. #4 Application Message Verifier built in helper in Rails 4.1
  18. 18. 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
  19. 19. USE CASES - Password reset - Invite links - Oauth tokens - and more
  20. 20. Show different templates to phone/browsers/tablets ?
  21. 21. #5 Application Pack Variants (Code example taken from http://coherence.io/blog/2013/12/17/whats-new-in-rails-4-1.html )
  22. 22. 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
  23. 23. 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
  24. 24. #6 Application Mailer Previews
  25. 25. class NotifierPreview < ActionMailer::Preview def welcome Notifier.welcome(User.first) end end # test/mailers/notifier_preview.rb
  26. 26. List of all mailer previews http://localhost:3000/rails/mailers

×