clearance 0.9.0.rc2 → 0.9.0.rc3

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

Potentially problematic release.


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

Files changed (96) hide show
  1. data/Rakefile +55 -1
  2. data/VERSION +1 -0
  3. data/clearance.gemspec +208 -0
  4. data/cucumber.yml +4 -0
  5. data/lib/rails/generators/clearance_generator.rb +5 -1
  6. data/spec/rails_root/Gemfile +40 -0
  7. data/spec/rails_root/README +244 -0
  8. data/spec/rails_root/Rakefile +10 -0
  9. data/spec/rails_root/app/controllers/accounts_controller.rb +10 -0
  10. data/spec/rails_root/app/controllers/application_controller.rb +6 -0
  11. data/spec/rails_root/app/helpers/application_helper.rb +2 -0
  12. data/spec/rails_root/app/models/user.rb +3 -0
  13. data/spec/rails_root/app/views/accounts/edit.html.erb +0 -0
  14. data/spec/rails_root/app/views/layouts/application.html.erb +24 -0
  15. data/spec/rails_root/config.ru +4 -0
  16. data/spec/rails_root/config/application.rb +46 -0
  17. data/spec/rails_root/config/boot.rb +6 -0
  18. data/spec/rails_root/config/cucumber.yml +8 -0
  19. data/spec/rails_root/config/database.yml +25 -0
  20. data/spec/rails_root/config/environment.rb +7 -0
  21. data/spec/rails_root/config/environments/development.rb +19 -0
  22. data/spec/rails_root/config/environments/production.rb +42 -0
  23. data/spec/rails_root/config/environments/test.rb +32 -0
  24. data/spec/rails_root/config/initializers/backtrace_silencers.rb +7 -0
  25. data/spec/rails_root/config/initializers/clearance.rb +3 -0
  26. data/spec/rails_root/config/initializers/inflections.rb +10 -0
  27. data/spec/rails_root/config/initializers/mime_types.rb +5 -0
  28. data/spec/rails_root/config/initializers/secret_token.rb +7 -0
  29. data/spec/rails_root/config/initializers/session_store.rb +8 -0
  30. data/spec/rails_root/config/locales/en.yml +5 -0
  31. data/spec/rails_root/config/routes.rb +61 -0
  32. data/spec/rails_root/db/schema.rb +28 -0
  33. data/spec/rails_root/doc/README_FOR_APP +2 -0
  34. data/spec/rails_root/features/password_reset.feature +33 -0
  35. data/spec/rails_root/features/sign_in.feature +35 -0
  36. data/spec/rails_root/features/sign_out.feature +15 -0
  37. data/spec/rails_root/features/sign_up.feature +45 -0
  38. data/spec/rails_root/features/step_definitions/clearance_steps.rb +130 -0
  39. data/spec/rails_root/features/step_definitions/web_steps.rb +219 -0
  40. data/spec/rails_root/features/support/env.rb +57 -0
  41. data/spec/rails_root/features/support/paths.rb +45 -0
  42. data/spec/rails_root/lib/tasks/cucumber.rake +53 -0
  43. data/spec/rails_root/public/404.html +26 -0
  44. data/spec/rails_root/public/422.html +26 -0
  45. data/spec/rails_root/public/500.html +26 -0
  46. data/spec/rails_root/public/favicon.ico +0 -0
  47. data/spec/rails_root/public/images/rails.png +0 -0
  48. data/spec/rails_root/public/javascripts/application.js +2 -0
  49. data/spec/rails_root/public/javascripts/controls.js +965 -0
  50. data/spec/rails_root/public/javascripts/dragdrop.js +974 -0
  51. data/spec/rails_root/public/javascripts/effects.js +1123 -0
  52. data/spec/rails_root/public/javascripts/prototype.js +4874 -0
  53. data/spec/rails_root/public/javascripts/rails.js +118 -0
  54. data/spec/rails_root/public/robots.txt +5 -0
  55. data/spec/rails_root/script/cucumber +10 -0
  56. data/spec/rails_root/script/rails +9 -0
  57. data/spec/rails_root/spec/factories/clearance.rb +13 -0
  58. data/spec/rails_root/vendor/plugins/dynamic_form/MIT-LICENSE +20 -0
  59. data/spec/rails_root/vendor/plugins/dynamic_form/README +13 -0
  60. data/spec/rails_root/vendor/plugins/dynamic_form/Rakefile +10 -0
  61. data/spec/rails_root/vendor/plugins/dynamic_form/init.rb +5 -0
  62. data/spec/rails_root/vendor/plugins/dynamic_form/lib/action_view/helpers/dynamic_form.rb +300 -0
  63. data/spec/rails_root/vendor/plugins/dynamic_form/lib/action_view/locale/en.yml +8 -0
  64. data/spec/rails_root/vendor/plugins/dynamic_form/test/dynamic_form_i18n_test.rb +42 -0
  65. data/spec/rails_root/vendor/plugins/dynamic_form/test/dynamic_form_test.rb +370 -0
  66. data/spec/rails_root/vendor/plugins/dynamic_form/test/test_helper.rb +9 -0
  67. data/test/rails_root/Gemfile +40 -0
  68. data/test/rails_root/README +244 -0
  69. data/test/rails_root/Rakefile +10 -0
  70. data/test/rails_root/app/views/accounts/edit.html.erb +0 -0
  71. data/test/rails_root/app/views/layouts/application.html.erb +24 -0
  72. data/test/rails_root/config.ru +4 -0
  73. data/test/rails_root/config/cucumber.yml +8 -0
  74. data/test/rails_root/config/database.yml +25 -0
  75. data/test/rails_root/config/locales/en.yml +5 -0
  76. data/test/rails_root/doc/README_FOR_APP +2 -0
  77. data/test/rails_root/lib/tasks/cucumber.rake +53 -0
  78. data/test/rails_root/public/404.html +26 -0
  79. data/test/rails_root/public/422.html +26 -0
  80. data/test/rails_root/public/500.html +26 -0
  81. data/test/rails_root/public/favicon.ico +0 -0
  82. data/test/rails_root/public/images/rails.png +0 -0
  83. data/test/rails_root/public/javascripts/application.js +2 -0
  84. data/test/rails_root/public/javascripts/controls.js +965 -0
  85. data/test/rails_root/public/javascripts/dragdrop.js +974 -0
  86. data/test/rails_root/public/javascripts/effects.js +1123 -0
  87. data/test/rails_root/public/javascripts/prototype.js +4874 -0
  88. data/test/rails_root/public/javascripts/rails.js +118 -0
  89. data/test/rails_root/public/robots.txt +5 -0
  90. data/test/rails_root/script/cucumber +10 -0
  91. data/test/rails_root/script/rails +9 -0
  92. data/test/rails_root/vendor/plugins/dynamic_form/MIT-LICENSE +20 -0
  93. data/test/rails_root/vendor/plugins/dynamic_form/README +13 -0
  94. data/test/rails_root/vendor/plugins/dynamic_form/Rakefile +10 -0
  95. data/test/rails_root/vendor/plugins/dynamic_form/lib/action_view/locale/en.yml +8 -0
  96. metadata +111 -47
