halorgium-actionpack 3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG +5179 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +409 -0
  4. data/lib/abstract_controller.rb +16 -0
  5. data/lib/abstract_controller/base.rb +158 -0
  6. data/lib/abstract_controller/callbacks.rb +113 -0
  7. data/lib/abstract_controller/exceptions.rb +12 -0
  8. data/lib/abstract_controller/helpers.rb +151 -0
  9. data/lib/abstract_controller/layouts.rb +250 -0
  10. data/lib/abstract_controller/localized_cache.rb +49 -0
  11. data/lib/abstract_controller/logger.rb +61 -0
  12. data/lib/abstract_controller/rendering_controller.rb +188 -0
  13. data/lib/action_controller.rb +72 -0
  14. data/lib/action_controller/base.rb +168 -0
  15. data/lib/action_controller/caching.rb +80 -0
  16. data/lib/action_controller/caching/actions.rb +163 -0
  17. data/lib/action_controller/caching/fragments.rb +116 -0
  18. data/lib/action_controller/caching/pages.rb +154 -0
  19. data/lib/action_controller/caching/sweeping.rb +97 -0
  20. data/lib/action_controller/deprecated.rb +4 -0
  21. data/lib/action_controller/deprecated/integration_test.rb +2 -0
  22. data/lib/action_controller/deprecated/performance_test.rb +1 -0
  23. data/lib/action_controller/dispatch/dispatcher.rb +57 -0
  24. data/lib/action_controller/metal.rb +129 -0
  25. data/lib/action_controller/metal/benchmarking.rb +73 -0
  26. data/lib/action_controller/metal/compatibility.rb +145 -0
  27. data/lib/action_controller/metal/conditional_get.rb +86 -0
  28. data/lib/action_controller/metal/configuration.rb +28 -0
  29. data/lib/action_controller/metal/cookies.rb +105 -0
  30. data/lib/action_controller/metal/exceptions.rb +55 -0
  31. data/lib/action_controller/metal/filter_parameter_logging.rb +77 -0
  32. data/lib/action_controller/metal/flash.rb +162 -0
  33. data/lib/action_controller/metal/head.rb +27 -0
  34. data/lib/action_controller/metal/helpers.rb +115 -0
  35. data/lib/action_controller/metal/hide_actions.rb +47 -0
  36. data/lib/action_controller/metal/http_authentication.rb +312 -0
  37. data/lib/action_controller/metal/layouts.rb +171 -0
  38. data/lib/action_controller/metal/mime_responds.rb +317 -0
  39. data/lib/action_controller/metal/rack_convenience.rb +27 -0
  40. data/lib/action_controller/metal/redirector.rb +22 -0
  41. data/lib/action_controller/metal/render_options.rb +103 -0
  42. data/lib/action_controller/metal/rendering_controller.rb +57 -0
  43. data/lib/action_controller/metal/request_forgery_protection.rb +108 -0
  44. data/lib/action_controller/metal/rescuable.rb +13 -0
  45. data/lib/action_controller/metal/responder.rb +200 -0
  46. data/lib/action_controller/metal/session.rb +15 -0
  47. data/lib/action_controller/metal/session_management.rb +45 -0
  48. data/lib/action_controller/metal/streaming.rb +188 -0
  49. data/lib/action_controller/metal/testing.rb +39 -0
  50. data/lib/action_controller/metal/url_for.rb +41 -0
  51. data/lib/action_controller/metal/verification.rb +130 -0
  52. data/lib/action_controller/middleware.rb +38 -0
  53. data/lib/action_controller/notifications.rb +10 -0
  54. data/lib/action_controller/polymorphic_routes.rb +183 -0
  55. data/lib/action_controller/record_identifier.rb +91 -0
  56. data/lib/action_controller/testing/process.rb +111 -0
  57. data/lib/action_controller/testing/test_case.rb +345 -0
  58. data/lib/action_controller/translation.rb +13 -0
  59. data/lib/action_controller/url_rewriter.rb +204 -0
  60. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  61. data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
  62. data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
  63. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +176 -0
  64. data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
  65. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
  66. data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
  67. data/lib/action_dispatch.rb +70 -0
  68. data/lib/action_dispatch/http/headers.rb +33 -0
  69. data/lib/action_dispatch/http/mime_type.rb +231 -0
  70. data/lib/action_dispatch/http/mime_types.rb +23 -0
  71. data/lib/action_dispatch/http/request.rb +539 -0
  72. data/lib/action_dispatch/http/response.rb +290 -0
  73. data/lib/action_dispatch/http/status_codes.rb +42 -0
  74. data/lib/action_dispatch/http/utils.rb +20 -0
  75. data/lib/action_dispatch/middleware/callbacks.rb +50 -0
  76. data/lib/action_dispatch/middleware/params_parser.rb +79 -0
  77. data/lib/action_dispatch/middleware/rescue.rb +26 -0
  78. data/lib/action_dispatch/middleware/session/abstract_store.rb +208 -0
  79. data/lib/action_dispatch/middleware/session/cookie_store.rb +235 -0
  80. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +47 -0
  81. data/lib/action_dispatch/middleware/show_exceptions.rb +143 -0
  82. data/lib/action_dispatch/middleware/stack.rb +116 -0
  83. data/lib/action_dispatch/middleware/static.rb +44 -0
  84. data/lib/action_dispatch/middleware/string_coercion.rb +29 -0
  85. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +24 -0
  86. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +26 -0
  87. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +10 -0
  88. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +29 -0
  89. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +2 -0
  90. data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +10 -0
  91. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +21 -0
  92. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +2 -0
  93. data/lib/action_dispatch/routing.rb +381 -0
  94. data/lib/action_dispatch/routing/deprecated_mapper.rb +878 -0
  95. data/lib/action_dispatch/routing/mapper.rb +327 -0
  96. data/lib/action_dispatch/routing/route.rb +49 -0
  97. data/lib/action_dispatch/routing/route_set.rb +497 -0
  98. data/lib/action_dispatch/testing/assertions.rb +8 -0
  99. data/lib/action_dispatch/testing/assertions/dom.rb +35 -0
  100. data/lib/action_dispatch/testing/assertions/model.rb +19 -0
  101. data/lib/action_dispatch/testing/assertions/response.rb +145 -0
  102. data/lib/action_dispatch/testing/assertions/routing.rb +144 -0
  103. data/lib/action_dispatch/testing/assertions/selector.rb +639 -0
  104. data/lib/action_dispatch/testing/assertions/tag.rb +123 -0
  105. data/lib/action_dispatch/testing/integration.rb +504 -0
  106. data/lib/action_dispatch/testing/performance_test.rb +15 -0
  107. data/lib/action_dispatch/testing/test_request.rb +83 -0
  108. data/lib/action_dispatch/testing/test_response.rb +131 -0
  109. data/lib/action_pack.rb +24 -0
  110. data/lib/action_pack/version.rb +9 -0
  111. data/lib/action_view.rb +58 -0
  112. data/lib/action_view/base.rb +308 -0
  113. data/lib/action_view/context.rb +44 -0
  114. data/lib/action_view/erb/util.rb +48 -0
  115. data/lib/action_view/helpers.rb +62 -0
  116. data/lib/action_view/helpers/active_model_helper.rb +306 -0
  117. data/lib/action_view/helpers/ajax_helper.rb +68 -0
  118. data/lib/action_view/helpers/asset_tag_helper.rb +830 -0
  119. data/lib/action_view/helpers/atom_feed_helper.rb +198 -0
  120. data/lib/action_view/helpers/cache_helper.rb +39 -0
  121. data/lib/action_view/helpers/capture_helper.rb +168 -0
  122. data/lib/action_view/helpers/date_helper.rb +988 -0
  123. data/lib/action_view/helpers/debug_helper.rb +38 -0
  124. data/lib/action_view/helpers/form_helper.rb +1102 -0
  125. data/lib/action_view/helpers/form_options_helper.rb +600 -0
  126. data/lib/action_view/helpers/form_tag_helper.rb +495 -0
  127. data/lib/action_view/helpers/javascript_helper.rb +208 -0
  128. data/lib/action_view/helpers/number_helper.rb +311 -0
  129. data/lib/action_view/helpers/prototype_helper.rb +1309 -0
  130. data/lib/action_view/helpers/raw_output_helper.rb +9 -0
  131. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  132. data/lib/action_view/helpers/record_tag_helper.rb +58 -0
  133. data/lib/action_view/helpers/sanitize_helper.rb +259 -0
  134. data/lib/action_view/helpers/scriptaculous_helper.rb +226 -0
  135. data/lib/action_view/helpers/tag_helper.rb +151 -0
  136. data/lib/action_view/helpers/text_helper.rb +594 -0
  137. data/lib/action_view/helpers/translation_helper.rb +39 -0
  138. data/lib/action_view/helpers/url_helper.rb +639 -0
  139. data/lib/action_view/locale/en.yml +117 -0
  140. data/lib/action_view/paths.rb +80 -0
  141. data/lib/action_view/render/partials.rb +342 -0
  142. data/lib/action_view/render/rendering.rb +134 -0
  143. data/lib/action_view/safe_buffer.rb +28 -0
  144. data/lib/action_view/template/error.rb +101 -0
  145. data/lib/action_view/template/handler.rb +36 -0
  146. data/lib/action_view/template/handlers.rb +52 -0
  147. data/lib/action_view/template/handlers/builder.rb +17 -0
  148. data/lib/action_view/template/handlers/erb.rb +53 -0
  149. data/lib/action_view/template/handlers/rjs.rb +18 -0
  150. data/lib/action_view/template/resolver.rb +165 -0
  151. data/lib/action_view/template/template.rb +131 -0
  152. data/lib/action_view/template/text.rb +38 -0
  153. data/lib/action_view/test_case.rb +163 -0
  154. metadata +236 -0
