33
Choose Your Battles and LetIt::REST Hampton Catlin and Jeffrey Hardy

New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

ChooseYourBattlesandLetIt::REST

Hampton Catlin and Jeffrey Hardy

Page 2: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative
Page 3: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Obligatory Resume

‣ 50 years of Rails experience

‣ PHDs from MIT

‣ Founded the Dharma Initiative

Page 4: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Invented

Haml, Scribbish, Sass, ASCII, Ruby, Lisp, The DaVinci Code, Unicorns, Rainbows, Kittens, Cat Macros, Balloons, Prototype, Rails, Babies, Altoids, SQL, Religion, and Monkey Poop (not monkeys, though).

Page 5: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

The Beauty of Resource-Based

Controllers

Page 6: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

def index @posts = Post.find(:all)

respond_to do |format| format.html # index.rhtml format.xml { render :xml => @posts.to_xml } end end

def show @post = Post.find(params[:id])

respond_to do |format| format.html # show.rhtml format.xml { render :xml => @post.to_xml } end end

def new @post = Post.new end

def edit @post = Post.find(params[:id]) end

def create @post = Post.new(params[:post])

respond_to do |format| if @posts.save flash[:notice] = 'Posts was successfully created.' format.html { redirect_to post_url(@post) } format.xml { head :created, :location => post_url(@post) } else format.html { render :action => "new" } format.xml { render :xml => @post.errors.to_xml } end end end

def update @post = Post.find(params[:id])

respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = 'Posts was successfully updated.' format.html { redirect_to post_url(@post) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @post.errors.to_xml } end end end

def destroy @post = Post.find(params[:id]) @post.destroy

respond_to do |format| format.html { redirect_to posts_url } format.xml { head :ok } end endend

Page 7: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

The Problem

Page 8: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

The Goal

Page 9: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

PRODUCTIONNo Shit!

Page 10: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Exceptions are Manageable

Page 11: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Action Pattern

‣ load resource object(s)

‣ optional preloads

‣ perform modifying action (CUD)

‣ respond

Page 12: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

def index @posts = Post.find(:all)

respond_to do |format| format.html # index.rhtml format.xml { render :xml => @posts.to_xml } end end

def show @post = Post.find(params[:id])

respond_to do |format| format.html # show.rhtml format.xml { render :xml => @post.to_xml } end end

def new @post = Post.new end

def edit @post = Post.find(params[:id]) end

def create @post = Post.new(params[:post])

respond_to do |format| if @posts.save flash[:notice] = 'Posts was successfully created.' format.html { redirect_to post_url(@post) } format.xml { head :created, :location => post_url(@post) } else format.html { render :action => "new" } format.xml { render :xml => @post.errors.to_xml } end end end

def update @post = Post.find(params[:id])

respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = 'Posts was successfully updated.' format.html { redirect_to post_url(@post) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @post.errors.to_xml } end end end

def destroy @post = Post.find(params[:id]) @post.destroy

respond_to do |format| format.html { redirect_to posts_url } format.xml { head :ok } end endend

Page 13: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

make_resourceful do build :all end

end

Page 14: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class CommentsController < ApplicationController

make_resourceful do build :create, :destroy end

end

Page 15: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

map.resources :posts, :has_many => :comments

Page 16: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class CommentsController < ApplicationController

make_resourceful do build :create, :destroy belongs_to :post end

end

Page 17: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Association Proxy(our saviour)

Page 18: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Post.find(params[:post_id]).comments.find(:all)Post.find(params[:post_id]).comments.find(params[:id])Post.find(params[:post_id]).comments.find(params[:id]).update_attributes(params[:comment])Post.find(params[:post_id]).comments.find(params[:id]).destroyPost.find(params[:post_id]).comments.build(params[:comment])Post.find(params[:post_id]).comments.build(params[:comment]).save

notice something?

Page 19: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

current_model

Page 20: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Comment.belongs_to :user

Page 21: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

current_user

Page 22: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class CommentsController < ApplicationController

make_resourceful do build :create, :destroy belongs_to :post associate_with :current_user end

end

Page 23: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

More Helpers

Page 24: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

def current_param params[:id]end

def current_object current_model.find(current_param)end

def current_objects current_model.find(:all)end

def load_object instance_variable_set("@#{model_name}", current_object)end

Page 25: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Callbacks

Page 26: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

make_resourceful do build :all

before :show do @page_title = "Awesome" end end

end

Page 27: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

make_resourceful do build :all

before :show do @page_title = "Awesome" end

response_for :show do |format| format.html { redirect_to current_object } end

response_for :update do redirect_to root_url end end

end

Page 28: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

No Blocking

Page 29: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Encourages Phat Models

Page 30: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

Experimental: Publish

Page 31: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

class PostsController < ApplicationController

make_resourceful do build :all #... publish :formats => [:xml, :yaml, :json], :only => :show, :attributes => [:title, :body, { :comments => :body }] end

end

Page 32: New Choose Your Battles and LetIt::REST - Hampton Catlin · 2015. 3. 6. · Obligatory Resume ‣ 50 years of Rails experience ‣ PHDs from MIT ‣ Founded the Dharma Initiative

super alpha(but we’re using it in production)