Action Pack Variants – Action Mailer Previews – Spring, Rails Preloader



Action Pack Variants – Action Mailer Previews – Spring, Rails Preloader

0 0


meetup-february-2014-4-1


On Github benstevenson / meetup-february-2014-4-1

Rails 4.1 - The Nifty Bits

Ben / @bennett_stevens

Dragons Ahead

Rails 4.1 is still in Beta, and some issues(brought up later) will be present.

4.1.0.beta1 branch, f706d5f945

Action Pack Variants

Seems small but very powerful addition to the framework for API centric shops.

              
                before_action do 
                  request.variant = :phone if request.user_agent =~ /Nexus 5/
                  request.variant = :tablet if request.user_agent =~ /Nexus 7/
                end
              

                before_action is alias for before_filter
            

... continued.

              
                app/views/postings/show.html.erb
                app/views/postings/show.html+tablet.erb
                app/views/postings/show.html+phone.erb
              

              
                class PostingsController < ApplicationController
                  def show
                    @posting = Posting.find(params[:id])
                    respond_to do |format|
                      format.json

                      format.html do |html|
                        html.tablet
                        html.phone
                        html.none
                      end
                    end
                  end
                end
              
            

Action Mailer Previews

Replaces opt-in gems:

Based on basecamp/mail_view

Contrived Example!

              ('test' || 'spec')/mailers/previews/registration_mailer
              
                class RegistrationMailer < ActionMailer::Preview
                  def new_user
                    RegistrationMailer.registration(User.last)
                  end
                end
              
            

Become available at:

              
                http://localhost:3000/rails/mailers/registraiton_mailer/new_user
              
            

Full list at:

              
                http://localhost:3000/rails/mailers
              
            

But the other tools work already!

True, but they can still be used in staging or beta environments. Plus if this is part of the Github rails

Spring, Rails Preloader

Who uses Zeus? Who uses Spork? Who has no idea what I'm talking about?

Time Comparison

Rails new. Rspec. Single Scaffold.

              
                time ./bin/rake spec

                real  0m2.861s
                user  0m0.140s
                sys 0m0.084s
              

              
                time ./bin/rake spec

                real  0m2.008s
                user  0m0.141s
                sys 0m0.086s
              

              
                ./bin/spring status
                Spring is running:

                87467 spring server | awesome_project | started 54 secs ago  
                87468 spring app    | awesome_project | started 54 secs ago | development mode 
              
            

Active Record Enums

Queryable statuses mapped to db integers.

              
                class Posting < ActiveRecord::Base
                  enum status:[:draft, :published, :archived]
                end

                posting.draft! # => true
                posting.published? # => false
                posting.published! # => true

                Posting.published # =>[#<Posting id: 1, status: 1>]
              
            

And others!

  • secrets.yml
    • secretkeybase
    • api keys
  • Message Verifiers
    Rails.application.message_verifier(:remember_me).
    generate(token)
    
    Rails.application.message_verifier(:remember_me).
    verify(signed_token)
                  
  • Module concerning
    • Separating concerns in models
  • No touching!

Possible Roadblocks

Deprecations

It's just anarchy with some gems. ActiveRecord dependent gems ATM do not work.

Ransack. Cocoon. Something else I can't remember.

Test Refactoring, xss

More explicit

?

Slight changes

              Spec/Test File
              
                post :create, format: :js
              
            

Turns into

              
                xhr :post, :create, format: :js
              
            

Clashing Gems

And so many others.

Check it out.