radmin 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/.gitignore +7 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +4 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README +30 -0
  6. data/Rakefile +9 -0
  7. data/app/controllers/admin/accounts_controller.rb +16 -0
  8. data/app/controllers/admin/passwords_controller.rb +4 -0
  9. data/app/controllers/admin/sessions_controller.rb +13 -0
  10. data/app/controllers/admin/settings_controller.rb +40 -0
  11. data/app/controllers/admin/users_controller.rb +43 -0
  12. data/app/controllers/admin_controller.rb +15 -0
  13. data/app/helpers/admin_helper.rb +45 -0
  14. data/app/models/radmin/assignment.rb +8 -0
  15. data/app/models/radmin/role.rb +8 -0
  16. data/app/models/radmin/setting.rb +38 -0
  17. data/app/models/radmin/user.rb +18 -0
  18. data/app/views/admin/accounts/edit.html.haml +11 -0
  19. data/app/views/admin/passwords/edit.html.haml +11 -0
  20. data/app/views/admin/passwords/new.html.haml +8 -0
  21. data/app/views/admin/sessions/new.html.haml +9 -0
  22. data/app/views/admin/settings/_form.html.haml +12 -0
  23. data/app/views/admin/settings/edit.html.haml +3 -0
  24. data/app/views/admin/settings/index.html.haml +18 -0
  25. data/app/views/admin/settings/new.html.haml +3 -0
  26. data/app/views/admin/users/_form.html.haml +21 -0
  27. data/app/views/admin/users/edit.html.haml +3 -0
  28. data/app/views/admin/users/index.html.haml +18 -0
  29. data/app/views/admin/users/new.html.haml +3 -0
  30. data/app/views/layouts/admin.html.haml +40 -0
  31. data/config/routes.rb +10 -0
  32. data/lib/generators/radmin/install_generator.rb +31 -0
  33. data/lib/generators/radmin/templates/assets/images/admin/topnav_active.gif +0 -0
  34. data/lib/generators/radmin/templates/assets/images/admin/topnav_stretch.gif +0 -0
  35. data/lib/generators/radmin/templates/assets/javascripts/admin/application.js +1 -0
  36. data/lib/generators/radmin/templates/assets/javascripts/admin/jquery.js +16 -0
  37. data/lib/generators/radmin/templates/assets/javascripts/admin/jquery.rails.js +226 -0
  38. data/lib/generators/radmin/templates/assets/stylesheets/admin/reset.css +48 -0
  39. data/lib/generators/radmin/templates/assets/stylesheets/admin/style.css +422 -0
  40. data/lib/generators/radmin/templates/authorization_rules.rb +17 -0
  41. data/lib/generators/radmin/templates/migrations/01_radmin_create_users.rb +17 -0
  42. data/lib/generators/radmin/templates/migrations/02_radmin_create_roles.rb +13 -0
  43. data/lib/generators/radmin/templates/migrations/03_radmin_create_assignments.rb +15 -0
  44. data/lib/generators/radmin/templates/migrations/04_radmin_create_settings.rb +15 -0
  45. data/lib/radmin.rb +5 -0
  46. data/lib/radmin/admin_ui.rb +136 -0
  47. data/lib/radmin/engine.rb +8 -0
  48. data/lib/radmin/i18n.rb +14 -0
  49. data/lib/radmin/version.rb +3 -0
  50. data/lib/tasks/radmin.rake +13 -0
  51. data/radmin.gemspec +32 -0
  52. data/spec/dummy/Rakefile +7 -0
  53. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  54. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  55. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  56. data/spec/dummy/config.ru +4 -0
  57. data/spec/dummy/config/application.rb +45 -0
  58. data/spec/dummy/config/authorization_rules.rb +17 -0
  59. data/spec/dummy/config/boot.rb +10 -0
  60. data/spec/dummy/config/database.yml +22 -0
  61. data/spec/dummy/config/environment.rb +5 -0
  62. data/spec/dummy/config/environments/development.rb +28 -0
  63. data/spec/dummy/config/environments/production.rb +49 -0
  64. data/spec/dummy/config/environments/test.rb +35 -0
  65. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  66. data/spec/dummy/config/initializers/devise.rb +194 -0
  67. data/spec/dummy/config/initializers/inflections.rb +10 -0
  68. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  69. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  70. data/spec/dummy/config/initializers/session_store.rb +8 -0
  71. data/spec/dummy/config/locales/en.yml +5 -0
  72. data/spec/dummy/config/routes.rb +58 -0
  73. data/spec/dummy/db/migrate/20110524171909_radmin_create_users.rb +17 -0
  74. data/spec/dummy/db/migrate/20110524171910_radmin_create_roles.rb +13 -0
  75. data/spec/dummy/db/migrate/20110524171911_radmin_create_assignments.rb +15 -0
  76. data/spec/dummy/db/migrate/20110524171912_radmin_create_settings.rb +15 -0
  77. data/spec/dummy/db/schema.rb +59 -0
  78. data/spec/dummy/public/404.html +26 -0
  79. data/spec/dummy/public/422.html +26 -0
  80. data/spec/dummy/public/500.html +26 -0
  81. data/spec/dummy/public/favicon.ico +0 -0
  82. data/spec/dummy/public/images/admin/topnav_active.gif +0 -0
  83. data/spec/dummy/public/images/admin/topnav_stretch.gif +0 -0
  84. data/spec/dummy/public/javascripts/admin/application.js +1 -0
  85. data/spec/dummy/public/javascripts/admin/jquery.js +16 -0
  86. data/spec/dummy/public/javascripts/admin/jquery.rails.js +226 -0
  87. data/spec/dummy/public/javascripts/application.js +2 -0
  88. data/spec/dummy/public/javascripts/controls.js +965 -0
  89. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  90. data/spec/dummy/public/javascripts/effects.js +1123 -0
  91. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  92. data/spec/dummy/public/javascripts/rails.js +191 -0
  93. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  94. data/spec/dummy/public/stylesheets/admin/reset.css +48 -0
  95. data/spec/dummy/public/stylesheets/admin/style.css +422 -0
  96. data/spec/dummy/script/rails +6 -0
  97. data/spec/dummy/vendor/plugins/dynamic_form/MIT-LICENSE +20 -0
  98. data/spec/dummy/vendor/plugins/dynamic_form/README +13 -0
  99. data/spec/dummy/vendor/plugins/dynamic_form/Rakefile +10 -0
  100. data/spec/dummy/vendor/plugins/dynamic_form/dynamic_form.gemspec +12 -0
  101. data/spec/dummy/vendor/plugins/dynamic_form/init.rb +1 -0
  102. data/spec/dummy/vendor/plugins/dynamic_form/lib/action_view/helpers/dynamic_form.rb +300 -0
  103. data/spec/dummy/vendor/plugins/dynamic_form/lib/action_view/locale/en.yml +8 -0
  104. data/spec/dummy/vendor/plugins/dynamic_form/lib/dynamic_form.rb +5 -0
  105. data/spec/dummy/vendor/plugins/dynamic_form/test/dynamic_form_i18n_test.rb +42 -0
  106. data/spec/dummy/vendor/plugins/dynamic_form/test/dynamic_form_test.rb +370 -0
  107. data/spec/dummy/vendor/plugins/dynamic_form/test/test_helper.rb +9 -0
  108. data/spec/integration/navigation_spec.rb +9 -0
  109. data/spec/radmin_spec.rb +7 -0
  110. data/spec/spec_helper.rb +33 -0
  111. metadata +270 -0
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 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.
@@ -0,0 +1,13 @@
1
+ DynamicForm
2
+ ===========
3
+
4
+ DynamicForm holds a few helpers method to help you deal with your models, they are:
5
+
6
+ * input(record, method, options = {})
7
+ * form(record, options = {})
8
+ * error_message_on(object, method, options={})
9
+ * error_messages_for(record, options={})
10
+
11
+ It also adds f.error_messages and f.error_messages_on to your form builders.
12
+
13
+ Copyright (c) 2010 David Heinemeier Hansson, released under the MIT license
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ desc 'Default: run unit tests.'
4
+ task :default => :test
5
+
6
+ desc 'Test the active_model_helper plugin.'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'dynamic_form'
3
+ s.version = '1.0.0'
4
+ s.author = 'David Heinemeier Hansson'
5
+ s.email = 'david@loudthinking.com'
6
+ s.summary = 'Deprecated dynamic form helpers: input, form, error_messages_for, error_messages_on'
7
+
8
+ s.add_dependency('rails', '>= 3.0.0')
9
+
10
+ s.files = Dir['lib/**/*']
11
+ s.require_path = 'lib'
12
+ end
@@ -0,0 +1 @@
1
+ require 'dynamic_form'
@@ -0,0 +1,300 @@
1
+ require 'action_view/helpers'
2
+ require 'active_support/i18n'
3
+ require 'active_support/core_ext/enumerable'
4
+ require 'active_support/core_ext/object/blank'
5
+
6
+ module ActionView
7
+ module Helpers
8
+ # The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the +form+
9
+ # method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
10
+ # is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
11
+ # In that case, it's better to use the +input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
12
+ module DynamicForm
13
+ # Returns a default input tag for the type of object returned by the method. For example, if <tt>@post</tt>
14
+ # has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World":
15
+ #
16
+ # input("post", "title")
17
+ # # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
18
+ def input(record_name, method, options = {})
19
+ InstanceTag.new(record_name, method, self).to_tag(options)
20
+ end
21
+
22
+ # Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
23
+ # has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
24
+ #
25
+ # form("post")
26
+ #
27
+ # would yield a form like the following (modulus formatting):
28
+ #
29
+ # <form action='/posts/create' method='post'>
30
+ # <p>
31
+ # <label for="post_title">Title</label><br />
32
+ # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
33
+ # </p>
34
+ # <p>
35
+ # <label for="post_body">Body</label><br />
36
+ # <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
37
+ # </p>
38
+ # <input name="commit" type="submit" value="Create" />
39
+ # </form>
40
+ #
41
+ # It's possible to specialize the form builder by using a different action name and by supplying another
42
+ # block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
43
+ #
44
+ # form("entry",
45
+ # :action => "sign",
46
+ # :input_block => Proc.new { |record, column|
47
+ # "#{column.human_name}: #{input(record, column.name)}<br />"
48
+ # })
49
+ #
50
+ # would yield a form like the following (modulus formatting):
51
+ #
52
+ # <form action="/entries/sign" method="post">
53
+ # Message:
54
+ # <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
55
+ # <input name="commit" type="submit" value="Sign" />
56
+ # </form>
57
+ #
58
+ # It's also possible to add additional content to the form by giving it a block, such as:
59
+ #
60
+ # form("entry", :action => "sign") do |form|
61
+ # form << content_tag("b", "Department")
62
+ # form << collection_select("department", "id", @departments, "id", "name")
63
+ # end
64
+ #
65
+ # The following options are available:
66
+ #
67
+ # * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
68
+ # * <tt>:input_block</tt> - Specialize the output using a different block, see above.
69
+ # * <tt>:method</tt> - The method used when submitting the form (default: +post+).
70
+ # * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
71
+ # * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
72
+ def form(record_name, options = {})
73
+ record = instance_variable_get("@#{record_name}")
74
+ record = convert_to_model(record)
75
+
76
+ options = options.symbolize_keys
77
+ options[:action] ||= record.persisted? ? "update" : "create"
78
+ action = url_for(:action => options[:action], :id => record)
79
+
80
+ submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
81
+
82
+ contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
83
+ contents.safe_concat hidden_field(record_name, :id) if record.persisted?
84
+ contents.safe_concat all_input_tags(record, record_name, options)
85
+ yield contents if block_given?
86
+ contents.safe_concat submit_tag(submit_value)
87
+ contents.safe_concat('</form>')
88
+ end
89
+
90
+ # Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
91
+ # This error message is wrapped in a <tt>DIV</tt> tag by default or with <tt>:html_tag</tt> if specified,
92
+ # which can be extended to include a <tt>:prepend_text</tt> and/or <tt>:append_text</tt> (to properly explain
93
+ # the error), and a <tt>:css_class</tt> to style it accordingly. +object+ should either be the name of an
94
+ # instance variable or the actual object. The method can be passed in either as a string or a symbol.
95
+ # As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
96
+ #
97
+ # <%= error_message_on "post", "title" %>
98
+ # # => <div class="formError">can't be empty</div>
99
+ #
100
+ # <%= error_message_on @post, :title %>
101
+ # # => <div class="formError">can't be empty</div>
102
+ #
103
+ # <%= error_message_on "post", "title",
104
+ # :prepend_text => "Title simply ",
105
+ # :append_text => " (or it won't work).",
106
+ # :html_tag => "span",
107
+ # :css_class => "inputError" %>
108
+ # # => <span class="inputError">Title simply can't be empty (or it won't work).</span>
109
+ def error_message_on(object, method, *args)
110
+ options = args.extract_options!
111
+ unless args.empty?
112
+ ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
113
+ 'prepend_text, append_text, html_tag, and css_class arguments', caller)
114
+
115
+ options[:prepend_text] = args[0] || ''
116
+ options[:append_text] = args[1] || ''
117
+ options[:html_tag] = args[2] || 'div'
118
+ options[:css_class] = args[3] || 'formError'
119
+ end
120
+ options.reverse_merge!(:prepend_text => '', :append_text => '', :html_tag => 'div', :css_class => 'formError')
121
+
122
+ object = convert_to_model(object)
123
+
124
+ if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
125
+ (errors = obj.errors[method]).presence
126
+ content_tag(options[:html_tag],
127
+ (options[:prepend_text].html_safe << errors.first).safe_concat(options[:append_text]),
128
+ :class => options[:css_class]
129
+ )
130
+ else
131
+ ''
132
+ end
133
+ end
134
+
135
+ # Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
136
+ # given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
137
+ # provided.
138
+ #
139
+ # This <tt>DIV</tt> can be tailored by the following options:
140
+ #
141
+ # * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
142
+ # * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
143
+ # * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
144
+ # * <tt>:object</tt> - The object (or array of objects) for which to display errors,
145
+ # if you need to escape the instance variable convention.
146
+ # * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
147
+ # If <tt>:object_name</tt> is not set, the name of the first object will be used.
148
+ # * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
149
+ # or an empty string to avoid the header message altogether. (Default: "X errors
150
+ # prohibited this object from being saved").
151
+ # * <tt>:message</tt> - The explanation message after the header message and before
152
+ # the error list. Pass +nil+ or an empty string to avoid the explanation message
153
+ # altogether. (Default: "There were problems with the following fields:").
154
+ #
155
+ # To specify the display for one object, you simply provide its name as a parameter.
156
+ # For example, for the <tt>@user</tt> model:
157
+ #
158
+ # error_messages_for 'user'
159
+ #
160
+ # You can also supply an object:
161
+ #
162
+ # error_messages_for @user
163
+ #
164
+ # This will use the last part of the model name in the presentation. For instance, if
165
+ # this is a MyKlass::User object, this will use "user" as the name in the String. This
166
+ # is taken from MyKlass::User.model_name.human, which can be overridden.
167
+ #
168
+ # To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
169
+ # will be the name used in the header message:
170
+ #
171
+ # error_messages_for 'user_common', 'user', :object_name => 'user'
172
+ #
173
+ # You can also use a number of objects, which will have the same naming semantics
174
+ # as a single object.
175
+ #
176
+ # error_messages_for @user, @post
177
+ #
178
+ # If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> parameter which gives the actual
179
+ # object (or array of objects to use):
180
+ #
181
+ # error_messages_for 'user', :object => @question.user
182
+ #
183
+ # NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
184
+ # you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
185
+ # instance yourself and set it up. View the source of this method to see how easy it is.
186
+ def error_messages_for(*params)
187
+ options = params.extract_options!.symbolize_keys
188
+
189
+ objects = Array.wrap(options.delete(:object) || params).map do |object|
190
+ object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
191
+ object = convert_to_model(object)
192
+
193
+ if object.class.respond_to?(:model_name)
194
+ options[:object_name] ||= object.class.model_name.human.downcase
195
+ end
196
+
197
+ object
198
+ end
199
+
200
+ objects.compact!
201
+ count = objects.inject(0) {|sum, object| sum + object.errors.count }
202
+
203
+ unless count.zero?
204
+ html = {}
205
+ [:id, :class].each do |key|
206
+ if options.include?(key)
207
+ value = options[key]
208
+ html[key] = value unless value.blank?
209
+ else
210
+ html[key] = 'errorExplanation'
211
+ end
212
+ end
213
+ options[:object_name] ||= params.first
214
+
215
+ I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale|
216
+ header_message = if options.include?(:header_message)
217
+ options[:header_message]
218
+ else
219
+ locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
220
+ end
221
+
222
+ message = options.include?(:message) ? options[:message] : locale.t(:body)
223
+
224
+ error_messages = objects.sum do |object|
225
+ object.errors.full_messages.map do |msg|
226
+ content_tag(:li, msg)
227
+ end
228
+ end.join.html_safe
229
+
230
+ contents = ''
231
+ contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
232
+ contents << content_tag(:p, message) unless message.blank?
233
+ contents << content_tag(:ul, error_messages)
234
+
235
+ content_tag(:div, contents.html_safe, html)
236
+ end
237
+ else
238
+ ''
239
+ end
240
+ end
241
+
242
+ private
243
+
244
+ def all_input_tags(record, record_name, options)
245
+ input_block = options[:input_block] || default_input_block
246
+ record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
247
+ end
248
+
249
+ def default_input_block
250
+ Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
251
+ end
252
+
253
+ module InstanceTagMethods
254
+ def to_tag(options = {})
255
+ case column_type
256
+ when :string
257
+ field_type = @method_name.include?("password") ? "password" : "text"
258
+ to_input_field_tag(field_type, options)
259
+ when :text
260
+ to_text_area_tag(options)
261
+ when :integer, :float, :decimal
262
+ to_input_field_tag("text", options)
263
+ when :date
264
+ to_date_select_tag(options)
265
+ when :datetime, :timestamp
266
+ to_datetime_select_tag(options)
267
+ when :time
268
+ to_time_select_tag(options)
269
+ when :boolean
270
+ to_boolean_select_tag(options)
271
+ end
272
+ end
273
+
274
+ def column_type
275
+ object.send(:column_for_attribute, @method_name).type
276
+ end
277
+ end
278
+
279
+ module FormBuilderMethods
280
+ def error_message_on(method, *args)
281
+ @template.error_message_on(@object || @object_name, method, *args)
282
+ end
283
+
284
+ def error_messages(options = {})
285
+ @template.error_messages_for(@object_name, objectify_options(options))
286
+ end
287
+ end
288
+ end
289
+
290
+ class InstanceTag
291
+ include DynamicForm::InstanceTagMethods
292
+ end
293
+
294
+ class FormBuilder
295
+ include DynamicForm::FormBuilderMethods
296
+ end
297
+ end
298
+ end
299
+
300
+ I18n.load_path << File.expand_path("../../locale/en.yml", __FILE__)
@@ -0,0 +1,8 @@
1
+ en:
2
+ errors:
3
+ template:
4
+ header:
5
+ one: "1 error prohibited this %{model} from being saved"
6
+ other: "%{count} errors prohibited this %{model} from being saved"
7
+ # The variable :count is also available
8
+ body: "There were problems with the following fields:"
@@ -0,0 +1,5 @@
1
+ require 'action_view/helpers/dynamic_form'
2
+
3
+ class ActionView::Base
4
+ include DynamicForm
5
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class DynamicFormI18nTest < Test::Unit::TestCase
4
+ include ActionView::Context
5
+ include ActionView::Helpers::DynamicForm
6
+
7
+ attr_reader :request
8
+
9
+ def setup
10
+ @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
11
+ @object.stubs :to_model => @object
12
+ @object.stubs :class => stub(:model_name => stub(:human => ""))
13
+
14
+ @object_name = 'book_seller'
15
+ @object_name_without_underscore = 'book seller'
16
+
17
+ stubs(:content_tag).returns 'content_tag'
18
+
19
+ I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
20
+ I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
21
+ end
22
+
23
+ def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
24
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').never
25
+ error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
26
+ end
27
+
28
+ def test_error_messages_for_given_no_header_option_it_translates_header_message
29
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns 'header message'
30
+ error_messages_for(:object => @object, :locale => 'en')
31
+ end
32
+
33
+ def test_error_messages_for_given_a_message_option_it_does_not_translate_message
34
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).never
35
+ error_messages_for(:object => @object, :message => 'message', :locale => 'en')
36
+ end
37
+
38
+ def test_error_messages_for_given_no_message_option_it_translates_message
39
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
40
+ error_messages_for(:object => @object, :locale => 'en')
41
+ end
42
+ end
@@ -0,0 +1,370 @@
1
+ require 'test_helper'
2
+ require 'action_view/template/handlers/erb'
3
+
4
+ class DynamicFormTest < ActionView::TestCase
5
+ tests ActionView::Helpers::DynamicForm
6
+
7
+ def form_for(*)
8
+ @output_buffer = super
9
+ end
10
+
11
+ silence_warnings do
12
+ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
13
+ extend ActiveModel::Naming
14
+ include ActiveModel::Conversion
15
+ end
16
+
17
+ class User < Struct.new(:email)
18
+ extend ActiveModel::Naming
19
+ include ActiveModel::Conversion
20
+ end
21
+
22
+ class Column < Struct.new(:type, :name, :human_name)
23
+ extend ActiveModel::Naming
24
+ include ActiveModel::Conversion
25
+ end
26
+ end
27
+
28
+ class DirtyPost
29
+ class Errors
30
+ def empty?
31
+ false
32
+ end
33
+
34
+ def count
35
+ 1
36
+ end
37
+
38
+ def full_messages
39
+ ["Author name can't be <em>empty</em>"]
40
+ end
41
+
42
+ def [](field)
43
+ ["can't be <em>empty</em>"]
44
+ end
45
+ end
46
+
47
+ def errors
48
+ Errors.new
49
+ end
50
+ end
51
+
52
+ def setup_post
53
+ @post = Post.new
54
+ def @post.errors
55
+ Class.new {
56
+ def [](field)
57
+ case field.to_s
58
+ when "author_name"
59
+ ["can't be empty"]
60
+ when "body"
61
+ ['foo']
62
+ else
63
+ []
64
+ end
65
+ end
66
+ def empty?() false end
67
+ def count() 1 end
68
+ def full_messages() [ "Author name can't be empty" ] end
69
+ }.new
70
+ end
71
+
72
+ def @post.persisted?() false end
73
+ def @post.to_param() nil end
74
+
75
+ def @post.column_for_attribute(attr_name)
76
+ Post.content_columns.select { |column| column.name == attr_name }.first
77
+ end
78
+
79
+ silence_warnings do
80
+ def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end
81
+ end
82
+
83
+ @post.title = "Hello World"
84
+ @post.author_name = ""
85
+ @post.body = "Back to the hill and over it again!"
86
+ @post.secret = 1
87
+ @post.written_on = Date.new(2004, 6, 15)
88
+ end
89
+
90
+ def setup_user
91
+ @user = User.new
92
+ def @user.errors
93
+ Class.new {
94
+ def [](field) field == "email" ? ['nonempty'] : [] end
95
+ def empty?() false end
96
+ def count() 1 end
97
+ def full_messages() [ "User email can't be empty" ] end
98
+ }.new
99
+ end
100
+
101
+ def @user.new_record?() true end
102
+ def @user.to_param() nil end
103
+
104
+ def @user.column_for_attribute(attr_name)
105
+ User.content_columns.select { |column| column.name == attr_name }.first
106
+ end
107
+
108
+ silence_warnings do
109
+ def User.content_columns() [ Column.new(:string, "email", "Email") ] end
110
+ end
111
+
112
+ @user.email = ""
113
+ end
114
+
115
+ def protect_against_forgery?
116
+ @protect_against_forgery ? true : false
117
+ end
118
+ attr_accessor :request_forgery_protection_token, :form_authenticity_token
119
+
120
+ def setup
121
+ super
122
+ setup_post
123
+ setup_user
124
+
125
+ @response = ActionController::TestResponse.new
126
+ end
127
+
128
+ def url_for(options)
129
+ options = options.symbolize_keys
130
+ [options[:action], options[:id].to_param].compact.join('/')
131
+ end
132
+
133
+ def test_generic_input_tag
134
+ assert_dom_equal(
135
+ %(<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />), input("post", "title")
136
+ )
137
+ end
138
+
139
+ def test_text_area_with_errors
140
+ assert_dom_equal(
141
+ %(<div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>),
142
+ text_area("post", "body")
143
+ )
144
+ end
145
+
146
+ def test_text_field_with_errors
147
+ assert_dom_equal(
148
+ %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>),
149
+ text_field("post", "author_name")
150
+ )
151
+ end
152
+
153
+ def test_field_error_proc
154
+ old_proc = ActionView::Base.field_error_proc
155
+ ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
156
+ %(<div class=\"fieldWithErrors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
157
+ end
158
+
159
+ assert_dom_equal(
160
+ %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>),
161
+ text_field("post", "author_name")
162
+ )
163
+ ensure
164
+ ActionView::Base.field_error_proc = old_proc if old_proc
165
+ end
166
+
167
+ def test_form_with_string
168
+ assert_dom_equal(
169
+ %(<form action="create" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
170
+ form("post")
171
+ )
172
+
173
+ silence_warnings do
174
+ class << @post
175
+ def persisted?() true end
176
+ def to_param() id end
177
+ def id() 1 end
178
+ end
179
+ end
180
+
181
+ assert_dom_equal(
182
+ %(<form action="update/1" method="post"><input id="post_id" name="post[id]" type="hidden" value="1" /><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Update" /></form>),
183
+ form("post")
184
+ )
185
+ end
186
+
187
+ def test_form_with_protect_against_forgery
188
+ @protect_against_forgery = true
189
+ @request_forgery_protection_token = 'authenticity_token'
190
+ @form_authenticity_token = '123'
191
+ assert_dom_equal(
192
+ %(<form action="create" method="post"><div style='margin:0;padding:0;display:inline'><input type='hidden' name='authenticity_token' value='123' /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
193
+ form("post")
194
+ )
195
+ end
196
+
197
+ def test_form_with_method_option
198
+ assert_dom_equal(
199
+ %(<form action="create" method="get"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
200
+ form("post", :method=>'get')
201
+ )
202
+ end
203
+
204
+ def test_form_with_action_option
205
+ output_buffer << form("post", :action => "sign")
206
+ assert_select "form[action=sign]" do |form|
207
+ assert_select "input[type=submit][value=Sign]"
208
+ end
209
+ end
210
+
211
+ def test_form_with_date
212
+ silence_warnings do
213
+ def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
214
+ end
215
+
216
+ assert_dom_equal(
217
+ %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
218
+ form("post")
219
+ )
220
+ end
221
+
222
+ def test_form_with_datetime
223
+ silence_warnings do
224
+ def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
225
+ end
226
+ @post.written_on = Time.gm(2004, 6, 15, 16, 30)
227
+
228
+ assert_dom_equal(
229
+ %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n &mdash; <select id="post_written_on_4i" name="post[written_on(4i)]">\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_written_on_5i" name="post[written_on(5i)]">\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30" selected="selected">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
230
+ form("post")
231
+ )
232
+ end
233
+
234
+ def test_error_for_block
235
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post")
236
+ assert_equal %(<div class="errorDeathByClass" id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
237
+ assert_equal %(<div id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1")
238
+ assert_equal %(<div class="errorDeathByClass"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
239
+ end
240
+
241
+ def test_error_messages_for_escapes_html
242
+ @dirty_post = DirtyPost.new
243
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this dirty post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be &lt;em&gt;empty&lt;/em&gt;</li></ul></div>), error_messages_for("dirty_post")
244
+ end
245
+
246
+ def test_error_messages_for_handles_nil
247
+ assert_equal "", error_messages_for("notthere")
248
+ end
249
+
250
+ def test_error_message_on_escapes_html
251
+ @dirty_post = DirtyPost.new
252
+ assert_dom_equal "<div class=\"formError\">can't be &lt;em&gt;empty&lt;/em&gt;</div>", error_message_on(:dirty_post, :author_name)
253
+ end
254
+
255
+ def test_error_message_on_handles_nil
256
+ assert_equal "", error_message_on("notthere", "notthere")
257
+ end
258
+
259
+ def test_error_message_on
260
+ assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(:post, :author_name)
261
+ end
262
+
263
+ def test_error_message_on_no_instance_variable
264
+ other_post = @post
265
+ assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(other_post, :author_name)
266
+ end
267
+
268
+ def test_error_message_on_with_options_hash
269
+ assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
270
+ end
271
+
272
+ def test_error_message_on_with_tag_option_in_options_hash
273
+ assert_dom_equal "<span class=\"differentError\">beforecan't be emptyafter</span>", error_message_on(:post, :author_name, :html_tag => "span", :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
274
+ end
275
+
276
+ def test_error_message_on_handles_empty_errors
277
+ assert_equal "", error_message_on(@post, :tag)
278
+ end
279
+
280
+ def test_error_messages_for_many_objects
281
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user")
282
+
283
+ # reverse the order, error order changes and so does the title
284
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post")
285
+
286
+ # add the default to put post back in the title
287
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object_name => "post")
288
+
289
+ # symbols work as well
290
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => :post)
291
+
292
+ # any default works too
293
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this monkey from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "monkey")
294
+
295
+ # should space object name
296
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this chunky bacon from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "chunky_bacon")
297
+
298
+ # hide header and explanation messages with nil or empty string
299
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => nil, :message => "")
300
+
301
+ # override header and explanation messages
302
+ header_message = "Yikes! Some errors"
303
+ message = "Please fix the following fields and resubmit:"
304
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => header_message, :message => message)
305
+ end
306
+
307
+ def test_error_messages_for_non_instance_variable
308
+ actual_user = @user
309
+ actual_post = @post
310
+ @user = nil
311
+ @post = nil
312
+
313
+ #explicitly set object
314
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :object => actual_post)
315
+
316
+ #multiple objects
317
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object => [actual_user, actual_post])
318
+
319
+ #nil object
320
+ assert_equal '', error_messages_for('user', :object => nil)
321
+ end
322
+
323
+ def test_error_messages_for_model_objects
324
+ error = error_messages_for(@post)
325
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>),
326
+ error
327
+
328
+ error = error_messages_for(@user, @post)
329
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>),
330
+ error
331
+ end
332
+
333
+ def test_form_with_string_multipart
334
+ assert_dom_equal(
335
+ %(<form action="create" enctype="multipart/form-data" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
336
+ form("post", :multipart => true)
337
+ )
338
+ end
339
+
340
+ def test_default_form_builder_with_dynamic_form_helpers
341
+ form_for(@post, :as => :post, :url => {}) do |f|
342
+ concat f.error_message_on('author_name')
343
+ concat f.error_messages
344
+ end
345
+
346
+ expected = %(<form class="post_new" method="post" action="" id="post_new">) +
347
+ %(<div class="formError">can't be empty</div>) +
348
+ %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
349
+ %(</form>)
350
+
351
+ assert_dom_equal expected, output_buffer
352
+ end
353
+
354
+ def test_default_form_builder_no_instance_variable
355
+ post = @post
356
+ @post = nil
357
+
358
+ form_for(post, :as => :post, :url => {}) do |f|
359
+ concat f.error_message_on('author_name')
360
+ concat f.error_messages
361
+ end
362
+
363
+ expected = %(<form class="post_new" method="post" action="" id="post_new">) +
364
+ %(<div class="formError">can't be empty</div>) +
365
+ %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
366
+ %(</form>)
367
+
368
+ assert_dom_equal expected, output_buffer
369
+ end
370
+ end