dynamic_form 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +74 -0
- data/MIT-LICENSE +20 -0
- data/README +13 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/dynamic-form.gemspec +58 -0
- data/dynamic_form.gemspec +60 -0
- data/init.rb +1 -0
- data/lib/action_view/helpers/dynamic_form.rb +300 -0
- data/lib/action_view/locale/en.yml +8 -0
- data/lib/dynamic_form.rb +5 -0
- data/test/dynamic_form_i18n_test.rb +42 -0
- data/test/dynamic_form_test.rb +372 -0
- data/test/test_helper.rb +9 -0
- metadata +93 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.bundle
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.0.rc)
|
6
|
+
actionpack (= 3.0.0.rc)
|
7
|
+
mail (~> 2.2.5)
|
8
|
+
actionpack (3.0.0.rc)
|
9
|
+
activemodel (= 3.0.0.rc)
|
10
|
+
activesupport (= 3.0.0.rc)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.4.1)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.9)
|
16
|
+
rack-test (~> 0.5.4)
|
17
|
+
tzinfo (~> 0.3.22)
|
18
|
+
activemodel (3.0.0.rc)
|
19
|
+
activesupport (= 3.0.0.rc)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.4.1)
|
22
|
+
activerecord (3.0.0.rc)
|
23
|
+
activemodel (= 3.0.0.rc)
|
24
|
+
activesupport (= 3.0.0.rc)
|
25
|
+
arel (~> 0.4.0)
|
26
|
+
tzinfo (~> 0.3.22)
|
27
|
+
activeresource (3.0.0.rc)
|
28
|
+
activemodel (= 3.0.0.rc)
|
29
|
+
activesupport (= 3.0.0.rc)
|
30
|
+
activesupport (3.0.0.rc)
|
31
|
+
arel (0.4.0)
|
32
|
+
activesupport (>= 3.0.0.beta)
|
33
|
+
builder (2.1.2)
|
34
|
+
erubis (2.6.6)
|
35
|
+
abstract (>= 1.0.0)
|
36
|
+
i18n (0.4.1)
|
37
|
+
mail (2.2.5)
|
38
|
+
activesupport (>= 2.3.6)
|
39
|
+
mime-types
|
40
|
+
treetop (>= 1.4.5)
|
41
|
+
mime-types (1.16)
|
42
|
+
mocha (0.9.8)
|
43
|
+
rake
|
44
|
+
polyglot (0.3.1)
|
45
|
+
rack (1.2.1)
|
46
|
+
rack-mount (0.6.9)
|
47
|
+
rack (>= 1.0.0)
|
48
|
+
rack-test (0.5.4)
|
49
|
+
rack (>= 1.0)
|
50
|
+
rails (3.0.0.rc)
|
51
|
+
actionmailer (= 3.0.0.rc)
|
52
|
+
actionpack (= 3.0.0.rc)
|
53
|
+
activerecord (= 3.0.0.rc)
|
54
|
+
activeresource (= 3.0.0.rc)
|
55
|
+
activesupport (= 3.0.0.rc)
|
56
|
+
bundler (>= 1.0.0.rc.1)
|
57
|
+
railties (= 3.0.0.rc)
|
58
|
+
railties (3.0.0.rc)
|
59
|
+
actionpack (= 3.0.0.rc)
|
60
|
+
activesupport (= 3.0.0.rc)
|
61
|
+
rake (>= 0.8.3)
|
62
|
+
thor (~> 0.14.0)
|
63
|
+
rake (0.8.7)
|
64
|
+
thor (0.14.0)
|
65
|
+
treetop (1.4.8)
|
66
|
+
polyglot (>= 0.3.1)
|
67
|
+
tzinfo (0.3.22)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
mocha
|
74
|
+
rails (= 3.0.0.rc)
|
data/MIT-LICENSE
ADDED
@@ -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.
|
data/README
ADDED
@@ -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
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "rake"
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "dynamic_form"
|
8
|
+
gem.summary = %Q{DynamicForm holds a few helper methods to help you deal with your Rails3 models}
|
9
|
+
gem.description = %Q{DynamicForm holds a few helper methods to help you deal with your Rails3 models. It includes the stripped out methods from Rails 2; error_message_on and error_messages_for}
|
10
|
+
gem.email = "joel@developwithstyle.com"
|
11
|
+
gem.homepage = "http://codaset.com/joelmoss/dynamic-form"
|
12
|
+
gem.authors = ["Joel Moss"]
|
13
|
+
gem.add_development_dependency "mocha", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Default: run unit tests.'
|
22
|
+
task :default => :test
|
23
|
+
|
24
|
+
desc 'Test the dynamic_form plugin.'
|
25
|
+
Rake::TestTask.new(:test) do |t|
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dynamic-form}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Joel Moss"]
|
12
|
+
s.date = %q{2010-08-09}
|
13
|
+
s.description = %q{DynamicForm holds a few helper methods to help you deal with your Rails3 models. It includes the stripped out methods from Rails 2; error_message_on and error_messages_for}
|
14
|
+
s.email = %q{joel@developwithstyle.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"MIT-LICENSE",
|
23
|
+
"README",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"dynamic-form.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/action_view/helpers/dynamic_form.rb",
|
29
|
+
"lib/action_view/locale/en.yml",
|
30
|
+
"test/dynamic_form_i18n_test.rb",
|
31
|
+
"test/dynamic_form_test.rb",
|
32
|
+
"test/test_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://codaset.com/joelmoss/dynamic-form}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{DynamicForm holds a few helper methods to help you deal with your Rails3 models}
|
39
|
+
s.test_files = [
|
40
|
+
"test/dynamic_form_i18n_test.rb",
|
41
|
+
"test/dynamic_form_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dynamic_form}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Joel Moss"]
|
12
|
+
s.date = %q{2010-08-09}
|
13
|
+
s.description = %q{DynamicForm holds a few helper methods to help you deal with your Rails3 models. It includes the stripped out methods from Rails 2; error_message_on and error_messages_for}
|
14
|
+
s.email = %q{joel@developwithstyle.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"MIT-LICENSE",
|
23
|
+
"README",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"dynamic-form.gemspec",
|
27
|
+
"dynamic_form.gemspec",
|
28
|
+
"init.rb",
|
29
|
+
"lib/action_view/helpers/dynamic_form.rb",
|
30
|
+
"lib/action_view/locale/en.yml",
|
31
|
+
"lib/dynamic_form.rb",
|
32
|
+
"test/dynamic_form_i18n_test.rb",
|
33
|
+
"test/dynamic_form_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://codaset.com/joelmoss/dynamic-form}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
40
|
+
s.summary = %q{DynamicForm holds a few helper methods to help you deal with your Rails3 models}
|
41
|
+
s.test_files = [
|
42
|
+
"test/dynamic_form_i18n_test.rb",
|
43
|
+
"test/dynamic_form_test.rb",
|
44
|
+
"test/test_helper.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/init.rb
ADDED
@@ -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__)
|
data/lib/dynamic_form.rb
ADDED
@@ -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,372 @@
|
|
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="field_with_errors"><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="field_with_errors"><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=\"field_with_errors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
|
157
|
+
end
|
158
|
+
|
159
|
+
assert_dom_equal(
|
160
|
+
%(<div class="field_with_errors"><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 accept-charset="UTF-8" action="create" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></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="field_with_errors"><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 accept-charset="UTF-8" action="update/1" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></div><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="field_with_errors"><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 accept-charset="UTF-8" action="create" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /><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="field_with_errors"><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 accept-charset="UTF-8" action="create" method="get"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></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="field_with_errors"><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 accept-charset="UTF-8" action="create" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></div><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 accept-charset="UTF-8" action="create" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></div><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 — <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 <em>empty</em></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 <em>empty</em></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 accept-charset="UTF-8" action="create" enctype="multipart/form-data" method="post"><div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></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="field_with_errors"><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 accept-charset="UTF-8" class="post_new" method="post" action="" id="post_new">) +
|
347
|
+
%(<div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></div>) +
|
348
|
+
%(<div class="formError">can't be empty</div>) +
|
349
|
+
%(<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>) +
|
350
|
+
%(</form>)
|
351
|
+
|
352
|
+
assert_dom_equal expected, output_buffer
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_default_form_builder_no_instance_variable
|
356
|
+
post = @post
|
357
|
+
@post = nil
|
358
|
+
|
359
|
+
form_for(post, :as => :post, :url => {}) do |f|
|
360
|
+
concat f.error_message_on('author_name')
|
361
|
+
concat f.error_messages
|
362
|
+
end
|
363
|
+
|
364
|
+
expected = %(<form accept-charset="UTF-8" class="post_new" method="post" action="" id="post_new">) +
|
365
|
+
%(<div style="margin:0;padding:0;display:inline"><input name="_snowman" type="hidden" value="☃" /></div>) +
|
366
|
+
%(<div class="formError">can't be empty</div>) +
|
367
|
+
%(<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>) +
|
368
|
+
%(</form>)
|
369
|
+
|
370
|
+
assert_dom_equal expected, output_buffer
|
371
|
+
end
|
372
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
require 'action_view'
|
6
|
+
require 'action_controller'
|
7
|
+
require 'action_controller/test_case'
|
8
|
+
require 'active_model'
|
9
|
+
require 'action_view/helpers/dynamic_form'
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joel Moss
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-09 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mocha
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: DynamicForm holds a few helper methods to help you deal with your Rails3 models. It includes the stripped out methods from Rails 2; error_message_on and error_messages_for
|
34
|
+
email: joel@developwithstyle.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README
|
41
|
+
files:
|
42
|
+
- .gitignore
|
43
|
+
- Gemfile
|
44
|
+
- Gemfile.lock
|
45
|
+
- MIT-LICENSE
|
46
|
+
- README
|
47
|
+
- Rakefile
|
48
|
+
- VERSION
|
49
|
+
- dynamic-form.gemspec
|
50
|
+
- dynamic_form.gemspec
|
51
|
+
- init.rb
|
52
|
+
- lib/action_view/helpers/dynamic_form.rb
|
53
|
+
- lib/action_view/locale/en.yml
|
54
|
+
- lib/dynamic_form.rb
|
55
|
+
- test/dynamic_form_i18n_test.rb
|
56
|
+
- test/dynamic_form_test.rb
|
57
|
+
- test/test_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://codaset.com/joelmoss/dynamic-form
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: DynamicForm holds a few helper methods to help you deal with your Rails3 models
|
90
|
+
test_files:
|
91
|
+
- test/dynamic_form_i18n_test.rb
|
92
|
+
- test/dynamic_form_test.rb
|
93
|
+
- test/test_helper.rb
|