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.

Cb5d9e9095cd41b636764a85e57ade4b?s=128

Nando Vieira

June 21, 2014
Tweet

Transcript

  1. 3.

    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. 5.

    $ 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. 9.

    class TasksController < ApplicationController respond_to :html, :json, :xml ! def

    index @tasks = Task.all respond_with(@tasks) end end
  4. 13.
  5. 14.
  6. 15.

    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
  7. 17.

    class TasksController < ApplicationController respond_to :html, :json ! def create

    @task = Task.create(task_params) respond_with @task, location: tasks_path, responder: TaskCreateResponder end end
  8. 18.
  9. 20.

    A primeira ideia que vem à cabeça é renderizar um

    template específico para dispositivos móveis.
  10. 21.

    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
  11. 29.
  12. 31.

    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
  13. 34.

    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
  14. 36.

    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
  15. 38.

    <%= render 'profile' %> <%= render 'groups', groups: @dashboard.group %>

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