actionpack 0.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (100) hide show
  1. data/CHANGELOG +604 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +418 -0
  4. data/RUNNING_UNIT_TESTS +14 -0
  5. data/examples/.htaccess +24 -0
  6. data/examples/address_book/index.rhtml +33 -0
  7. data/examples/address_book/layout.rhtml +8 -0
  8. data/examples/address_book_controller.cgi +9 -0
  9. data/examples/address_book_controller.fcgi +6 -0
  10. data/examples/address_book_controller.rb +52 -0
  11. data/examples/address_book_controller.rbx +4 -0
  12. data/examples/benchmark.rb +52 -0
  13. data/examples/benchmark_with_ar.fcgi +89 -0
  14. data/examples/blog_controller.cgi +53 -0
  15. data/examples/debate/index.rhtml +14 -0
  16. data/examples/debate/new_topic.rhtml +22 -0
  17. data/examples/debate/topic.rhtml +32 -0
  18. data/examples/debate_controller.cgi +57 -0
  19. data/install.rb +93 -0
  20. data/lib/action_controller.rb +47 -0
  21. data/lib/action_controller/assertions/action_pack_assertions.rb +166 -0
  22. data/lib/action_controller/assertions/active_record_assertions.rb +65 -0
  23. data/lib/action_controller/base.rb +626 -0
  24. data/lib/action_controller/benchmarking.rb +49 -0
  25. data/lib/action_controller/cgi_ext/cgi_ext.rb +43 -0
  26. data/lib/action_controller/cgi_ext/cgi_methods.rb +91 -0
  27. data/lib/action_controller/cgi_process.rb +123 -0
  28. data/lib/action_controller/filters.rb +279 -0
  29. data/lib/action_controller/flash.rb +65 -0
  30. data/lib/action_controller/layout.rb +143 -0
  31. data/lib/action_controller/request.rb +92 -0
  32. data/lib/action_controller/rescue.rb +94 -0
  33. data/lib/action_controller/response.rb +15 -0
  34. data/lib/action_controller/scaffolding.rb +183 -0
  35. data/lib/action_controller/session/active_record_store.rb +72 -0
  36. data/lib/action_controller/session/drb_server.rb +9 -0
  37. data/lib/action_controller/session/drb_store.rb +31 -0
  38. data/lib/action_controller/support/class_attribute_accessors.rb +57 -0
  39. data/lib/action_controller/support/class_inheritable_attributes.rb +37 -0
  40. data/lib/action_controller/support/clean_logger.rb +10 -0
  41. data/lib/action_controller/support/cookie_performance_fix.rb +121 -0
  42. data/lib/action_controller/support/inflector.rb +70 -0
  43. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +28 -0
  44. data/lib/action_controller/templates/rescues/diagnostics.rhtml +22 -0
  45. data/lib/action_controller/templates/rescues/layout.rhtml +29 -0
  46. data/lib/action_controller/templates/rescues/missing_template.rhtml +2 -0
  47. data/lib/action_controller/templates/rescues/template_error.rhtml +26 -0
  48. data/lib/action_controller/templates/rescues/unknown_action.rhtml +2 -0
  49. data/lib/action_controller/templates/scaffolds/edit.rhtml +6 -0
  50. data/lib/action_controller/templates/scaffolds/layout.rhtml +29 -0
  51. data/lib/action_controller/templates/scaffolds/list.rhtml +24 -0
  52. data/lib/action_controller/templates/scaffolds/new.rhtml +5 -0
  53. data/lib/action_controller/templates/scaffolds/show.rhtml +9 -0
  54. data/lib/action_controller/test_process.rb +194 -0
  55. data/lib/action_controller/url_rewriter.rb +153 -0
  56. data/lib/action_view.rb +40 -0
  57. data/lib/action_view/base.rb +253 -0
  58. data/lib/action_view/helpers/active_record_helper.rb +171 -0
  59. data/lib/action_view/helpers/date_helper.rb +223 -0
  60. data/lib/action_view/helpers/debug_helper.rb +17 -0
  61. data/lib/action_view/helpers/form_helper.rb +176 -0
  62. data/lib/action_view/helpers/form_options_helper.rb +169 -0
  63. data/lib/action_view/helpers/tag_helper.rb +59 -0
  64. data/lib/action_view/helpers/text_helper.rb +129 -0
  65. data/lib/action_view/helpers/url_helper.rb +72 -0
  66. data/lib/action_view/partials.rb +61 -0
  67. data/lib/action_view/template_error.rb +84 -0
  68. data/lib/action_view/vendor/builder.rb +13 -0
  69. data/lib/action_view/vendor/builder/blankslate.rb +21 -0
  70. data/lib/action_view/vendor/builder/xmlbase.rb +143 -0
  71. data/lib/action_view/vendor/builder/xmlevents.rb +63 -0
  72. data/lib/action_view/vendor/builder/xmlmarkup.rb +288 -0
  73. data/rakefile +105 -0
  74. data/test/abstract_unit.rb +9 -0
  75. data/test/controller/action_pack_assertions_test.rb +295 -0
  76. data/test/controller/active_record_assertions_test.rb +118 -0
  77. data/test/controller/cgi_test.rb +142 -0
  78. data/test/controller/cookie_test.rb +38 -0
  79. data/test/controller/filters_test.rb +159 -0
  80. data/test/controller/flash_test.rb +69 -0
  81. data/test/controller/layout_test.rb +49 -0
  82. data/test/controller/redirect_test.rb +44 -0
  83. data/test/controller/render_test.rb +169 -0
  84. data/test/controller/url_test.rb +318 -0
  85. data/test/fixtures/layouts/builder.rxml +3 -0
  86. data/test/fixtures/layouts/standard.rhtml +1 -0
  87. data/test/fixtures/test/_customer.rhtml +1 -0
  88. data/test/fixtures/test/greeting.rhtml +1 -0
  89. data/test/fixtures/test/hello.rxml +4 -0
  90. data/test/fixtures/test/hello_world.rhtml +1 -0
  91. data/test/fixtures/test/hello_xml_world.rxml +11 -0
  92. data/test/fixtures/test/list.rhtml +1 -0
  93. data/test/template/active_record_helper_test.rb +76 -0
  94. data/test/template/date_helper_test.rb +103 -0
  95. data/test/template/form_helper_test.rb +115 -0
  96. data/test/template/form_options_helper_test.rb +174 -0
  97. data/test/template/tag_helper_test.rb +18 -0
  98. data/test/template/text_helper_test.rb +62 -0
  99. data/test/template/url_helper_test.rb +35 -0
  100. metadata +154 -0
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2004 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,418 @@
1
+ = Action Pack -- On rails from request to response
2
+
3
+ Action Pack splits the response to a web request into a controller part
4
+ (performing the logic) and a view part (rendering a template). This two-step
5
+ approach is known as an action, which will normally create, read, update, or
6
+ delete (CRUD for short) some sort of model part (often backed by a database)
7
+ before choosing either to render a template or redirecting to another action.
8
+
9
+ Action Pack implements these actions as public methods on Action Controllers
10
+ and uses Action Views to implement the template rendering. Action Controllers
11
+ are then responsible for handling all the actions relating to a certain part
12
+ of an application. This grouping usually consists of actions for lists and for
13
+ CRUDs revolving around a single (or a few) model objects. So ContactController
14
+ would be responsible for listing contacts, creating, deleting, and updating
15
+ contacts. A WeblogController could be responsible for both posts and comments.
16
+
17
+ Action View templates are written using embedded Ruby in tags mingled in with
18
+ the HTML. To avoid cluttering the templates with code, a bunch of helper
19
+ classes provide common behavior for forms, dates, and strings. And it's easy
20
+ to add specific helpers to keep the separation as the application evolves.
21
+
22
+ Note: Some of the features, such as scaffolding and form building, are tied to
23
+ ActiveRecord[http://activerecord.rubyonrails.org] (an object-relational
24
+ mapping package), but that doesn't mean that Action Pack depends on Active
25
+ Record. Action Pack is an independent package that can be used with any sort
26
+ of backend (Instiki[http://www.instiki.org], which is based on an older version
27
+ of Action Pack, uses Madeleine for example). Read more about the role Action
28
+ Pack can play when used together with Active Record on
29
+ http://www.rubyonrails.org.
30
+
31
+ A short rundown of the major features:
32
+
33
+ * Actions grouped in controller as methods instead of separate command objects
34
+ and can therefore helper share methods.
35
+
36
+ BlogController < ActionController::Base
37
+ def display
38
+ @customer = find_customer
39
+ end
40
+
41
+ def update
42
+ @customer = find_customer
43
+ @customer.attributes = @params["customer"]
44
+ @customer.save ?
45
+ redirect_to(:action => "display") :
46
+ render("customer/edit")
47
+ end
48
+
49
+ private
50
+ def find_customer() Customer.find(@params["id"]) end
51
+ end
52
+
53
+ Learn more in link:classes/ActionController/Base.html
54
+
55
+
56
+ * Embedded Ruby for templates (no new "easy" template language)
57
+
58
+ <% for post in @posts %>
59
+ Title: <%= post.title %>
60
+ <% end %>
61
+
62
+ All post titles: <%= @post.collect{ |p| p.title }.join ", " %>
63
+
64
+ <% unless @person.is_client? %>
65
+ Not for clients to see...
66
+ <% end %>
67
+
68
+ Learn more in link:classes/ActionView.html
69
+
70
+
71
+ * Builder-based templates (great for XML content, like RSS)
72
+
73
+ xml.rss("version" => "2.0") do
74
+ xml.channel do
75
+ xml.title(@feed_title)
76
+ xml.link(@url)
77
+ xml.description "Basecamp: Recent items"
78
+ xml.language "en-us"
79
+ xml.ttl "40"
80
+
81
+ for item in @recent_items
82
+ xml.item do
83
+ xml.title(item_title(item))
84
+ xml.description(item_description(item))
85
+ xml.pubDate(item_pubDate(item))
86
+ xml.guid(@recent_items.url(item))
87
+ xml.link(@recent_items.url(item))
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+
94
+ * Filters for pre and post processing of the response (as methods, procs, and classes)
95
+
96
+ class WeblogController < ActionController::Base
97
+ before_filter :authenticate, :cache, :audit
98
+ after_filter proc{|c| c.response.body = GZip::compress(c.response.body)}
99
+ after_filter LocalizeFilter
100
+
101
+ def list
102
+ # Before this action is run, the user will be authenticated, the cache
103
+ # will be examined to see if a valid copy of the results already
104
+ # exist, and the action will be logged for auditing.
105
+
106
+ # After this action has run, the output will first be localized then
107
+ # compressed to minimize bandwith usage
108
+ end
109
+
110
+ private
111
+ def authenticate
112
+ # Implement the filter will full access to both request and response
113
+ end
114
+ end
115
+
116
+ Learn more in link:classes/ActionController/Filters/ClassMethods.html
117
+
118
+
119
+ * Helpers for forms, dates, action links, and text
120
+
121
+ <%= text_field "post", "title", "size" => 30 %>
122
+ <%= html_date_select(Date.today) %>
123
+ <%= link_to "New post", :controller => "post", :action => "new" %>
124
+ <%= truncate(post.title, 25) %>
125
+
126
+ Learn more in link:classes/ActionView/Helpers.html
127
+
128
+
129
+ * Layout sharing for template reuse (think simple version of Struts
130
+ Tiles[http://jakarta.apache.org/struts/userGuide/dev_tiles.html])
131
+
132
+ class WeblogController < ActionController::Base
133
+ layout "weblog_layout"
134
+
135
+ def hello_world
136
+ end
137
+ end
138
+
139
+ Layout file (called weblog_layout):
140
+ <html><body><%= @content_for_layout %></body></html>
141
+
142
+ Template for hello_world action:
143
+ <h1>Hello world</h1>
144
+
145
+ Result of running hello_world action:
146
+ <html><body><h1>Hello world</h1></body></html>
147
+
148
+ Learn more in link:classes/ActionController/Layout.html
149
+
150
+
151
+ * Advanced redirection that makes pretty urls easy
152
+
153
+ RewriteRule ^/library/books/([A-Z]+)([0-9]+)/([-_a-zA-Z0-9]+)$ \
154
+ /books_controller.cgi?action=$3&type=$1&code=$2 [QSA] [L]
155
+
156
+ Accessing /library/books/ISBN/0743536703/show calls BooksController#show
157
+
158
+ From that URL, you can rewrite the redirect in a number of ways:
159
+
160
+ redirect_to(:action => "edit") =>
161
+ /library/books/ISBN/0743536703/edit
162
+
163
+ redirect_to(:path_params => { "type" => "XTC", "code" => "12354345" }) =>
164
+ /library/books/XTC/12354345/show
165
+
166
+ redirect_to(:controller_prefix => "admin", :controller => "accounts") =>
167
+ /admin/accounts/
168
+
169
+ Learn more in link:classes/ActionController/Base.html
170
+
171
+
172
+ * Easy testing of both controller and template result through TestRequest/Response
173
+
174
+ class LoginControllerTest < Test::Unit::TestCase
175
+ def setup
176
+ @controller = LoginController.new
177
+ @request = ActionController::TestRequest.new
178
+ @response = ActionController::TestResponse.new
179
+ end
180
+
181
+ def test_failing_authenticate
182
+ process :authenticate, "user_name" => "nop", "password" => ""
183
+ assert_flash_has 'alert'
184
+ assert_redirected_to :action => "index"
185
+ end
186
+ end
187
+
188
+ Learn more in link:classes/ActionController/TestRequest.html
189
+
190
+
191
+ * Automated benchmarking and integrated logging
192
+
193
+ Processing WeblogController#index (for 127.0.0.1 at Fri May 28 00:41:55)
194
+ Parameters: {"action"=>"index", "controller"=>"weblog"}
195
+ Rendering weblog/index (200 OK)
196
+ Completed in 0.029281 (34 reqs/sec)
197
+
198
+ If Active Record is used as the model, you'll have the database debugging
199
+ as well:
200
+
201
+ Processing WeblogController#create (for 127.0.0.1 at Sat Jun 19 14:04:23)
202
+ Params: {"controller"=>"weblog", "action"=>"create",
203
+ "post"=>{"title"=>"this is good"} }
204
+ SQL (0.000627) INSERT INTO posts (title) VALUES('this is good')
205
+ Redirected to http://test/weblog/display/5
206
+ Completed in 0.221764 (4 reqs/sec) | DB: 0.059920 (27%)
207
+
208
+ You specify a logger through a class method, such as:
209
+
210
+ ActionController::Base.logger = Logger.new("Application Log")
211
+ ActionController::Base.logger = Log4r::Logger.new("Application Log")
212
+
213
+
214
+ * Powerful debugging mechanism for local requests
215
+
216
+ All exceptions raised on actions performed on the request of a local user
217
+ will be presented with a tailored debugging screen that includes exception
218
+ message, stack trace, request parameters, session contents, and the
219
+ half-finished response.
220
+
221
+ Learn more in link:classes/ActionController/Rescue.html
222
+
223
+
224
+ * Scaffolding for Action Record model objects
225
+
226
+ require 'account' # must be an Active Record class
227
+ class AccountController < ActionController::Base
228
+ scaffold :account
229
+ end
230
+
231
+ The AccountController now has the full CRUD range of actions and default
232
+ templates: list, show, destroy, new, create, edit, update
233
+
234
+ Learn more in link:classes/ActionController/Scaffolding/ClassMethods.html
235
+
236
+
237
+ * Form building for Active Record model objects
238
+
239
+ The post object has a title (varchar), content (text), and
240
+ written_on (date)
241
+
242
+ <%= form "post" %>
243
+
244
+ ...will generate something like (the selects will have more options of
245
+ course):
246
+
247
+ <form action="create" method="POST">
248
+ <p>
249
+ <b>Title:</b><br/>
250
+ <input type="text" name="post[title]" value="<%= @post.title %>" />
251
+ </p>
252
+ <p>
253
+ <b>Content:</b><br/>
254
+ <textarea name="post[content]"><%= @post.title %></textarea>
255
+ </p>
256
+ <p>
257
+ <b>Written on:</b><br/>
258
+ <select name='post[written_on(3i)]'><option>18</option></select>
259
+ <select name='post[written_on(2i)]'><option value='7'>July</option></select>
260
+ <select name='post[written_on(1i)]'><option>2004</option></select>
261
+ </p>
262
+
263
+ <input type="submit" value="Create">
264
+ </form>
265
+
266
+ This form generates a @params["post"] array that can be used directly in a save action:
267
+
268
+ class WeblogController < ActionController::Base
269
+ def save
270
+ post = Post.create(@params["post"])
271
+ redirect_to :action => "display", :path_params => { "id" => post.id }
272
+ end
273
+ end
274
+
275
+ Learn more in link:classes/ActionView/Helpers/ActiveRecordHelper.html
276
+
277
+
278
+ * Automated mapping of URLs to controller/action pairs through Apache's
279
+ mod_rewrite
280
+
281
+ Requesting /blog/display/5 will call BlogController#display and
282
+ make 5 available as an instance variable through @params["id"]
283
+
284
+
285
+ * Runs on top of CGI, FCGI, and mod_ruby
286
+
287
+ See the address_book_controller example for all three forms
288
+
289
+
290
+ == Simple example
291
+
292
+ This example will implement a simple weblog system using inline templates and
293
+ an Active Record model. The first thing we need to do is setup an .htaccess to
294
+ interpret pretty URLs into something the controller can use. Let's use the
295
+ simplest form for starters:
296
+
297
+ RewriteRule ^weblog/([-_a-zA-Z0-9]+)/([0-9]+)$ \
298
+ /weblog_controller.cgi?action=$2&id=$3 [QSA]
299
+ RewriteRule ^weblog/([-_a-zA-Z0-9]+)$ \
300
+ /weblog_controller.cgi?action=$2 [QSA]
301
+ RewriteRule ^weblog/$ \
302
+ /weblog_controller.cgi?action=index [QSA]
303
+
304
+ Now we'll be able to access URLs like weblog/display/5 and have
305
+ WeblogController#display called with { "id" => 5 } in the @params array
306
+ available for the action. So let's build that WeblogController with just a few
307
+ methods:
308
+
309
+ require 'action_controller'
310
+ require 'post'
311
+ class WeblogController < ActionController::Base
312
+ layout "weblog/layout"
313
+
314
+ def index
315
+ @posts = Post.find_all
316
+ end
317
+
318
+ def display
319
+ @post = Post.find(@params["id"])
320
+ end
321
+
322
+ def new
323
+ @post = Post.new
324
+ end
325
+
326
+ def create
327
+ @post = Post.create(@params["post"])
328
+ @post.save
329
+ redirect_to :action => "display", :id => @post.id
330
+ end
331
+ end
332
+
333
+ WeblogController::Base.template_root = File.dirname(__FILE__)
334
+ WeblogController.process_cgi if $0 == __FILE__
335
+
336
+ The last two lines are responsible for telling ActionController where the
337
+ template files are located and actually running the controller on a new
338
+ request from the web-server (like to be Apache).
339
+
340
+ And the templates look like this:
341
+
342
+ weblog/layout.rhtml:
343
+ <html><body>
344
+ <%= @content_for_layout %>
345
+ </body></html>
346
+
347
+ weblog/index.rhtml:
348
+ <% for post in @posts %>
349
+ <p><%= link_to(post.title, :action => "display", :id => post.id %></p>
350
+ <% end %>
351
+
352
+ weblog/display.rhtml:
353
+ <p>
354
+ <b><%= post.title %></b><br/>
355
+ <b><%= post.content %></b>
356
+ </p>
357
+
358
+ weblog/new.rhtml:
359
+ <%= form "post" %>
360
+
361
+ This simple setup will list all the posts in the system on the index page,
362
+ which is called by accessing /weblog/. It uses the form builder for the Active
363
+ Record model to make the new screen, which in turns hand everything over to
364
+ the create action (that's the default target for the form builder when given a
365
+ new model). After creating the post, it'll redirect to the display page using
366
+ an URL such as /weblog/display/5 (where 5 is the id of the post.
367
+
368
+
369
+ == Examples
370
+
371
+ Action Pack ships with three examples that all demonstrate an increasingly
372
+ detailed view of the possibilities. First is blog_controller that is just a
373
+ single file for the whole MVC (but still split into separate parts). Second is
374
+ the debate_controller that uses separate template files and multiple screens.
375
+ Third is the address_book_controller that uses the layout feature to separate
376
+ template casing from content.
377
+
378
+ Please note that you might need to change the "shebang" line to
379
+ #!/usr/local/env ruby, if your Ruby is not placed in /usr/local/bin/ruby
380
+
381
+
382
+ == Download
383
+
384
+ The latest version of Action Pack can be found at
385
+
386
+ * http://rubyforge.org/project/showfiles.php?group_id=249
387
+
388
+ Documentation can be found at
389
+
390
+ * http://actionpack.rubyonrails.org
391
+
392
+
393
+ == Installation
394
+
395
+ You can install Action Pack with the following command.
396
+
397
+ % [sudo] ruby install.rb
398
+
399
+ from its distribution directory.
400
+
401
+
402
+ == License
403
+
404
+ Action Pack is released under the same license as Ruby.
405
+
406
+
407
+ == Support
408
+
409
+ The Action Pack homepage is http://actionpack.rubyonrails.org. You can find
410
+ the Action Pack RubyForge page at http://rubyforge.org/projects/actionpack.
411
+ And as Jim from Rake says:
412
+
413
+ Feel free to submit commits or feature requests. If you send a patch,
414
+ remember to update the corresponding unit tests. If fact, I prefer
415
+ new feature to be submitted in the form of new unit tests.
416
+
417
+ For other information, feel free to ask on the ruby-talk mailing list (which
418
+ is mirrored to comp.lang.ruby) or contact mailto:david@loudthinking.com.
@@ -0,0 +1,14 @@
1
+ == Running with Rake
2
+
3
+ The easiest way to run the unit tests is through Rake. The default task runs
4
+ the entire test suite for all classes. For more information, checkout the
5
+ full array of rake tasks with "rake -T"
6
+
7
+ Rake can be found at http://rake.rubyforge.org
8
+
9
+ == Running by hand
10
+
11
+ If you only want to run a single test suite, or don't want to bother with Rake,
12
+ you can do so with something like:
13
+
14
+ ruby controller/base_test.rb