Fingertips-form-san 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/form_san.rb +146 -24
- metadata +4 -7
- data/LICENSE +0 -20
- data/README.rdoc +0 -27
data/lib/form_san.rb
CHANGED
@@ -1,25 +1,74 @@
|
|
1
1
|
require 'action_view/helpers'
|
2
2
|
|
3
3
|
module FormSan
|
4
|
+
# = FormSan::FormBuilder
|
5
|
+
#
|
6
|
+
# FormSan has two goals:
|
7
|
+
# * Make it easier for the developer to write out a form
|
8
|
+
# * Make the resulting form more usable
|
9
|
+
#
|
10
|
+
# You can use the form builder by specifying it in the <tt>:builder</tt> option in <tt>form_for</tt>.
|
11
|
+
#
|
12
|
+
# form_for(@post, :builder => FormSan::FormBuilder)
|
13
|
+
#
|
14
|
+
# You can also make it the default form builder by specifying it in an initializer or <tt>environment.rb</tt>.
|
15
|
+
#
|
16
|
+
# ActionView::Base.default_form_builder = FormSan::FormBuilder
|
4
17
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
18
|
+
attr_reader :template
|
19
|
+
delegate :concat, :content_tag, :to => :template
|
20
|
+
|
21
|
+
# Wraps its content in a div with a fieldset class. You use it to group sets of fields.
|
22
|
+
#
|
23
|
+
# <% f.fieldset do %>
|
24
|
+
# <p>Please complete the form.</p>
|
25
|
+
# …
|
26
|
+
# <% end %>
|
27
|
+
#
|
28
|
+
# Generates
|
29
|
+
#
|
30
|
+
# <div class="fieldset">
|
31
|
+
# <p>Please complete the form.</p>
|
32
|
+
# …
|
33
|
+
# </div>
|
5
34
|
def fieldset(&block)
|
6
|
-
|
35
|
+
content_tag(:div, :class => 'fieldset', &block)
|
7
36
|
end
|
8
37
|
|
38
|
+
# Wraps its content in a div with a fields class. You use it to group fields.
|
39
|
+
#
|
40
|
+
# <% f.fields do %>
|
41
|
+
# <h4>Personal information</h4>
|
42
|
+
# …
|
43
|
+
# <% end %>
|
44
|
+
#
|
45
|
+
# Generates
|
46
|
+
#
|
47
|
+
# <div class="fields">
|
48
|
+
# <h4>Personal information</h4>
|
49
|
+
# …
|
50
|
+
# </div>
|
9
51
|
def fields(&block)
|
10
|
-
|
52
|
+
content_tag(:div, :class => 'fields', &block)
|
11
53
|
end
|
12
54
|
|
55
|
+
# Generates a short sentence in a paragraph describing which attributes have validation errors.
|
56
|
+
#
|
57
|
+
# <%= f.error_messages %>
|
58
|
+
#
|
59
|
+
# Generates something like
|
60
|
+
#
|
61
|
+
# <p class="errors">Sorry, there were problems with the title, published date, and description.</p>
|
13
62
|
def error_messages
|
14
63
|
unless @object.errors.count.zero?
|
15
64
|
attributes_with_errors = @object.errors.map { |attribute, _| attribute } - ['base']
|
16
|
-
|
65
|
+
content_tag(:p, :class => 'errors') do
|
17
66
|
if attributes_with_errors.size > 1
|
18
|
-
|
67
|
+
concat "Sorry, there were problems with the #{attributes_with_errors.to_sentence}."
|
19
68
|
elsif attributes_with_errors.size == 1
|
20
|
-
|
69
|
+
concat "Sorry, there was a problem with the #{attributes_with_errors.first}."
|
21
70
|
else
|
22
|
-
|
71
|
+
concat "#{@object.class} #{@object.errors.on(:base)}."
|
23
72
|
end
|
24
73
|
end
|
25
74
|
else
|
@@ -27,52 +76,125 @@ module FormSan
|
|
27
76
|
end
|
28
77
|
end
|
29
78
|
|
79
|
+
# Generates a short in a paragraph with the validation error of a specific attribute.
|
80
|
+
#
|
81
|
+
# <%= f.error_message(:title) %>
|
82
|
+
#
|
83
|
+
# Generates something like
|
84
|
+
#
|
85
|
+
# <p class="notice">Can't be blank</p>
|
30
86
|
def error_message(attribute)
|
31
87
|
unless @object.errors.on(attribute).blank?
|
32
|
-
|
33
|
-
|
88
|
+
content_tag(:p, :class => 'notice') do
|
89
|
+
concat ERB::Util.html_escape(@object.errors.on(attribute).mb_chars.capitalize)
|
34
90
|
end
|
35
91
|
else
|
36
92
|
''
|
37
93
|
end
|
38
94
|
end
|
39
95
|
|
40
|
-
|
96
|
+
# Generates a label for an attribute wrapped in a div. It allows you to specify an alternative
|
97
|
+
# humanized form of the attribute name and whether the associated field is optional or not.
|
98
|
+
#
|
99
|
+
# <%= f.label(:title) %>
|
100
|
+
# <div class="label"><label for="book_title">Title</label></div>
|
101
|
+
#
|
102
|
+
# <%= f.label(:title, 'Book title') %>
|
103
|
+
# <div class="label"><label for="book_title">Book title</label></div>
|
104
|
+
#
|
105
|
+
# <%= f.label(:title, :optional => true) %>
|
106
|
+
# <div class="label"><label for="book_title">Title <span>(optional)</span></label></div>
|
107
|
+
#
|
108
|
+
# When there is a validation error on the attribute, this will also be reflected in the HTML.
|
109
|
+
#
|
110
|
+
# <%= f.label(:title) %>
|
111
|
+
# <div class="label">
|
112
|
+
# <label for="book_title">Title <span>(optional)</span></label>
|
113
|
+
# <p class="notice">Can't be blank.</p>
|
114
|
+
# </div>
|
115
|
+
def wrapped_label(attribute, *args)
|
116
|
+
options = args.extract_options!
|
117
|
+
|
118
|
+
label_text = args.first || attribute.to_s.humanize
|
119
|
+
label_text << ' <span>(optional)</span>' if options[:optional]
|
120
|
+
|
121
|
+
content_tag(:div, :class => 'label') do
|
122
|
+
concat label(attribute, label_text)
|
123
|
+
concat error_message(attribute)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def field_with_extra_content(attribute, extra_content=nil, *args) #:nodoc:
|
41
128
|
options = args.extract_options!
|
42
129
|
classes = @object.errors.on(attribute) ? 'invalid field' : 'field'
|
43
130
|
|
44
|
-
|
45
|
-
|
46
|
-
label_text = args.first || attribute.to_s.humanize
|
47
|
-
label_text << ' <span>(optional)</span>' if options.delete(:optional)
|
48
|
-
@template.concat self.label(attribute, label_text)
|
49
|
-
end)
|
131
|
+
content_tag(:div, :class => classes) do
|
132
|
+
concat wrapped_label(attribute, args.first, :optional => options.delete(:optional))
|
50
133
|
|
51
134
|
case input_type = options.delete(:type).to_s
|
52
135
|
when 'textarea'
|
53
|
-
|
136
|
+
concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_text_area_tag(options)
|
54
137
|
else
|
55
|
-
|
138
|
+
concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_input_field_tag(input_type, options)
|
56
139
|
end
|
57
140
|
|
58
|
-
|
59
|
-
@template.concat extra_content unless extra_content.blank?
|
141
|
+
concat extra_content unless extra_content.blank?
|
60
142
|
@template.output_buffer # The block needs to return the current buffer
|
61
143
|
end
|
62
144
|
end
|
63
145
|
|
146
|
+
# Generates a field of a certain type wrapped in all sorts of HTML.
|
147
|
+
#
|
148
|
+
# <%= f.field(:title, :type => :text) %>
|
149
|
+
# <div class="field">
|
150
|
+
# <div class="label"><label for="book_title">Title</label></div>
|
151
|
+
# <input id="book_title" type="text" name="book[title]" value="Empire of the Sun" />
|
152
|
+
# </div>
|
153
|
+
#
|
154
|
+
# When there is a validation error on the attribute, this will also be reflected in the HTML.
|
155
|
+
#
|
156
|
+
# <div class="invalid field">
|
157
|
+
# <div class="label"><label for="book_title">Title</label></div>
|
158
|
+
# <input id="book_title" type="text" name="book[title]" value="" />
|
159
|
+
# <p class="notice">Can't be blank.</p>
|
160
|
+
# </div>
|
161
|
+
#
|
162
|
+
# You can also pass a block to field, that way you can include more HTML into the div.
|
163
|
+
#
|
164
|
+
# <% f.field(:title, :type => :text) do %>
|
165
|
+
# <p class="note">This will be shown on the overview page as the book title.</p>
|
166
|
+
# <% end %>
|
167
|
+
#
|
168
|
+
# <div class="field">
|
169
|
+
# <div class="label"><label for="book_title">Title</label></div>
|
170
|
+
# <input id="book_title" type="text" name="book[title]" value="Empire of the Sun" />
|
171
|
+
# <p class="note">This will be shown on the overview page as the book title.</p>
|
172
|
+
# </div>
|
173
|
+
#
|
174
|
+
# All options except <tt>:type</tt> are passed to the input.
|
175
|
+
#
|
176
|
+
# <%= f.field(:title, :type => :text, :class => 'small') %>
|
177
|
+
#
|
178
|
+
# <div class="field">
|
179
|
+
# <div class="label"><label for="book_title">Title</label></div>
|
180
|
+
# <input id="book_title" type="text" name="book[title]" value="Empire of the Sun" class="small" />
|
181
|
+
# </div>
|
64
182
|
def field(attribute, *args, &block)
|
65
183
|
if block_given?
|
66
|
-
|
184
|
+
concat field_with_extra_content(attribute, @template.capture(&block), *args)
|
67
185
|
else
|
68
|
-
|
186
|
+
field_with_extra_content(attribute, nil, *args)
|
69
187
|
end
|
70
188
|
end
|
71
189
|
end
|
72
190
|
end
|
73
191
|
|
74
|
-
|
75
|
-
|
76
|
-
|
192
|
+
module ActionView #:nodoc:
|
193
|
+
module Helpers #:nodoc:
|
194
|
+
class InstanceTag #:nodoc:
|
195
|
+
def error_wrapping(html_tag, has_error)
|
196
|
+
html_tag
|
197
|
+
end
|
198
|
+
end
|
77
199
|
end
|
78
200
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Fingertips-form-san
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manfred Stienstra
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -19,14 +19,11 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
24
|
-
- LICENSE
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
25
24
|
files:
|
26
25
|
- lib/form_san.rb
|
27
26
|
- rails/init.rb
|
28
|
-
- README.rdoc
|
29
|
-
- LICENSE
|
30
27
|
has_rdoc: true
|
31
28
|
homepage: http://github.com/Fingertips/form-san
|
32
29
|
post_install_message:
|
data/LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright © 2009 Fingertips, Manfred Stienstra
|
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.rdoc
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
= Form-San
|
2
|
-
|
3
|
-
Form-San is a lean extension for the Rails form builder.
|
4
|
-
|
5
|
-
<% form_for(@member, :builder => FormSan::FormBuilder) do |f| %>
|
6
|
-
<% f.fieldset do %>
|
7
|
-
<% f.fields do %>
|
8
|
-
<%= f.error_messages %>
|
9
|
-
|
10
|
-
<% f.field(:email, :type => :text, :class => 'medium') do %>
|
11
|
-
<p class="hint">You’ll log in using your email address.</p>
|
12
|
-
<% end %>
|
13
|
-
<%= f.field(:password, :type => :password) %>
|
14
|
-
|
15
|
-
<% f.fields_for(@member.artist) do |a| %>
|
16
|
-
<% a.field(:name, :type => :text, :class => 'medium') do %>
|
17
|
-
<p class="hint">Shown on the site.</p>
|
18
|
-
<% end %>
|
19
|
-
<% end %>
|
20
|
-
<% end %>
|
21
|
-
<% end %>
|
22
|
-
|
23
|
-
<div class="submit">
|
24
|
-
<%= f.submit 'Sign up' %>
|
25
|
-
<%= link_to 'Cancel', root_path, :class => 'cancel' %>
|
26
|
-
</div>
|
27
|
-
<% end %>
|