lightning talk
DESCRIPTION
LT feita apresnetada no Rock and Rails IITRANSCRIPT
ActionController::ResponderResponders na sua aplicação Rails
Responders são responsavéis por
Expor os resources para diferentes MIME requestes
"MIME types" são usados para identificar otipo de informação que um arquivo contém
Controllers gigantesclass ProjectsController < ApplicationController
def index
@projects = Project.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @projects }
end
end
def show
@project = Project.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @project }
end
end
def new
@project = Project.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
Letras pequenas, sinal de coisa ruim
Preciso enxugar meus controllersclass ProjectsController < ApplicationController before_filter :find_project, :only => [:show, :edit, :update, :destroy]] respond_to :html, :jsondef index @projects = Project.all respond_with @projectsenddef show respond_with @projectenddef new @project = Project.new respond_with @projectenddef editend
Condicionais
KEEP CALM and WRITE TESTS
Responders são personalizavéisapp/responders/application_responder.rbapp/responders/create_responder.rbapp/responders/update_responder.rb
gem 'responders' (github.com/plataformatec/responders)FlashResponder rails generate responders:install
Actions create e updatedef create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
def update
@project = Project.find(params[:id])
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
Perimta que o responder cuide da lógica dos "MIME" e alguns condicionais
before_filter :find_project, :only => [:show, :edit. :update, :destroy]def create @project = Project.new(params[:project]) respond_with(@project, :location => @project)end def update @project.update_attributes(params[:projects]) respond_with(@project, :location => @projects) end
Obrigado!
www.gilgomes.com.br
@gil27
github.com/gilgomesp
facebook.com/gilgomes27