View
242
Download
0
Category
Preview:
Citation preview
7/31/2019 Make Resourceful
1/33
Choose Your Battles and
LetIt::RESTHampton Catlin and Jeffrey Hardy
7/31/2019 Make Resourceful
2/33
7/31/2019 Make Resourceful
3/33
Obligatory Resume
50 years of Rails experience
PHDs from MIT
Founded the Dharma Initiative
7/31/2019 Make Resourceful
4/33
Invented
Haml, Scribbish, Sass, ASCII, Ruby, Lisp,
The DaVinci Code, Unicorns, Rainbows,Kittens, Cat Macros, Balloons, Prototype,Rails, Babies, Altoids, SQL, Religion, andMonkey Poop (not monkeys, though).
7/31/2019 Make Resourceful
5/33
The Beauty of
Resource-BasedControllers
7/31/2019 Make Resourceful
6/33
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
end
end
7/31/2019 Make Resourceful
7/33
The Problem
7/31/2019 Make Resourceful
8/33
The Goal
7/31/2019 Make Resourceful
9/33
PRODUCTIONNo Shit!
7/31/2019 Make Resourceful
10/33
Exceptions areManageable
7/31/2019 Make Resourceful
11/33
Action Pattern
load resource object(s) optional preloads
perform modifying action (CUD)
respond
7/31/2019 Make Resourceful
12/33
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
end
end
7/31/2019 Make Resourceful
13/33
class PostsController < ApplicationController
make_resourceful dobuild :all end
end
7/31/2019 Make Resourceful
14/33
class CommentsController < ApplicationController
make_resourceful dobuild :create, :destroy end
end
7/31/2019 Make Resourceful
15/33
map.resources:posts, :has_many => :comments
7/31/2019 Make Resourceful
16/33
class CommentsController < ApplicationController
make_resourceful do
build :create, :destroybelongs_to :post
end
end
7/31/2019 Make Resourceful
17/33
Association Proxy(our saviour)
7/31/2019 Make Resourceful
18/33
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]).destroy
Post.find(params[:post_id]).comments.build(params[:comment])Post.find(params[:post_id]).comments.build(params[:comment]).save
notice something?
7/31/2019 Make Resourceful
19/33
current_model
7/31/2019 Make Resourceful
20/33
Comment.belongs_to:user
7/31/2019 Make Resourceful
21/33
current_user
7/31/2019 Make Resourceful
22/33
class CommentsController < ApplicationController
make_resourceful do
build:create, :destroy belongs_to:post associate_with:current_user end
end
7/31/2019 Make Resourceful
23/33
More Helpers
7/31/2019 Make Resourceful
24/33
def current_paramparams[:id]
end
def current_objectcurrent_model.find(current_param)
end
def current_objectscurrent_model.find(:all)
end
def load_objectinstance_variable_set("@#{model_name}", current_object)end
7/31/2019 Make Resourceful
25/33
Callbacks
7/31/2019 Make Resourceful
26/33
class PostsController < ApplicationController
make_resourceful dobuild :all
before :showdo@page_title = "Awesome"
end end
end
7/31/2019 Make Resourceful
27/33
class PostsController < ApplicationController
make_resourceful do
build :all
before :showdo@page_title = "Awesome"
end
response_for :showdo |format|format.html { redirect_to current_object }
end
response_for :update do
redirect_to root_url end end
end
7/31/2019 Make Resourceful
28/33
No Blocking
7/31/2019 Make Resourceful
29/33
Encourages Phat Models
7/31/2019 Make Resourceful
30/33
Experimental: Publish
7/31/2019 Make Resourceful
31/33
class PostsController < ApplicationController
make_resourceful dobuild :all
#...publish :formats => [:xml, :yaml, :json],
:only => :show, :attributes => [:title, :body, { :comments => :body }] end
end
7/31/2019 Make Resourceful
32/33
super alpha(but were using it in production)
7/31/2019 Make Resourceful
33/33
http://hamptoncatlin.comhcatlin@gmail.com
mailto:hcatlin@gmail.commailto:hcatlin@gmail.comhttp://hamptoncatlin.com/http://hamptoncatlin.com/Recommended