| Module | Ardes::ResourcesController::Actions |
| In: |
lib/ardes/resources_controller/actions.rb
|
standard CRUD actions, with html, js and xml responses, re-written to mnake best use of resources_cotroller. This helps if you‘re writing controllers that you want to share via mixin or inheritance.
This module is used as the actions for the controller by default, but you can change this behaviour:
resources_controller_for :foos, :actions_include => false # don't include any actions resources_controller_for :foos, :actions_include => Some::Other::Module # use this module instead
The idea is to decouple the model name from the action code.
Here‘s how:
Instead of this:
@post = Post.find(params[:id]) @post = Post.new @posts = Post.find(:all)
do this:
self.resource = find_resource self.resource = new_resource self.resources = find_resources
Instead of this:
format.xml { render :xml => @post }
format.xml { render :xml => @posts }
do this:
format.xml { render :xml => resource }
format.xml { render :xml => resources }
Instead of this:
redirect_to posts_url redirect_to new_post_url
do this:
redirect_to resources_url redirect_to new_resource_url
POST /events POST /events.xml
# File lib/ardes/resources_controller/actions.rb, line 94
94: def create
95: self.resource = new_resource
96:
97: respond_to do |format|
98: if resource.save
99: format.html do
100: flash[:notice] = "#{resource_name.humanize} was successfully created."
101: redirect_to resource_url
102: end
103: format.js
104: format.xml { render :xml => resource, :status => :created, :location => resource_url }
105: else
106: format.html { render :action => "new" }
107: format.js { render :action => "new" }
108: format.xml { render :xml => resource.errors, :status => :unprocessable_entity }
109: end
110: end
111: end
DELETE /events/1 DELETE /events/1.xml
# File lib/ardes/resources_controller/actions.rb, line 136
136: def destroy
137: self.resource = find_resource
138: resource.destroy
139: respond_to do |format|
140: format.html do
141: flash[:notice] = "#{resource_name.humanize} was successfully destroyed."
142: redirect_to resources_url
143: end
144: format.js
145: format.xml { head :ok }
146: end
147: end
GET /events/1/edit
# File lib/ardes/resources_controller/actions.rb, line 83
83: def edit
84: self.resource = find_resource
85: respond_to do |format|
86: format.html # edit.html.erb
87: format.js
88: format.xml { render :xml => resource }
89: end
90: end
GET /events GET /events.xml
# File lib/ardes/resources_controller/actions.rb, line 49
49: def index
50: self.resources = find_resources
51:
52: respond_to do |format|
53: format.html # index.rhtml
54: format.js
55: format.xml { render :xml => resources }
56: end
57: end
GET /events/new
# File lib/ardes/resources_controller/actions.rb, line 72
72: def new
73: self.resource = new_resource
74:
75: respond_to do |format|
76: format.html # new.html.erb
77: format.js
78: format.xml { render :xml => resource }
79: end
80: end
GET /events/1 GET /events/1.xml
# File lib/ardes/resources_controller/actions.rb, line 61
61: def show
62: self.resource = find_resource
63:
64: respond_to do |format|
65: format.html # show.erb.html
66: format.js
67: format.xml { render :xml => resource }
68: end
69: end
PUT /events/1 PUT /events/1.xml
# File lib/ardes/resources_controller/actions.rb, line 115
115: def update
116: self.resource = find_resource
117:
118: respond_to do |format|
119: if resource.update_attributes(params[resource_name])
120: format.html do
121: flash[:notice] = "#{resource_name.humanize} was successfully updated."
122: redirect_to resource_url
123: end
124: format.js
125: format.xml { head :ok }
126: else
127: format.html { render :action => "edit" }
128: format.js { render :action => "edit" }
129: format.xml { render :xml => resource.errors, :status => :unprocessable_entity }
130: end
131: end
132: end