/
actions.rb
150 lines (139 loc) · 4.23 KB
/
actions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
module Ardes#:nodoc:
module ResourcesController
# 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
#
# == Why?
#
# The idea is to decouple the <b>model name</b> from the action code.
#
# Here's how:
#
# === finding and making new resources
# 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
#
# === referring to 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 }
#
# === urls
# Instead of this:
# redirect_to posts_url
# redirect_to new_post_url
#
# do this:
# redirect_to resources_url
# redirect_to new_resource_url
#
module Actions
# GET /events
# GET /events.xml
def index
self.resources = find_resources
respond_to do |format|
format.html # index.rhtml
format.js
format.xml { render :xml => resources }
end
end
# GET /events/1
# GET /events/1.xml
def show
self.resource = find_resource
respond_to do |format|
format.html # show.erb.html
format.js
format.xml { render :xml => resource }
end
end
# GET /events/new
def new
self.resource = new_resource
respond_to do |format|
format.html # new.html.erb
format.js
format.xml { render :xml => resource }
end
end
# GET /events/1/edit
def edit
self.resource = find_resource
respond_to do |format|
format.html # edit.html.erb
format.js
format.xml { render :xml => resource }
end
end
# POST /events
# POST /events.xml
def create
self.resource = new_resource
respond_to do |format|
if resource.save
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully created."
redirect_to resource_url
end
format.js
format.xml { render :xml => resource, :status => :created, :location => resource_url }
else
format.html { render :action => "new" }
format.js { render :action => "new" }
format.xml { render :xml => resource.errors, :status => :unprocessable_entity }
end
end
end
# PUT /events/1
# PUT /events/1.xml
def update
self.resource = find_resource
respond_to do |format|
if resource.update_attributes(params[resource_name])
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully updated."
redirect_to resource_url
end
format.js
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.js { render :action => "edit" }
format.xml { render :xml => resource.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.xml
def destroy
self.resource = find_resource
resource.destroy
respond_to do |format|
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully destroyed."
redirect_to resources_url
end
format.js
format.xml { head :ok }
end
end
end
end
end