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 (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | ||
| |
README | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
install.rb | ||
| |
lib/ | ||
| |
tasks/ | ||
| |
test/ | ||
| |
uninstall.rb |
README
SexyActions
===========
No more ugly "respond_to" format.
Requirements
============
* Rails 2.x or higher
Example
=======
* before
class UserController < ApplicationController
def show
@user = User.find(params[:id])
respond_to do |type|
type.html
type.xml { render :text => @user.to_xml }
type.js { render :action => "index.rjs" }
end
end
* after
class UserController < ApplicationController
include SexyActions
show {
@user = User.find(params[:id])
}
show.xml {
render :text => @user.to_xml
}
show.js {
render :action => "index.rjs"
}
Mechanism
=========
show {
@user = User.find(params[:id])
}
1. Accessing to unknown "show" method with block invokes UserController.method_missing
2. It defines "show" instance method with given block
show.xml {
render :text => @user.to_xml
}
3. Accessing to unknown "show" method without block returns a SexyActions::Responder instance object
4. ".xml" defines UserController#render_{action}_for_{mime_type} method by SexyActions::Responder#method_missing
http://localhost:3000/user/show/1.xml
5. UserController#show action is called as usal
6. UserController#default_render kicks UserController#render_show_for_xml for mime rendering
Console Coding
==============
% ./script/console
# list action is not defined yet
>> UserController.new.respond_to?(:list)
=> false
# accesing without block causes normal error
>> UserController.list
NoMethodError: undefined method `list' for UserController:Class
from /home/maiha/sexy_actions/vendor/plugins/sexy_actions/lib/sexy_actions.rb:70:in `method_missing'
from (irb):1
# accessing with block means defining action
>> UserController.list { @users = User.find(:all) }
=> proc {@users = User.find(:all)}
# list action was just defined
>> UserController.new.respond_to?(:list)
=> true
# once defined, accessing without block returns a proxy object
>> UserController.list
=> #<SexyActions::Responder:0xb728af34 @owner=UserController, @action="list", @order=[]>
# the proxy object accepts mime rendering logic
>> UserController.list.xml { render :text=>@users.to_xml }
=> UserController
# getting mime rendering method name
>> UserController.list.render_method_for(:xml)
=> "render_list_for_xml"
# mime rendering method is defined as private
>> UserController.new.respond_to? "render_list_for_xml"
=> false
>> UserController.new.respond_to? "render_list_for_xml", true
=> true
Copyright (c) 2008 [maiha@wota.jp], released under the MIT license