@@ -0,0 +1,118 @@
1
+ document.observe("dom:loaded", function() {
2
+ function handleRemote(element) {
3
+ var method, url, params;
4
+
5
+ if (element.tagName.toLowerCase() === 'form') {
6
+ method = element.readAttribute('method') || 'post';
7
+ url = element.readAttribute('action');
8
+ params = element.serialize(true);
9
+ } else {
10
+ method = element.readAttribute('data-method') || 'get';
11
+ url = element.readAttribute('href');
12
+ params = {};
13
+ }
14
+
15
+ var event = element.fire("ajax:before");
16
+ if (event.stopped) return false;
17
+
18
+ new Ajax.Request(url, {
19
+ method: method,
20
+ parameters: params,
21
+ asynchronous: true,
22
+ evalScripts: true,
23
+
24
+ onLoading: function(request) { element.fire("ajax:loading", {request: request}); },
25
+ onLoaded: function(request) { element.fire("ajax:loaded", {request: request}); },
26
+ onInteractive: function(request) { element.fire("ajax:interactive", {request: request}); },
27
+ onComplete: function(request) { element.fire("ajax:complete", {request: request}); },
28
+ onSuccess: function(request) { element.fire("ajax:success", {request: request}); },
29
+ onFailure: function(request) { element.fire("ajax:failure", {request: request}); }
30
+ });
31
+
32
+ element.fire("ajax:after");
33
+ }
34
+
35
+ function handleMethod(element) {
36
+ var method, url, token_name, token;
37
+
38
+ method = element.readAttribute('data-method');
39
+ url = element.readAttribute('href');
40
+ csrf_param = $$('meta[name=csrf-param]').first();
41
+ csrf_token = $$('meta[name=csrf-token]').first();
42
+
43
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
44
+ element.parentNode.appendChild(form);
45
+
46
+ if (method != 'post') {
47
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
48
+ form.appendChild(field);
49
+ }
50
+
51
+ if (csrf_param) {
52
+ var param = csrf_param.readAttribute('content');
53
+ var token = csrf_token.readAttribute('content');
54
+ var field = new Element('input', { type: 'hidden', name: param, value: token });
55
+ form.appendChild(field);
56
+ }
57
+
58
+ form.submit();
59
+ }
60
+
61
+ $(document.body).observe("click", function(event) {
62
+ var message = event.findElement().readAttribute('data-confirm');
63
+ if (message && !confirm(message)) {
64
+ event.stop();
65
+ return false;
66
+ }
67
+
68
+ var element = event.findElement("a[data-remote]");
69
+ if (element) {
70
+ handleRemote(element);
71
+ event.stop();
72
+ return true;
73
+ }
74
+
75
+ var element = event.findElement("a[data-method]");
76
+ if (element) {
77
+ handleMethod(element);
78
+ event.stop();
79
+ return true;
80
+ }
81
+ });
82
+
83
+ // TODO: I don't think submit bubbles in IE
84
+ $(document.body).observe("submit", function(event) {
85
+ var element = event.findElement(),
86
+ message = element.readAttribute('data-confirm');
87
+ if (message && !confirm(message)) {
88
+ event.stop();
89
+ return false;
90
+ }
91
+
92
+ var inputs = element.select("input[type=submit][data-disable-with]");
93
+ inputs.each(function(input) {
94
+ input.disabled = true;
95
+ input.writeAttribute('data-original-value', input.value);
96
+ input.value = input.readAttribute('data-disable-with');
97
+ });
98
+
99
+ var element = event.findElement("form[data-remote]");
100
+ if (element) {
101
+ handleRemote(element);
102
+ event.stop();
103
+ }
104
+ });
105
+
106
+ $(document.body).observe("ajax:after", function(event) {
107
+ var element = event.findElement();
108
+
109
+ if (element.tagName.toLowerCase() === 'form') {
110
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
111
+ inputs.each(function(input) {
112
+ input.value = input.readAttribute('data-original-value');
113
+ input.writeAttribute('data-original-value', null);
114
+ input.disabled = false;
115
+ });
116
+ }
117
+ });
118
+ });
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
4
+ if vendored_cucumber_bin
5
+ load File.expand_path(vendored_cucumber_bin)
6
+ else
7
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
8
+ require 'cucumber'
9
+ load Cucumber::BINARY
10
+ end
@@ -0,0 +1,9 @@
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
+ ENV_PATH = File.expand_path('../../config/environment', __FILE__)
5
+ BOOT_PATH = File.expand_path('../../config/boot', __FILE__)
6
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
7
+
8
+ require BOOT_PATH
9
+ require 'rails/commands'
@@ -0,0 +1,13 @@
1
+ Factory.sequence :email do |n|
2
+ "user#{n}@example.com"
3
+ end
4
+
5
+ Factory.define :user do |user|
6
+ user.email { Factory.next :email }
7
+ user.password { "password" }
8
+ user.password_confirmation { "password" }
9
+ end
10
+
11
+ Factory.define :email_confirmed_user, :parent => :user do |user|
12
+ user.email_confirmed { true }
13
+ end
@@ -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,5 @@
1
+ require 'action_view/helpers/dynamic_form'
2
+
3
+ class ActionView::Base
4
+ include DynamicForm
5
+ end
@@ -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__)