Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Conhecendo Variants do Rails 4.1

Conhecendo Variants do Rails 4.1

O Rails está em uma fase evolutiva bastante interessante. Com releases menores e mais rápidos, está adicionando novas funcionalidades aos poucos. O Rails 4.1 trouxe uma funcionalidade chamada de variants, que permite definir views alternativas para uma mesma action e formato.

Nessa palestra vou mostrar como você pode renderizar views alternativas para dispositivos móveis, além de abstrair lógica das views sem muito esforço.

Nando Vieira

June 21, 2014
Tweet

More Decks by Nando Vieira

Other Decks in Programming

Transcript

  1. class TasksController < ApplicationController def index @tasks = Task.all !

    respond_to do |format| format.html format.xml { render xml: @tasks } format.json { render json: @tasks } end end end
  2. $ rake routes Prefix Verb URI Pattern Controller#Action root GET

    / site#index tasks GET /tasks(.:format) tasks#index POST /tasks(.:format) tasks#create new_task GET /tasks/new(.:format) tasks#new edit_task GET /tasks/:id/edit(.:format) tasks#edit task GET /tasks/:id(.:format) tasks#show PATCH /tasks/:id(.:format) tasks#update PUT /tasks/:id(.:format) tasks#update DELETE /tasks/:id(.:format) tasks#destroy
  3. class TasksController < ApplicationController respond_to :html, :json, :xml ! def

    index @tasks = Task.all respond_with(@tasks) end end
  4. class TaskCreateResponder < ActionController::Responder delegate :t, :flash, to: :controller def

    to_html if resource.new_record? flash.notice = t('flash.tasks.create.notice') else flash.alert = resource.errors.full_messages .to_sentence end ! redirect_to navigation_location end end
  5. class TasksController < ApplicationController respond_to :html, :json ! def create

    @task = Task.create(task_params) respond_with @task, location: tasks_path, responder: TaskCreateResponder end end
  6. A primeira ideia que vem à cabeça é renderizar um

    template específico para dispositivos móveis.
  7. class ApplicationController < ActionController::Base before_action :set_variant ! private ! def

    set_variant request.variant = :tablet if browser.tablet? request.variant = :mobile if browser.mobile? end end
  8. class SecurityModeController < ApplicationController def edit request.variant = SecurityMode.fetch(params[:security_mode]) !

    respond_to do |format| format.html do |variant| variant.none variant.card { initialize_card_security_mode } variant.sms { initialize_sms_security_mod } variant.totp { initialize_totp_security_mode } end end end end
  9. class SecurityModeController < ApplicationController def edit security_mode = SecurityMode.fetch(params[:security_mode]) request.variant

    = security_mode ! initializer = "initialize_#{security_mode}_security_mode" send(initialize) if respond_to?(initializer, true) end end
  10. class Dashboard def initialize(user) @user = user end ! def

    new_status @new_status ||= Status.new end ! def statuses Status.for(user) end ! def notifications @notifications ||= user.notifications end ! private ! attr_reader :user end
  11. <%= render 'profile' %> <%= render 'groups', groups: @dashboard.group %>

    ! <%= render 'statuses/form', status: @dashboard.new_status %> <%= render 'statuses', statuses: @dashboard.statuses %>