@@ -0,0 +1,495 @@
1
+ require 'cgi'
2
+ require 'action_view/helpers/tag_helper'
3
+ require 'active_support/core_ext/object/returning'
4
+
5
+ module ActionView
6
+ module Helpers
7
+ # Provides a number of methods for creating form tags that doesn't rely on an Active Record object assigned to the template like
8
+ # FormHelper does. Instead, you provide the names and values manually.
9
+ #
10
+ # NOTE: The HTML options <tt>disabled</tt>, <tt>readonly</tt>, and <tt>multiple</tt> can all be treated as booleans. So specifying
11
+ # <tt>:disabled => true</tt> will give <tt>disabled="disabled"</tt>.
12
+ module FormTagHelper
13
+ # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
14
+ # ActionController::Base#url_for. The method for the form defaults to POST.
15
+ #
16
+ # ==== Options
17
+ # * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data".
18
+ # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post".
19
+ # If "put", "delete", or another verb is used, a hidden input with name <tt>_method</tt>
20
+ # is added to simulate the verb over post.
21
+ # * A list of parameters to feed to the URL the form will be posted to.
22
+ #
23
+ # ==== Examples
24
+ # form_tag('/posts')
25
+ # # => <form action="/posts" method="post">
26
+ #
27
+ # form_tag('/posts/1', :method => :put)
28
+ # # => <form action="/posts/1" method="put">
29
+ #
30
+ # form_tag('/upload', :multipart => true)
31
+ # # => <form action="/upload" method="post" enctype="multipart/form-data">
32
+ #
33
+ # <% form_tag '/posts' do -%>
34
+ # <div><%= submit_tag 'Save' %></div>
35
+ # <% end -%>
36
+ # # => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
37
+ def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
38
+ html_options = html_options_for_form(url_for_options, options, *parameters_for_url)
39
+ if block_given?
40
+ form_tag_in_block(html_options, &block)
41
+ else
42
+ form_tag_html(html_options)
43
+ end
44
+ end
45
+
46
+ # Creates a dropdown selection box, or if the <tt>:multiple</tt> option is set to true, a multiple
47
+ # choice selection box.
48
+ #
49
+ # Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or
50
+ # associated records. <tt>option_tags</tt> is a string containing the option tags for the select box.
51
+ #
52
+ # ==== Options
53
+ # * <tt>:multiple</tt> - If set to true the selection will allow multiple choices.
54
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
55
+ # * Any other key creates standard HTML attributes for the tag.
56
+ #
57
+ # ==== Examples
58
+ # select_tag "people", "<option>David</option>"
59
+ # # => <select id="people" name="people"><option>David</option></select>
60
+ #
61
+ # select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>"
62
+ # # => <select id="count" name="count"><option>1</option><option>2</option>
63
+ # # <option>3</option><option>4</option></select>
64
+ #
65
+ # select_tag "colors", "<option>Red</option><option>Green</option><option>Blue</option>", :multiple => true
66
+ # # => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option>
67
+ # # <option>Green</option><option>Blue</option></select>
68
+ #
69
+ # select_tag "locations", "<option>Home</option><option selected="selected">Work</option><option>Out</option>"
70
+ # # => <select id="locations" name="locations"><option>Home</option><option selected='selected'>Work</option>
71
+ # # <option>Out</option></select>
72
+ #
73
+ # select_tag "access", "<option>Read</option><option>Write</option>", :multiple => true, :class => 'form_input'
74
+ # # => <select class="form_input" id="access" multiple="multiple" name="access[]"><option>Read</option>
75
+ # # <option>Write</option></select>
76
+ #
77
+ # select_tag "destination", "<option>NYC</option><option>Paris</option><option>Rome</option>", :disabled => true
78
+ # # => <select disabled="disabled" id="destination" name="destination"><option>NYC</option>
79
+ # # <option>Paris</option><option>Rome</option></select>
80
+ def select_tag(name, option_tags = nil, options = {})
81
+ html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
82
+ if blank = options.delete(:include_blank)
83
+ if blank.kind_of?(String)
84
+ option_tags = "<option value=\"\">#{blank}</option>" + option_tags
85
+ else
86
+ option_tags = "<option value=\"\"></option>" + option_tags
87
+ end
88
+ end
89
+ content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
90
+ end
91
+
92
+ # Creates a standard text field; use these text fields to input smaller chunks of text like a username
93
+ # or a search query.
94
+ #
95
+ # ==== Options
96
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
97
+ # * <tt>:size</tt> - The number of visible characters that will fit in the input.
98
+ # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
99
+ # * Any other key creates standard HTML attributes for the tag.
100
+ #
101
+ # ==== Examples
102
+ # text_field_tag 'name'
103
+ # # => <input id="name" name="name" type="text" />
104
+ #
105
+ # text_field_tag 'query', 'Enter your search query here'
106
+ # # => <input id="query" name="query" type="text" value="Enter your search query here" />
107
+ #
108
+ # text_field_tag 'request', nil, :class => 'special_input'
109
+ # # => <input class="special_input" id="request" name="request" type="text" />
110
+ #
111
+ # text_field_tag 'address', '', :size => 75
112
+ # # => <input id="address" name="address" size="75" type="text" value="" />
113
+ #
114
+ # text_field_tag 'zip', nil, :maxlength => 5
115
+ # # => <input id="zip" maxlength="5" name="zip" type="text" />
116
+ #
117
+ # text_field_tag 'payment_amount', '$0.00', :disabled => true
118
+ # # => <input disabled="disabled" id="payment_amount" name="payment_amount" type="text" value="$0.00" />
119
+ #
120
+ # text_field_tag 'ip', '0.0.0.0', :maxlength => 15, :size => 20, :class => "ip-input"
121
+ # # => <input class="ip-input" id="ip" maxlength="15" name="ip" size="20" type="text" value="0.0.0.0" />
122
+ def text_field_tag(name, value = nil, options = {})
123
+ tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
124
+ end
125
+
126
+ # Creates a label field
127
+ #
128
+ # ==== Options
129
+ # * Creates standard HTML attributes for the tag.
130
+ #
131
+ # ==== Examples
132
+ # label_tag 'name'
133
+ # # => <label for="name">Name</label>
134
+ #
135
+ # label_tag 'name', 'Your name'
136
+ # # => <label for="name">Your Name</label>
137
+ #
138
+ # label_tag 'name', nil, :class => 'small_label'
139
+ # # => <label for="name" class="small_label">Name</label>
140
+ def label_tag(name, text = nil, options = {})
141
+ content_tag :label, text || name.to_s.humanize, { "for" => sanitize_to_id(name) }.update(options.stringify_keys)
142
+ end
143
+
144
+ # Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or
145
+ # data that should be hidden from the user.
146
+ #
147
+ # ==== Options
148
+ # * Creates standard HTML attributes for the tag.
149
+ #
150
+ # ==== Examples
151
+ # hidden_field_tag 'tags_list'
152
+ # # => <input id="tags_list" name="tags_list" type="hidden" />
153
+ #
154
+ # hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
155
+ # # => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
156
+ #
157
+ # hidden_field_tag 'collected_input', '', :onchange => "alert('Input collected!')"
158
+ # # => <input id="collected_input" name="collected_input" onchange="alert('Input collected!')"
159
+ # # type="hidden" value="" />
160
+ def hidden_field_tag(name, value = nil, options = {})
161
+ text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
162
+ end
163
+
164
+ # Creates a file upload field. If you are using file uploads then you will also need
165
+ # to set the multipart option for the form tag:
166
+ #
167
+ # <% form_tag '/upload', :multipart => true do %>
168
+ # <label for="file">File to Upload</label> <%= file_field_tag "file" %>
169
+ # <%= submit_tag %>
170
+ # <% end %>
171
+ #
172
+ # The specified URL will then be passed a File object containing the selected file, or if the field
173
+ # was left blank, a StringIO object.
174
+ #
175
+ # ==== Options
176
+ # * Creates standard HTML attributes for the tag.
177
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
178
+ #
179
+ # ==== Examples
180
+ # file_field_tag 'attachment'
181
+ # # => <input id="attachment" name="attachment" type="file" />
182
+ #
183
+ # file_field_tag 'avatar', :class => 'profile-input'
184
+ # # => <input class="profile-input" id="avatar" name="avatar" type="file" />
185
+ #
186
+ # file_field_tag 'picture', :disabled => true
187
+ # # => <input disabled="disabled" id="picture" name="picture" type="file" />
188
+ #
189
+ # file_field_tag 'resume', :value => '~/resume.doc'
190
+ # # => <input id="resume" name="resume" type="file" value="~/resume.doc" />
191
+ #
192
+ # file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg'
193
+ # # => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" />
194
+ #
195
+ # file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html'
196
+ # # => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
197
+ def file_field_tag(name, options = {})
198
+ text_field_tag(name, nil, options.update("type" => "file"))
199
+ end
200
+
201
+ # Creates a password field, a masked text field that will hide the users input behind a mask character.
202
+ #
203
+ # ==== Options
204
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
205
+ # * <tt>:size</tt> - The number of visible characters that will fit in the input.
206
+ # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
207
+ # * Any other key creates standard HTML attributes for the tag.
208
+ #
209
+ # ==== Examples
210
+ # password_field_tag 'pass'
211
+ # # => <input id="pass" name="pass" type="password" />
212
+ #
213
+ # password_field_tag 'secret', 'Your secret here'
214
+ # # => <input id="secret" name="secret" type="password" value="Your secret here" />
215
+ #
216
+ # password_field_tag 'masked', nil, :class => 'masked_input_field'
217
+ # # => <input class="masked_input_field" id="masked" name="masked" type="password" />
218
+ #
219
+ # password_field_tag 'token', '', :size => 15
220
+ # # => <input id="token" name="token" size="15" type="password" value="" />
221
+ #
222
+ # password_field_tag 'key', nil, :maxlength => 16
223
+ # # => <input id="key" maxlength="16" name="key" type="password" />
224
+ #
225
+ # password_field_tag 'confirm_pass', nil, :disabled => true
226
+ # # => <input disabled="disabled" id="confirm_pass" name="confirm_pass" type="password" />
227
+ #
228
+ # password_field_tag 'pin', '1234', :maxlength => 4, :size => 6, :class => "pin-input"
229
+ # # => <input class="pin-input" id="pin" maxlength="4" name="pin" size="6" type="password" value="1234" />
230
+ def password_field_tag(name = "password", value = nil, options = {})
231
+ text_field_tag(name, value, options.update("type" => "password"))
232
+ end
233
+
234
+ # Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
235
+ #
236
+ # ==== Options
237
+ # * <tt>:size</tt> - A string specifying the dimensions (columns by rows) of the textarea (e.g., "25x10").
238
+ # * <tt>:rows</tt> - Specify the number of rows in the textarea
239
+ # * <tt>:cols</tt> - Specify the number of columns in the textarea
240
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
241
+ # * <tt>:escape</tt> - By default, the contents of the text input are HTML escaped.
242
+ # If you need unescaped contents, set this to false.
243
+ # * Any other key creates standard HTML attributes for the tag.
244
+ #
245
+ # ==== Examples
246
+ # text_area_tag 'post'
247
+ # # => <textarea id="post" name="post"></textarea>
248
+ #
249
+ # text_area_tag 'bio', @user.bio
250
+ # # => <textarea id="bio" name="bio">This is my biography.</textarea>
251
+ #
252
+ # text_area_tag 'body', nil, :rows => 10, :cols => 25
253
+ # # => <textarea cols="25" id="body" name="body" rows="10"></textarea>
254
+ #
255
+ # text_area_tag 'body', nil, :size => "25x10"
256
+ # # => <textarea name="body" id="body" cols="25" rows="10"></textarea>
257
+ #
258
+ # text_area_tag 'description', "Description goes here.", :disabled => true
259
+ # # => <textarea disabled="disabled" id="description" name="description">Description goes here.</textarea>
260
+ #
261
+ # text_area_tag 'comment', nil, :class => 'comment_input'
262
+ # # => <textarea class="comment_input" id="comment" name="comment"></textarea>
263
+ def text_area_tag(name, content = nil, options = {})
264
+ options.stringify_keys!
265
+
266
+ if size = options.delete("size")
267
+ options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
268
+ end
269
+
270
+ escape = options.key?("escape") ? options.delete("escape") : true
271
+ content = html_escape(content) if escape
272
+
273
+ content_tag :textarea, content, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
274
+ end
275
+
276
+ # Creates a check box form input tag.
277
+ #
278
+ # ==== Options
279
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
280
+ # * Any other key creates standard HTML options for the tag.
281
+ #
282
+ # ==== Examples
283
+ # check_box_tag 'accept'
284
+ # # => <input id="accept" name="accept" type="checkbox" value="1" />
285
+ #
286
+ # check_box_tag 'rock', 'rock music'
287
+ # # => <input id="rock" name="rock" type="checkbox" value="rock music" />
288
+ #
289
+ # check_box_tag 'receive_email', 'yes', true
290
+ # # => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />
291
+ #
292
+ # check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
293
+ # # => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />
294
+ #
295
+ # check_box_tag 'eula', 'accepted', false, :disabled => true
296
+ # # => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
297
+ def check_box_tag(name, value = "1", checked = false, options = {})
298
+ html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
299
+ html_options["checked"] = "checked" if checked
300
+ tag :input, html_options
301
+ end
302
+
303
+ # Creates a radio button; use groups of radio buttons named the same to allow users to
304
+ # select from a group of options.
305
+ #
306
+ # ==== Options
307
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
308
+ # * Any other key creates standard HTML options for the tag.
309
+ #
310
+ # ==== Examples
311
+ # radio_button_tag 'gender', 'male'
312
+ # # => <input id="gender_male" name="gender" type="radio" value="male" />
313
+ #
314
+ # radio_button_tag 'receive_updates', 'no', true
315
+ # # => <input checked="checked" id="receive_updates_no" name="receive_updates" type="radio" value="no" />
316
+ #
317
+ # radio_button_tag 'time_slot', "3:00 p.m.", false, :disabled => true
318
+ # # => <input disabled="disabled" id="time_slot_300_pm" name="time_slot" type="radio" value="3:00 p.m." />
319
+ #
320
+ # radio_button_tag 'color', "green", true, :class => "color_input"
321
+ # # => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />
322
+ def radio_button_tag(name, value, checked = false, options = {})
323
+ pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase
324
+ pretty_name = name.to_s.gsub(/\[/, "_").gsub(/\]/, "")
325
+ html_options = { "type" => "radio", "name" => name, "id" => "#{pretty_name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys)
326
+ html_options["checked"] = "checked" if checked
327
+ tag :input, html_options
328
+ end
329
+
330
+ # Creates a submit button with the text <tt>value</tt> as the caption.
331
+ #
332
+ # ==== Options
333
+ # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
334
+ # prompt with the question specified. If the user accepts, the form is
335
+ # processed normally, otherwise no action is taken.
336
+ # * <tt>:disabled</tt> - If true, the user will not be able to use this input.
337
+ # * <tt>:disable_with</tt> - Value of this parameter will be used as the value for a disabled version
338
+ # of the submit button when the form is submitted.
339
+ # * Any other key creates standard HTML options for the tag.
340
+ #
341
+ # ==== Examples
342
+ # submit_tag
343
+ # # => <input name="commit" type="submit" value="Save changes" />
344
+ #
345
+ # submit_tag "Edit this article"
346
+ # # => <input name="commit" type="submit" value="Edit this article" />
347
+ #
348
+ # submit_tag "Save edits", :disabled => true
349
+ # # => <input disabled="disabled" name="commit" type="submit" value="Save edits" />
350
+ #
351
+ # submit_tag "Complete sale", :disable_with => "Please wait..."
352
+ # # => <input name="commit" onclick="this.disabled=true;this.value='Please wait...';this.form.submit();"
353
+ # # type="submit" value="Complete sale" />
354
+ #
355
+ # submit_tag nil, :class => "form_submit"
356
+ # # => <input class="form_submit" name="commit" type="submit" />
357
+ #
358
+ # submit_tag "Edit", :disable_with => "Editing...", :class => "edit-button"
359
+ # # => <input class="edit-button" onclick="this.disabled=true;this.value='Editing...';this.form.submit();"
360
+ # # name="commit" type="submit" value="Edit" />
361
+ def submit_tag(value = "Save changes", options = {})
362
+ options.stringify_keys!
363
+
364
+ if disable_with = options.delete("disable_with")
365
+ disable_with = "this.value='#{disable_with}'"
366
+ disable_with << ";#{options.delete('onclick')}" if options['onclick']
367
+
368
+ options["onclick"] = "if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }"
369
+ options["onclick"] << "else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';"
370
+ options["onclick"] << "hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }"
371
+ options["onclick"] << "this.setAttribute('originalValue', this.value);this.disabled = true;#{disable_with};"
372
+ options["onclick"] << "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());"
373
+ options["onclick"] << "if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;"
374
+ end
375
+
376
+ if confirm = options.delete("confirm")
377
+ options["onclick"] ||= 'return true;'
378
+ options["onclick"] = "if (!#{confirm_javascript_function(confirm)}) return false; #{options['onclick']}"
379
+ end
380
+
381
+ tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
382
+ end
383
+
384
+ # Displays an image which when clicked will submit the form.
385
+ #
386
+ # <tt>source</tt> is passed to AssetTagHelper#image_path
387
+ #
388
+ # ==== Options
389
+ # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
390
+ # prompt with the question specified. If the user accepts, the form is
391
+ # processed normally, otherwise no action is taken.
392
+ # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
393
+ # * Any other key creates standard HTML options for the tag.
394
+ #
395
+ # ==== Examples
396
+ # image_submit_tag("login.png")
397
+ # # => <input src="/images/login.png" type="image" />
398
+ #
399
+ # image_submit_tag("purchase.png", :disabled => true)
400
+ # # => <input disabled="disabled" src="/images/purchase.png" type="image" />
401
+ #
402
+ # image_submit_tag("search.png", :class => 'search-button')
403
+ # # => <input class="search-button" src="/images/search.png" type="image" />
404
+ #
405
+ # image_submit_tag("agree.png", :disabled => true, :class => "agree-disagree-button")
406
+ # # => <input class="agree-disagree-button" disabled="disabled" src="/images/agree.png" type="image" />
407
+ def image_submit_tag(source, options = {})
408
+ options.stringify_keys!
409
+
410
+ if confirm = options.delete("confirm")
411
+ options["onclick"] ||= ''
412
+ options["onclick"] += "return #{confirm_javascript_function(confirm)};"
413
+ end
414
+
415
+ tag :input, { "type" => "image", "src" => path_to_image(source) }.update(options.stringify_keys)
416
+ end
417
+
418
+ # Creates a field set for grouping HTML form elements.
419
+ #
420
+ # <tt>legend</tt> will become the fieldset's title (optional as per W3C).
421
+ # <tt>options</tt> accept the same values as tag.
422
+ #
423
+ # ==== Examples
424
+ # <% field_set_tag do %>
425
+ # <p><%= text_field_tag 'name' %></p>
426
+ # <% end %>
427
+ # # => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
428
+ #
429
+ # <% field_set_tag 'Your details' do %>
430
+ # <p><%= text_field_tag 'name' %></p>
431
+ # <% end %>
432
+ # # => <fieldset><legend>Your details</legend><p><input id="name" name="name" type="text" /></p></fieldset>
433
+ #
434
+ # <% field_set_tag nil, :class => 'format' do %>
435
+ # <p><%= text_field_tag 'name' %></p>
436
+ # <% end %>
437
+ # # => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset>
438
+ def field_set_tag(legend = nil, options = nil, &block)
439
+ content = capture(&block)
440
+ concat(tag(:fieldset, options, true))
441
+ concat(content_tag(:legend, legend)) unless legend.blank?
442
+ concat(content)
443
+ concat("</fieldset>".html_safe!)
444
+ end
445
+
446
+ private
447
+ def html_options_for_form(url_for_options, options, *parameters_for_url)
448
+ returning options.stringify_keys do |html_options|
449
+ html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
450
+ html_options["action"] = url_for(url_for_options, *parameters_for_url)
451
+ end
452
+ end
453
+
454
+ def extra_tags_for_form(html_options)
455
+ case method = html_options.delete("method").to_s
456
+ when /^get$/i # must be case-insentive, but can't use downcase as might be nil
457
+ html_options["method"] = "get"
458
+ ''
459
+ when /^post$/i, "", nil
460
+ html_options["method"] = "post"
461
+ protect_against_forgery? ? content_tag(:div, token_tag, :style => 'margin:0;padding:0;display:inline') : ''
462
+ else
463
+ html_options["method"] = "post"
464
+ content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag, :style => 'margin:0;padding:0;display:inline')
465
+ end
466
+ end
467
+
468
+ def form_tag_html(html_options)
469
+ extra_tags = extra_tags_for_form(html_options)
470
+ (tag(:form, html_options, true) + extra_tags).html_safe!
471
+ end
472
+
473
+ def form_tag_in_block(html_options, &block)
474
+ content = capture(&block)
475
+ concat(form_tag_html(html_options))
476
+ concat(content)
477
+ concat("</form>".html_safe!)
478
+ end
479
+
480
+ def token_tag
481
+ unless protect_against_forgery?
482
+ ''
483
+ else
484
+ tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
485
+ end
486
+ end
487
+
488
+ # see http://www.w3.org/TR/html4/types.html#type-name
489
+ def sanitize_to_id(name)
490
+ name.to_s.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "_")
491
+ end
492
+
493
+ end
494
+ end
495
+ end