This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
jnewland (author)
Wed Sep 26 05:19:09 -0700 2007
commit 93a28c0993f6b13ea244800452b883f51dfcaf58
tree 4f7a2c41efd791fcda7b2d052142724f06f6d9ff
parent 9788da3a0993a7854474cd16d583bc536da70947
tree 4f7a2c41efd791fcda7b2d052142724f06f6d9ff
parent 9788da3a0993a7854474cd16d583bc536da70947
| name | age | message | |
|---|---|---|---|
| |
CHANGELOG | Fri Sep 21 06:30:32 -0700 2007 | [jnewland] |
| |
MIT-LICENSE | Sun Sep 16 10:38:32 -0700 2007 | [jnewland] |
| |
README | Fri Sep 21 06:30:32 -0700 2007 | [jnewland] |
| |
Rakefile | Sun Sep 16 12:03:44 -0700 2007 | [jnewland] |
| |
TODO | Sun Sep 16 16:32:51 -0700 2007 | [jnewland] |
| |
generators/ | Mon Sep 17 06:33:29 -0700 2007 | [jnewland] |
| |
init.rb | Sun Sep 16 10:38:32 -0700 2007 | [jnewland] |
| |
lib/ | Wed Sep 26 05:19:09 -0700 2007 | [jnewland] |
| |
test/ | Fri Sep 21 06:26:48 -0700 2007 | [jnewland] |
README
resouce_this
===========
Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
class PostsController < ActionController::Base
resource_this
end
...will generate the following code:
class PostsController < ActionController::Base
before_filter :load_post, :only => [ :show, :edit, :update, :destroy ]
before_filter :load_posts, :only => [ :index ]
before_filter :new_post, :only => [ :new ]
before_filter :create_post, :only => [ :create ]
before_filter :update_post, :only => [ :update ]
before_filter :destroy_post, :only => [ :destroy ]
protected
def load_post
@post = Post.find(params[:id])
end
def new_post
@post = Post.new
end
def create_post
returning true do
@post = Post.new(params[:post])
@created = @post.save
end
end
def update_post
returning true do
@updated = @post.update_attributes(params[:post])
end
end
def destroy_post
@post = @post.destroy
end
def load_posts
@posts = Post.find(:all)
end
public
def index
respond_to do |format|
format.html
format.xml { render :xml => @posts }
format.js
end
end
def show
respond_to do |format|
format.html
format.xml { render :xml => @post }
format.js
end
end
def new
respond_to do |format|
format.html { render :action => :edit }
format.xml { render :xml => @post }
format.js
end
end
def create
respond_to do |format|
if @created
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to @post }
format.xml { render :xml => @post, :status => :created, :location => @post }
format.js
else
format.html { render :action => :new }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
format.js
end
end
end
def edit
respond_to do |format|
format.html
format.js
end
end
def update
respond_to do |format|
if @updated
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to @post }
format.xml { head :ok }
format.js
else
format.html { render :action => :edit }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
format.js
end
end
end
def destroy
respond_to do |format|
format.html { redirect_to :action => posts_url }
format.xml { head :ok }
format.js
end
end
end
Nested resources like so:
class CommentsController < ActionController::Base
resource_this :nested => [:posts]
end
This generates a very similar controller to the one above with adjusted redirects and one additional before_filter /
loader method pair to grab the parent resource. In this case:
before_filter :load_post
def load_post
@post = Post.find(params[:post_id])
end
The separation of logic - DB operations in before_filters, rendering in the standard resource controller methods - makes
this approach ridiculously easy to customize. Need to load an additional object for the :show action? Slap another
before_filter on it. Need to change the path that the :update action redirects to? Override the :update action with your
new rendering behavior.
A resource_this generator is included - does the same thing as the 'resource' generator but adds 'resource_this' to the
generated controller.
Questions? Comments? Flames? Patches?
===========
jnewland@gmail.com
Copyright
===========
Copyright (c) 2007 Jesse Newland, released under the MIT license



