actionpack 3.0.0.beta2 → 3.0.0.beta3
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.
- data/CHANGELOG +37 -0
- data/lib/abstract_controller/rendering.rb +9 -11
- data/lib/action_controller.rb +0 -1
- data/lib/action_controller/base.rb +45 -44
- data/lib/action_controller/caching/pages.rb +2 -2
- data/lib/action_controller/deprecated/base.rb +21 -5
- data/lib/action_controller/metal/compatibility.rb +0 -2
- data/lib/action_controller/metal/cookies.rb +1 -1
- data/lib/action_controller/metal/helpers.rb +1 -1
- data/lib/action_controller/metal/http_authentication.rb +12 -5
- data/lib/action_controller/metal/responder.rb +3 -2
- data/lib/action_controller/polymorphic_routes.rb +1 -1
- data/lib/action_controller/railtie.rb +6 -1
- data/lib/action_controller/record_identifier.rb +2 -2
- data/lib/action_controller/test_case.rb +2 -0
- data/lib/action_dispatch/http/mime_negotiation.rb +2 -2
- data/lib/action_dispatch/http/request.rb +39 -21
- data/lib/action_dispatch/middleware/cookies.rb +18 -22
- data/lib/action_dispatch/middleware/params_parser.rb +8 -9
- data/lib/action_dispatch/middleware/session/cookie_store.rb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +1 -1
- data/lib/action_dispatch/railtie.rb +1 -0
- data/lib/action_dispatch/routing.rb +24 -12
- data/lib/action_dispatch/routing/deprecated_mapper.rb +1 -1
- data/lib/action_dispatch/routing/mapper.rb +77 -51
- data/lib/action_dispatch/routing/route_set.rb +9 -5
- data/lib/action_dispatch/routing/url_for.rb +2 -3
- data/lib/action_dispatch/testing/integration.rb +24 -11
- data/lib/action_dispatch/testing/test_request.rb +2 -0
- data/lib/action_pack/version.rb +1 -1
- data/lib/action_view/base.rb +8 -2
- data/lib/action_view/helpers.rb +3 -6
- data/lib/action_view/helpers/active_model_helper.rb +20 -265
- data/lib/action_view/helpers/asset_tag_helper.rb +46 -9
- data/lib/action_view/helpers/atom_feed_helper.rb +2 -2
- data/lib/action_view/helpers/form_helper.rb +161 -120
- data/lib/action_view/helpers/form_options_helper.rb +0 -2
- data/lib/action_view/helpers/form_tag_helper.rb +76 -13
- data/lib/action_view/helpers/url_helper.rb +48 -21
- data/lib/action_view/locale/en.yml +0 -8
- data/lib/action_view/lookup_context.rb +11 -4
- data/lib/action_view/render/layouts.rb +32 -18
- data/lib/action_view/render/partials.rb +2 -2
- data/lib/action_view/template.rb +10 -1
- data/lib/action_view/test_case.rb +6 -1
- metadata +7 -8
- data/lib/action_controller/metal/verification.rb +0 -130
@@ -11,7 +11,7 @@ module ActionDispatch
|
|
11
11
|
|
12
12
|
PARAMETERS_KEY = 'action_dispatch.request.path_parameters'
|
13
13
|
|
14
|
-
class Dispatcher
|
14
|
+
class Dispatcher #:nodoc:
|
15
15
|
def initialize(options={})
|
16
16
|
@defaults = options[:defaults]
|
17
17
|
@glob_param = options.delete(:glob)
|
@@ -187,18 +187,19 @@ module ActionDispatch
|
|
187
187
|
|
188
188
|
attr_accessor :routes, :named_routes
|
189
189
|
attr_accessor :disable_clear_and_finalize, :resources_path_names
|
190
|
-
attr_accessor :default_url_options
|
190
|
+
attr_accessor :default_url_options, :request_class
|
191
191
|
|
192
192
|
def self.default_resources_path_names
|
193
193
|
{ :new => 'new', :edit => 'edit' }
|
194
194
|
end
|
195
195
|
|
196
|
-
def initialize
|
196
|
+
def initialize(request_class = ActionDispatch::Request)
|
197
197
|
self.routes = []
|
198
198
|
self.named_routes = NamedRouteCollection.new
|
199
199
|
self.resources_path_names = self.class.default_resources_path_names.dup
|
200
200
|
self.controller_namespaces = Set.new
|
201
201
|
self.default_url_options = {}
|
202
|
+
self.request_class = request_class
|
202
203
|
|
203
204
|
@disable_clear_and_finalize = false
|
204
205
|
clear!
|
@@ -232,7 +233,10 @@ module ActionDispatch
|
|
232
233
|
@finalized = false
|
233
234
|
routes.clear
|
234
235
|
named_routes.clear
|
235
|
-
@set = ::Rack::Mount::RouteSet.new(
|
236
|
+
@set = ::Rack::Mount::RouteSet.new(
|
237
|
+
:parameters_key => PARAMETERS_KEY,
|
238
|
+
:request_class => request_class
|
239
|
+
)
|
236
240
|
end
|
237
241
|
|
238
242
|
def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
|
@@ -281,7 +285,7 @@ module ActionDispatch
|
|
281
285
|
route
|
282
286
|
end
|
283
287
|
|
284
|
-
class Generator
|
288
|
+
class Generator #:nodoc:
|
285
289
|
attr_reader :options, :recall, :set, :script_name, :named_route
|
286
290
|
|
287
291
|
def initialize(options, recall, set, extras = false)
|
@@ -4,8 +4,7 @@ module ActionDispatch
|
|
4
4
|
# is also possible: an URL can be generated from one of your routing definitions.
|
5
5
|
# URL generation functionality is centralized in this module.
|
6
6
|
#
|
7
|
-
# See ActionDispatch::Routing
|
8
|
-
# information about routing and routes.rb.
|
7
|
+
# See ActionDispatch::Routing for general information about routing and routes.rb.
|
9
8
|
#
|
10
9
|
# <b>Tip:</b> If you need to generate URLs from your models or some other place,
|
11
10
|
# then ActionController::UrlFor is what you're looking for. Read on for
|
@@ -64,7 +63,7 @@ module ActionDispatch
|
|
64
63
|
# named routes. For example, suppose that you have a 'users' resource in your
|
65
64
|
# <b>routes.rb</b>:
|
66
65
|
#
|
67
|
-
#
|
66
|
+
# resources :users
|
68
67
|
#
|
69
68
|
# This generates, among other things, the method <tt>users_path</tt>. By default,
|
70
69
|
# this method is accessible from your controllers, views and mailers. If you need
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'stringio'
|
2
2
|
require 'uri'
|
3
|
-
require 'active_support/core_ext/
|
3
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
4
4
|
require 'rack/test'
|
5
5
|
require 'test/unit/assertions'
|
6
6
|
|
@@ -137,7 +137,10 @@ module ActionDispatch
|
|
137
137
|
end
|
138
138
|
|
139
139
|
# The hostname used in the last request.
|
140
|
-
|
140
|
+
def host
|
141
|
+
@host || DEFAULT_HOST
|
142
|
+
end
|
143
|
+
attr_writer :host
|
141
144
|
|
142
145
|
# The remote_addr used in the last request.
|
143
146
|
attr_accessor :remote_addr
|
@@ -148,7 +151,7 @@ module ActionDispatch
|
|
148
151
|
# A map of the cookies returned by the last response, and which will be
|
149
152
|
# sent with the next request.
|
150
153
|
def cookies
|
151
|
-
|
154
|
+
_mock_session.cookie_jar
|
152
155
|
end
|
153
156
|
|
154
157
|
# A reference to the controller instance used by the last request.
|
@@ -189,8 +192,8 @@ module ActionDispatch
|
|
189
192
|
# session.reset!
|
190
193
|
def reset!
|
191
194
|
@https = false
|
192
|
-
@mock_session = Rack::MockSession.new(@app, DEFAULT_HOST)
|
193
195
|
@controller = @request = @response = nil
|
196
|
+
@_mock_session = nil
|
194
197
|
@request_count = 0
|
195
198
|
|
196
199
|
self.host = DEFAULT_HOST
|
@@ -234,6 +237,9 @@ module ActionDispatch
|
|
234
237
|
end
|
235
238
|
|
236
239
|
private
|
240
|
+
def _mock_session
|
241
|
+
@_mock_session ||= Rack::MockSession.new(@app, host)
|
242
|
+
end
|
237
243
|
|
238
244
|
# Performs the actual request.
|
239
245
|
def process(method, path, parameters = nil, rack_environment = nil)
|
@@ -254,7 +260,7 @@ module ActionDispatch
|
|
254
260
|
:method => method,
|
255
261
|
:params => parameters,
|
256
262
|
|
257
|
-
"SERVER_NAME" => host,
|
263
|
+
"SERVER_NAME" => host.split(':')[0],
|
258
264
|
"SERVER_PORT" => (https? ? "443" : "80"),
|
259
265
|
"HTTPS" => https? ? "on" : "off",
|
260
266
|
"rack.url_scheme" => https? ? "https" : "http",
|
@@ -263,21 +269,28 @@ module ActionDispatch
|
|
263
269
|
"HTTP_HOST" => host,
|
264
270
|
"REMOTE_ADDR" => remote_addr,
|
265
271
|
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
|
266
|
-
"HTTP_ACCEPT" => accept
|
267
|
-
|
268
|
-
"action_dispatch.show_exceptions" => false
|
272
|
+
"HTTP_ACCEPT" => accept
|
269
273
|
}
|
270
274
|
|
275
|
+
session = Rack::Test::Session.new(_mock_session)
|
276
|
+
|
271
277
|
(rack_environment || {}).each do |key, value|
|
272
278
|
env[key] = value
|
273
279
|
end
|
274
280
|
|
275
|
-
|
276
|
-
|
281
|
+
# NOTE: rack-test v0.5 doesn't build a default uri correctly
|
282
|
+
# Make sure requested path is always a full uri
|
283
|
+
uri = URI.parse('/')
|
284
|
+
uri.scheme ||= env['rack.url_scheme']
|
285
|
+
uri.host ||= env['SERVER_NAME']
|
286
|
+
uri.port ||= env['SERVER_PORT'].try(:to_i)
|
287
|
+
uri += path
|
288
|
+
|
289
|
+
session.request(uri.to_s, env)
|
277
290
|
|
278
291
|
@request_count += 1
|
279
292
|
@request = ActionDispatch::Request.new(session.last_request.env)
|
280
|
-
response =
|
293
|
+
response = _mock_session.last_response
|
281
294
|
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
|
282
295
|
@html_document = nil
|
283
296
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/core_ext/object/blank'
|
2
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
2
3
|
|
3
4
|
module ActionDispatch
|
4
5
|
class TestRequest < Request
|
@@ -9,6 +10,7 @@ module ActionDispatch
|
|
9
10
|
end
|
10
11
|
|
11
12
|
def initialize(env = {})
|
13
|
+
env = Rails.application.env_defaults.merge(env) if defined?(Rails.application)
|
12
14
|
super(DEFAULT_ENV.merge(env))
|
13
15
|
|
14
16
|
self.host = 'test.host'
|
data/lib/action_pack/version.rb
CHANGED
data/lib/action_view/base.rb
CHANGED
@@ -24,9 +24,10 @@ module ActionView #:nodoc:
|
|
24
24
|
# The loop is setup in regular embedding tags <% %> and the name is written using the output embedding tag <%= %>. Note that this
|
25
25
|
# is not just a usage suggestion. Regular output functions like print or puts won't work with ERb templates. So this would be wrong:
|
26
26
|
#
|
27
|
+
# <%# WRONG %>
|
27
28
|
# Hi, Mr. <% puts "Frodo" %>
|
28
29
|
#
|
29
|
-
# If you absolutely must write from within a function
|
30
|
+
# If you absolutely must write from within a function use +concat+.
|
30
31
|
#
|
31
32
|
# <%- and -%> suppress leading and trailing whitespace, including the trailing newline, and can be used interchangeably with <% and %>.
|
32
33
|
#
|
@@ -168,6 +169,8 @@ module ActionView #:nodoc:
|
|
168
169
|
remove_method :helpers
|
169
170
|
attr_reader :helpers
|
170
171
|
|
172
|
+
class_attribute :_router
|
173
|
+
|
171
174
|
class << self
|
172
175
|
delegate :erb_trim_mode=, :to => 'ActionView::Template::Handlers::ERB'
|
173
176
|
delegate :logger, :to => 'ActionController::Base', :allow_nil => true
|
@@ -204,7 +207,10 @@ module ActionView #:nodoc:
|
|
204
207
|
@assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
|
205
208
|
@helpers = self.class.helpers || Module.new
|
206
209
|
|
207
|
-
@_controller
|
210
|
+
if @_controller = controller
|
211
|
+
@_request = controller.request if controller.respond_to?(:request)
|
212
|
+
end
|
213
|
+
|
208
214
|
@_config = ActiveSupport::InheritableOptions.new(controller.config) if controller && controller.respond_to?(:config)
|
209
215
|
@_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
|
210
216
|
@_virtual_path = nil
|
data/lib/action_view/helpers.rb
CHANGED
@@ -29,16 +29,13 @@ module ActionView #:nodoc:
|
|
29
29
|
autoload :TranslationHelper
|
30
30
|
autoload :UrlHelper
|
31
31
|
|
32
|
-
|
33
|
-
base.extend(ClassMethods)
|
34
|
-
end
|
32
|
+
extend ActiveSupport::Concern
|
35
33
|
|
36
|
-
|
37
|
-
|
34
|
+
included do
|
35
|
+
extend SanitizeHelper::ClassMethods
|
38
36
|
end
|
39
37
|
|
40
38
|
include ActiveSupport::Benchmarkable
|
41
|
-
|
42
39
|
include ActiveModelHelper
|
43
40
|
include AssetTagHelper
|
44
41
|
include AtomFeedHelper
|
@@ -1,8 +1,6 @@
|
|
1
|
-
require 'cgi'
|
2
1
|
require 'action_view/helpers/form_helper'
|
3
2
|
require 'active_support/core_ext/class/attribute_accessors'
|
4
3
|
require 'active_support/core_ext/enumerable'
|
5
|
-
require 'active_support/core_ext/kernel/reporting'
|
6
4
|
require 'active_support/core_ext/object/blank'
|
7
5
|
|
8
6
|
module ActionView
|
@@ -14,252 +12,29 @@ module ActionView
|
|
14
12
|
end
|
15
13
|
|
16
14
|
module Helpers
|
17
|
-
# The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the +form+
|
18
|
-
# method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
|
19
|
-
# is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
|
20
|
-
# In that case, it's better to use the +input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
|
21
15
|
module ActiveModelHelper
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
def input(record_name, method, options = {})
|
28
|
-
InstanceTag.new(record_name, method, self).to_tag(options)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
|
32
|
-
# has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
|
33
|
-
#
|
34
|
-
# form("post")
|
35
|
-
#
|
36
|
-
# would yield a form like the following (modulus formatting):
|
37
|
-
#
|
38
|
-
# <form action='/posts/create' method='post'>
|
39
|
-
# <p>
|
40
|
-
# <label for="post_title">Title</label><br />
|
41
|
-
# <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
42
|
-
# </p>
|
43
|
-
# <p>
|
44
|
-
# <label for="post_body">Body</label><br />
|
45
|
-
# <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
|
46
|
-
# </p>
|
47
|
-
# <input name="commit" type="submit" value="Create" />
|
48
|
-
# </form>
|
49
|
-
#
|
50
|
-
# It's possible to specialize the form builder by using a different action name and by supplying another
|
51
|
-
# block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
|
52
|
-
#
|
53
|
-
# form("entry",
|
54
|
-
# :action => "sign",
|
55
|
-
# :input_block => Proc.new { |record, column|
|
56
|
-
# "#{column.human_name}: #{input(record, column.name)}<br />"
|
57
|
-
# })
|
58
|
-
#
|
59
|
-
# would yield a form like the following (modulus formatting):
|
60
|
-
#
|
61
|
-
# <form action="/entries/sign" method="post">
|
62
|
-
# Message:
|
63
|
-
# <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
|
64
|
-
# <input name="commit" type="submit" value="Sign" />
|
65
|
-
# </form>
|
66
|
-
#
|
67
|
-
# It's also possible to add additional content to the form by giving it a block, such as:
|
68
|
-
#
|
69
|
-
# form("entry", :action => "sign") do |form|
|
70
|
-
# form << content_tag("b", "Department")
|
71
|
-
# form << collection_select("department", "id", @departments, "id", "name")
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# The following options are available:
|
75
|
-
#
|
76
|
-
# * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
|
77
|
-
# * <tt>:input_block</tt> - Specialize the output using a different block, see above.
|
78
|
-
# * <tt>:method</tt> - The method used when submitting the form (default: +post+).
|
79
|
-
# * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
|
80
|
-
# * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
|
81
|
-
def form(record_name, options = {})
|
82
|
-
record = instance_variable_get("@#{record_name}")
|
83
|
-
record = convert_to_model(record)
|
84
|
-
|
85
|
-
options = options.symbolize_keys
|
86
|
-
options[:action] ||= record.persisted? ? "update" : "create"
|
87
|
-
action = url_for(:action => options[:action], :id => record)
|
88
|
-
|
89
|
-
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
|
90
|
-
|
91
|
-
contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
|
92
|
-
contents.safe_concat hidden_field(record_name, :id) if record.persisted?
|
93
|
-
contents.safe_concat all_input_tags(record, record_name, options)
|
94
|
-
yield contents if block_given?
|
95
|
-
contents.safe_concat submit_tag(submit_value)
|
96
|
-
contents.safe_concat('</form>')
|
97
|
-
end
|
98
|
-
|
99
|
-
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
|
100
|
-
# This error message is wrapped in a <tt>DIV</tt> tag by default or with <tt>:html_tag</tt> if specified,
|
101
|
-
# which can be extended to include a <tt>:prepend_text</tt> and/or <tt>:append_text</tt> (to properly explain
|
102
|
-
# the error), and a <tt>:css_class</tt> to style it accordingly. +object+ should either be the name of an
|
103
|
-
# instance variable or the actual object. The method can be passed in either as a string or a symbol.
|
104
|
-
# As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
|
105
|
-
#
|
106
|
-
# <%= error_message_on "post", "title" %>
|
107
|
-
# # => <div class="formError">can't be empty</div>
|
108
|
-
#
|
109
|
-
# <%= error_message_on @post, :title %>
|
110
|
-
# # => <div class="formError">can't be empty</div>
|
111
|
-
#
|
112
|
-
# <%= error_message_on "post", "title",
|
113
|
-
# :prepend_text => "Title simply ",
|
114
|
-
# :append_text => " (or it won't work).",
|
115
|
-
# :html_tag => "span",
|
116
|
-
# :css_class => "inputError" %>
|
117
|
-
# # => <span class="inputError">Title simply can't be empty (or it won't work).</span>
|
118
|
-
def error_message_on(object, method, *args)
|
119
|
-
options = args.extract_options!
|
120
|
-
unless args.empty?
|
121
|
-
ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
|
122
|
-
'prepend_text, append_text, html_tag, and css_class arguments', caller)
|
123
|
-
|
124
|
-
options[:prepend_text] = args[0] || ''
|
125
|
-
options[:append_text] = args[1] || ''
|
126
|
-
options[:html_tag] = args[2] || 'div'
|
127
|
-
options[:css_class] = args[3] || 'formError'
|
128
|
-
end
|
129
|
-
options.reverse_merge!(:prepend_text => '', :append_text => '', :html_tag => 'div', :css_class => 'formError')
|
130
|
-
|
131
|
-
object = convert_to_model(object)
|
132
|
-
|
133
|
-
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
|
134
|
-
(errors = obj.errors[method]).presence
|
135
|
-
content_tag(options[:html_tag],
|
136
|
-
(options[:prepend_text].html_safe << errors.first).safe_concat(options[:append_text]),
|
137
|
-
:class => options[:css_class]
|
138
|
-
)
|
139
|
-
else
|
140
|
-
''
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
|
145
|
-
# given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
|
146
|
-
# provided.
|
147
|
-
#
|
148
|
-
# This <tt>DIV</tt> can be tailored by the following options:
|
149
|
-
#
|
150
|
-
# * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
|
151
|
-
# * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
|
152
|
-
# * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
|
153
|
-
# * <tt>:object</tt> - The object (or array of objects) for which to display errors,
|
154
|
-
# if you need to escape the instance variable convention.
|
155
|
-
# * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
|
156
|
-
# If <tt>:object_name</tt> is not set, the name of the first object will be used.
|
157
|
-
# * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
|
158
|
-
# or an empty string to avoid the header message altogether. (Default: "X errors
|
159
|
-
# prohibited this object from being saved").
|
160
|
-
# * <tt>:message</tt> - The explanation message after the header message and before
|
161
|
-
# the error list. Pass +nil+ or an empty string to avoid the explanation message
|
162
|
-
# altogether. (Default: "There were problems with the following fields:").
|
163
|
-
#
|
164
|
-
# To specify the display for one object, you simply provide its name as a parameter.
|
165
|
-
# For example, for the <tt>@user</tt> model:
|
166
|
-
#
|
167
|
-
# error_messages_for 'user'
|
168
|
-
#
|
169
|
-
# You can also supply an object:
|
170
|
-
#
|
171
|
-
# error_messages_for @user
|
172
|
-
#
|
173
|
-
# This will use the last part of the model name in the presentation. For instance, if
|
174
|
-
# this is a MyKlass::User object, this will use "user" as the name in the String. This
|
175
|
-
# is taken from MyKlass::User.model_name.human, which can be overridden.
|
176
|
-
#
|
177
|
-
# To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
|
178
|
-
# will be the name used in the header message:
|
179
|
-
#
|
180
|
-
# error_messages_for 'user_common', 'user', :object_name => 'user'
|
181
|
-
#
|
182
|
-
# You can also use a number of objects, which will have the same naming semantics
|
183
|
-
# as a single object.
|
184
|
-
#
|
185
|
-
# error_messages_for @user, @post
|
186
|
-
#
|
187
|
-
# If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> parameter which gives the actual
|
188
|
-
# object (or array of objects to use):
|
189
|
-
#
|
190
|
-
# error_messages_for 'user', :object => @question.user
|
191
|
-
#
|
192
|
-
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
|
193
|
-
# you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
|
194
|
-
# instance yourself and set it up. View the source of this method to see how easy it is.
|
195
|
-
def error_messages_for(*params)
|
196
|
-
options = params.extract_options!.symbolize_keys
|
197
|
-
|
198
|
-
objects = Array.wrap(options.delete(:object) || params).map do |object|
|
199
|
-
object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
|
200
|
-
object = convert_to_model(object)
|
201
|
-
|
202
|
-
if object.class.respond_to?(:model_name)
|
203
|
-
options[:object_name] ||= object.class.model_name.human.downcase
|
16
|
+
%w(input form error_messages_for error_message_on).each do |method|
|
17
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
18
|
+
def #{method}(*args)
|
19
|
+
ActiveSupport::Deprecation.warn "#{method} was removed from Rails and is now available as a plugin. " <<
|
20
|
+
"Please install it with `rails plugin install git://github.com/rails/dynamic_form.git`.", caller
|
204
21
|
end
|
22
|
+
RUBY
|
23
|
+
end
|
24
|
+
end
|
205
25
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
unless count.zero?
|
213
|
-
html = {}
|
214
|
-
[:id, :class].each do |key|
|
215
|
-
if options.include?(key)
|
216
|
-
value = options[key]
|
217
|
-
html[key] = value unless value.blank?
|
218
|
-
else
|
219
|
-
html[key] = 'errorExplanation'
|
220
|
-
end
|
221
|
-
end
|
222
|
-
options[:object_name] ||= params.first
|
223
|
-
|
224
|
-
I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale|
|
225
|
-
header_message = if options.include?(:header_message)
|
226
|
-
options[:header_message]
|
227
|
-
else
|
228
|
-
locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
|
229
|
-
end
|
230
|
-
|
231
|
-
message = options.include?(:message) ? options[:message] : locale.t(:body)
|
232
|
-
|
233
|
-
error_messages = objects.sum do |object|
|
234
|
-
object.errors.full_messages.map do |msg|
|
235
|
-
content_tag(:li, msg)
|
236
|
-
end
|
237
|
-
end.join.html_safe
|
238
|
-
|
239
|
-
contents = ''
|
240
|
-
contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
|
241
|
-
contents << content_tag(:p, message) unless message.blank?
|
242
|
-
contents << content_tag(:ul, error_messages)
|
243
|
-
|
244
|
-
content_tag(:div, contents.html_safe, html)
|
26
|
+
module ActiveModelFormBuilder
|
27
|
+
%w(error_messages error_message_on).each do |method|
|
28
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
29
|
+
def #{method}(*args)
|
30
|
+
ActiveSupport::Deprecation.warn "f.#{method} was removed from Rails and is now available as a plugin. " <<
|
31
|
+
"Please install it with `rails plugin install git://github.com/rails/dynamic_form.git`.", caller
|
245
32
|
end
|
246
|
-
|
247
|
-
''
|
248
|
-
end
|
33
|
+
RUBY
|
249
34
|
end
|
250
|
-
|
251
|
-
private
|
252
|
-
def all_input_tags(record, record_name, options)
|
253
|
-
input_block = options[:input_block] || default_input_block
|
254
|
-
record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
|
255
|
-
end
|
256
|
-
|
257
|
-
def default_input_block
|
258
|
-
Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
|
259
|
-
end
|
260
35
|
end
|
261
36
|
|
262
|
-
module
|
37
|
+
module ActiveModelInstanceTag
|
263
38
|
def object
|
264
39
|
@active_model_object ||= begin
|
265
40
|
object = super
|
@@ -267,26 +42,6 @@ module ActionView
|
|
267
42
|
end
|
268
43
|
end
|
269
44
|
|
270
|
-
def to_tag(options = {})
|
271
|
-
case column_type
|
272
|
-
when :string
|
273
|
-
field_type = @method_name.include?("password") ? "password" : "text"
|
274
|
-
to_input_field_tag(field_type, options)
|
275
|
-
when :text
|
276
|
-
to_text_area_tag(options)
|
277
|
-
when :integer, :float, :decimal
|
278
|
-
to_input_field_tag("text", options)
|
279
|
-
when :date
|
280
|
-
to_date_select_tag(options)
|
281
|
-
when :datetime, :timestamp
|
282
|
-
to_datetime_select_tag(options)
|
283
|
-
when :time
|
284
|
-
to_time_select_tag(options)
|
285
|
-
when :boolean
|
286
|
-
to_boolean_select_tag(options)
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
45
|
%w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
|
291
46
|
module_eval "def #{meth}(*) error_wrapping(super) end"
|
292
47
|
end
|
@@ -302,14 +57,14 @@ module ActionView
|
|
302
57
|
def error_message
|
303
58
|
object.errors[@method_name]
|
304
59
|
end
|
60
|
+
end
|
305
61
|
|
306
|
-
|
307
|
-
|
308
|
-
end
|
62
|
+
class FormBuilder
|
63
|
+
include ActiveModelFormBuilder
|
309
64
|
end
|
310
65
|
|
311
66
|
class InstanceTag
|
312
|
-
include
|
67
|
+
include ActiveModelInstanceTag
|
313
68
|
end
|
314
69
|
end
|
315
70
|
end
|