simple_form 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- data/README.rdoc +80 -14
- data/lib/generators/simple_form/templates/_form.html.erb +1 -11
- data/lib/generators/simple_form/templates/simple_form.rb +31 -6
- data/lib/simple_form.rb +38 -2
- data/lib/simple_form/action_view_extensions/builder.rb +53 -20
- data/lib/simple_form/components.rb +6 -5
- data/lib/simple_form/components/errors.rb +4 -6
- data/lib/simple_form/components/hints.rb +2 -2
- data/lib/simple_form/components/labels.rb +3 -5
- data/lib/simple_form/components/placeholders.rb +22 -0
- data/lib/simple_form/components/wrapper.rb +7 -0
- data/lib/simple_form/error_notification.rb +4 -6
- data/lib/simple_form/form_builder.rb +61 -55
- data/lib/simple_form/has_errors.rb +14 -0
- data/lib/simple_form/inputs/base.rb +45 -17
- data/lib/simple_form/inputs/block_input.rb +3 -2
- data/lib/simple_form/inputs/collection_input.rb +42 -17
- data/lib/simple_form/inputs/date_time_input.rb +3 -1
- data/lib/simple_form/inputs/hidden_input.rb +11 -2
- data/lib/simple_form/inputs/mapping_input.rb +16 -3
- data/lib/simple_form/inputs/numeric_input.rb +45 -8
- data/lib/simple_form/inputs/string_input.rb +13 -9
- data/lib/simple_form/map_type.rb +4 -4
- data/lib/simple_form/version.rb +1 -1
- data/test/action_view_extensions/builder_test.rb +155 -51
- data/test/components/error_test.rb +10 -8
- data/test/components/hint_test.rb +4 -8
- data/test/components/label_test.rb +22 -9
- data/test/components/wrapper_test.rb +16 -7
- data/test/error_notification_test.rb +4 -3
- data/test/form_builder_test.rb +81 -34
- data/test/inputs_test.rb +242 -3
- data/test/support/misc_helpers.rb +6 -4
- data/test/support/models.rb +26 -3
- data/test/test_helper.rb +13 -5
- metadata +25 -8
data/README.rdoc
CHANGED
@@ -4,6 +4,28 @@ Forms made easy (for Rails)!
|
|
4
4
|
|
5
5
|
SimpleForm aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of simple form is to not touch your way of defining the layout, letting you find the better design for your eyes. Good part of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.
|
6
6
|
|
7
|
+
== Information
|
8
|
+
|
9
|
+
=== Google Group
|
10
|
+
|
11
|
+
If you have any questions, comments, or concerns please use the Google Group instead of the GitHub Issues tracker:
|
12
|
+
|
13
|
+
http://groups.google.com/group/plataformatec-simpleform
|
14
|
+
|
15
|
+
=== RDocs
|
16
|
+
|
17
|
+
You can view the SimpleForm documentation in RDoc format here:
|
18
|
+
|
19
|
+
http://rubydoc.info/github/plataformatec/simple_form/master/frames
|
20
|
+
|
21
|
+
If you need to use SimpleForm with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
|
22
|
+
|
23
|
+
=== Bug reports
|
24
|
+
|
25
|
+
If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us fixing the possible bug. We also encourage you to help even more by forking and sending us a pull request.
|
26
|
+
|
27
|
+
http://github.com/plataformatec/simple_form/issues
|
28
|
+
|
7
29
|
== Installation
|
8
30
|
|
9
31
|
Install the gem:
|
@@ -18,6 +40,10 @@ Run the generator:
|
|
18
40
|
|
19
41
|
rails generate simple_form:install
|
20
42
|
|
43
|
+
Also, if you want to use the country select, you will need the country_select plugin, install with following command:
|
44
|
+
|
45
|
+
rails plugin install git://github.com/rails/country_select.git
|
46
|
+
|
21
47
|
And you are ready to go. Since this branch aims Rails 3 support,
|
22
48
|
if you want to use it with Rails 2.3 you should check this branch:
|
23
49
|
|
@@ -37,11 +63,12 @@ To start using SimpleForm you just have to use the helper it provides:
|
|
37
63
|
|
38
64
|
This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).
|
39
65
|
|
40
|
-
You can overwrite the default label by passing it to the input method, or even
|
66
|
+
You can overwrite the default label by passing it to the input method, add a hint or even a placeholder:
|
41
67
|
|
42
68
|
<%= simple_form_for @user do |f| %>
|
43
69
|
<%= f.input :username, :label => 'Your username please' %>
|
44
70
|
<%= f.input :password, :hint => 'No special characters.' %>
|
71
|
+
<%= f.input :email, :placeholder => 'user@domain.com' %>
|
45
72
|
<%= f.button :submit %>
|
46
73
|
<% end %>
|
47
74
|
|
@@ -54,6 +81,15 @@ You can also disable labels, hints or error or configure the html of any of them
|
|
54
81
|
<%= f.button :submit %>
|
55
82
|
<% end %>
|
56
83
|
|
84
|
+
It is also possible to pass any html attribute straight to the input, by using the :input_html option, for instance:
|
85
|
+
|
86
|
+
<%= simple_form_for @user do |f| %>
|
87
|
+
<%= f.input :username, :input_html => { :class => 'special' } %>
|
88
|
+
<%= f.input :password, :input_html => { :maxlength => 20 } %>
|
89
|
+
<%= f.input :remember_me, :input_html => { :value => '1' } %>
|
90
|
+
<%= f.button :submit %>
|
91
|
+
<% end %>
|
92
|
+
|
57
93
|
By default all inputs are required, which means an * is prepended to the label, but you can disable it in any input you want:
|
58
94
|
|
59
95
|
<%= simple_form_for @user do |f| %>
|
@@ -75,6 +111,22 @@ SimpleForm also lets you overwrite the default input type it creates:
|
|
75
111
|
|
76
112
|
So instead of a checkbox for the :accepts attribute, you'll have a pair of radio buttons with yes/no labels and a text area instead of a text field for the description. You can also render boolean attributes using :as => :select to show a dropdown.
|
77
113
|
|
114
|
+
It is also possible to give the :disabled option to SimpleForm, and it'll automatically mark the wrapper as disabled with a css class, so you can style labels, hints and other components inside the wrapper as well:
|
115
|
+
|
116
|
+
<%= simple_form_for @user do |f| %>
|
117
|
+
<%= f.input :username, :disabled => true, :hint => "You cannot change your username." %>
|
118
|
+
<%= f.button :submit %>
|
119
|
+
<% end %>
|
120
|
+
|
121
|
+
SimpleForm accepts same options as their corresponding input type helper in Rails:
|
122
|
+
|
123
|
+
<%= simple_form_for @user do |f| %>
|
124
|
+
<%= f.input :date_of_birth, :as => :date, :start_year => Date.today.year - 90,
|
125
|
+
:end_year => Date.today.year - 12, :discard_day => true,
|
126
|
+
:order => [:month, :year] %>
|
127
|
+
<%= f.button :submit %>
|
128
|
+
<% end %>
|
129
|
+
|
78
130
|
SimpleForm also allows you using label, hint and error helpers it provides:
|
79
131
|
|
80
132
|
<%= simple_form_for @user do |f| %>
|
@@ -105,7 +157,7 @@ Collection inputs accepts two other options beside collections:
|
|
105
157
|
|
106
158
|
* value_method => the value method to be applied to the collection to retrieve the value
|
107
159
|
|
108
|
-
Those methods are useful to manipulate the given collection. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
|
160
|
+
Those methods are useful to manipulate the given collection. Both of these options also except lambda/procs incase you want to calculate the value or label in a special way eg. custom translation. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
|
109
161
|
|
110
162
|
f.input :age, :collection => 18..60, :prompt => "Select your age"
|
111
163
|
|
@@ -212,7 +264,7 @@ Creates a collection of radio inputs with labels associated (same API as collect
|
|
212
264
|
Creates a collection of check boxes with labels associated (same API as collection_select):
|
213
265
|
|
214
266
|
form_for @user do |f|
|
215
|
-
f.
|
267
|
+
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
216
268
|
end
|
217
269
|
|
218
270
|
<input name="user[options][]" type="hidden" value="" />
|
@@ -232,7 +284,9 @@ SimpleForm comes with a lot of default mappings:
|
|
232
284
|
string text field string
|
233
285
|
email email field string with name matching "email"
|
234
286
|
url url field string with name matching "url"
|
287
|
+
tel tel field string with name matching "phone"
|
235
288
|
password password field string with name matching "password"
|
289
|
+
search search -
|
236
290
|
text text area text
|
237
291
|
file file field string, responding to file methods
|
238
292
|
hidden hidden field -
|
@@ -250,7 +304,7 @@ SimpleForm comes with a lot of default mappings:
|
|
250
304
|
|
251
305
|
== I18n
|
252
306
|
|
253
|
-
SimpleForm uses all power of I18n API to lookup labels
|
307
|
+
SimpleForm uses all power of I18n API to lookup labels, hints and placeholders. To customize your forms you can create a locale file like this:
|
254
308
|
|
255
309
|
en:
|
256
310
|
simple_form:
|
@@ -262,10 +316,14 @@ SimpleForm uses all power of I18n API to lookup labels and hints for you. To cus
|
|
262
316
|
user:
|
263
317
|
username: 'User name to sign in.'
|
264
318
|
password: 'No special characters, please.'
|
319
|
+
placeholders:
|
320
|
+
user:
|
321
|
+
username: 'Your username'
|
322
|
+
password: '****'
|
265
323
|
|
266
|
-
And your forms will use this information to render
|
324
|
+
And your forms will use this information to render the components for you.
|
267
325
|
|
268
|
-
SimpleForm also lets you be more specific, separating lookups through actions for
|
326
|
+
SimpleForm also lets you be more specific, separating lookups through actions for labels, hints and placeholders. Let's say you want a different label for new and edit actions, the locale file would be something like:
|
269
327
|
|
270
328
|
en:
|
271
329
|
simple_form:
|
@@ -287,12 +345,24 @@ This way SimpleForm will figure out the right translation for you, based on the
|
|
287
345
|
hints:
|
288
346
|
username: 'User name to sign in.'
|
289
347
|
password: 'No special characters, please.'
|
348
|
+
placeholders:
|
349
|
+
username: 'Your username'
|
350
|
+
password: '****'
|
351
|
+
|
352
|
+
SimpleForm will always look for a default attribute translation if no specific is found inside the model key. In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other translation is found for labels.
|
290
353
|
|
291
|
-
|
354
|
+
Finally, you can also overwrite any label, hint or placeholder inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
|
292
355
|
|
293
|
-
|
356
|
+
It's also possible to translate buttons, using Rails' built-in I18n support:
|
294
357
|
|
295
|
-
|
358
|
+
en:
|
359
|
+
helpers:
|
360
|
+
submit:
|
361
|
+
user:
|
362
|
+
create: "Add %{model}"
|
363
|
+
update: "Save Changes"
|
364
|
+
|
365
|
+
There are other options that can be configured through I18n API, such as required text and boolean. Be sure to check our locale file or the one copied to your application after you run "rails generate simple_form:install".
|
296
366
|
|
297
367
|
== Configuration
|
298
368
|
|
@@ -309,10 +379,6 @@ Please refer to TODO file.
|
|
309
379
|
* José Valim (http://github.com/josevalim)
|
310
380
|
* Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
|
311
381
|
|
312
|
-
==
|
313
|
-
|
314
|
-
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
315
|
-
|
316
|
-
http://github.com/plataformatec/simple_form/issues
|
382
|
+
== License
|
317
383
|
|
318
384
|
MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
@@ -1,15 +1,5 @@
|
|
1
1
|
<%%= simple_form_for(@<%= singular_name %>) do |f| %>
|
2
|
-
|
3
|
-
<div id="error_explanation">
|
4
|
-
<h2><%%= pluralize(@<%= singular_name %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:</h2>
|
5
|
-
|
6
|
-
<ul>
|
7
|
-
<%% @<%= singular_name %>.errors.full_messages.each do |msg| %>
|
8
|
-
<li><%%= msg %></li>
|
9
|
-
<%% end %>
|
10
|
-
</ul>
|
11
|
-
</div>
|
12
|
-
<%% end %>
|
2
|
+
<%%= f.error_notification %>
|
13
3
|
|
14
4
|
<div class="inputs">
|
15
5
|
<%- attributes.each do |attribute| -%>
|
@@ -1,13 +1,18 @@
|
|
1
1
|
# Use this setup block to configure all options available in SimpleForm.
|
2
2
|
SimpleForm.setup do |config|
|
3
|
-
|
4
3
|
# Components used by the form builder to generate a complete input. You can remove
|
5
4
|
# any of them, change the order, or even add your own components to the stack.
|
6
|
-
# config.components = [ :label_input, :hint, :error ]
|
5
|
+
# config.components = [ :placeholder, :label_input, :hint, :error ]
|
7
6
|
|
8
7
|
# Default tag used on hints.
|
9
8
|
# config.hint_tag = :span
|
10
9
|
|
10
|
+
# CSS class to add to all hint tags.
|
11
|
+
# config.hint_class = :hint
|
12
|
+
|
13
|
+
# Default tag used on errors.
|
14
|
+
# config.error_class = :error
|
15
|
+
|
11
16
|
# Default tag used on errors.
|
12
17
|
# config.error_tag = :span
|
13
18
|
|
@@ -17,6 +22,12 @@ SimpleForm.setup do |config|
|
|
17
22
|
# Default tag used for error notification helper.
|
18
23
|
# config.error_notification_tag = :p
|
19
24
|
|
25
|
+
# CSS class to add for error notification helper.
|
26
|
+
# config.error_notification_class = :error_notification
|
27
|
+
|
28
|
+
# ID to add for error notification helper.
|
29
|
+
# config.error_notification_id = nil
|
30
|
+
|
20
31
|
# You can wrap all inputs in a pre-defined tag.
|
21
32
|
# config.wrapper_tag = :div
|
22
33
|
|
@@ -26,11 +37,11 @@ SimpleForm.setup do |config|
|
|
26
37
|
# CSS class to add to the wrapper if the field has errors.
|
27
38
|
# config.wrapper_error_class = :field_with_errors
|
28
39
|
|
29
|
-
#
|
30
|
-
# config.
|
40
|
+
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
|
41
|
+
# config.collection_wrapper_tag = nil
|
31
42
|
|
32
|
-
#
|
33
|
-
# config.
|
43
|
+
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
|
44
|
+
# config.item_wrapper_tag = nil
|
34
45
|
|
35
46
|
# Series of attemps to detect a default label method for collection.
|
36
47
|
# config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
|
@@ -38,6 +49,17 @@ SimpleForm.setup do |config|
|
|
38
49
|
# Series of attemps to detect a default value method for collection.
|
39
50
|
# config.collection_value_methods = [ :id, :to_s ]
|
40
51
|
|
52
|
+
# How the label text should be generated altogether with the required text.
|
53
|
+
# config.label_text = lambda { |label, required| "#{required} #{label}" }
|
54
|
+
|
55
|
+
# Whether attributes are required by default (or not). Default is true.
|
56
|
+
# config.required_by_default = true
|
57
|
+
|
58
|
+
# Custom mappings for input types. This should be a hash containing a regexp
|
59
|
+
# to match as key, and the input type that will be used when the field name
|
60
|
+
# matches the regexp as value.
|
61
|
+
# config.input_mappings = { /count/ => :integer }
|
62
|
+
|
41
63
|
# Collection of methods to detect if a file type was given.
|
42
64
|
# config.file_methods = [ :file?, :public_filename ]
|
43
65
|
|
@@ -49,4 +71,7 @@ SimpleForm.setup do |config|
|
|
49
71
|
|
50
72
|
# Default size for text inputs.
|
51
73
|
# config.default_input_size = 50
|
74
|
+
|
75
|
+
# When false, do not use translations for labels, hints or placeholders.
|
76
|
+
# config.translate = true
|
52
77
|
end
|
data/lib/simple_form.rb
CHANGED
@@ -6,6 +6,7 @@ module SimpleForm
|
|
6
6
|
autoload :Components, 'simple_form/components'
|
7
7
|
autoload :ErrorNotification, 'simple_form/error_notification'
|
8
8
|
autoload :FormBuilder, 'simple_form/form_builder'
|
9
|
+
autoload :HasErrors, 'simple_form/has_errors'
|
9
10
|
autoload :I18nCache, 'simple_form/i18n_cache'
|
10
11
|
autoload :Inputs, 'simple_form/inputs'
|
11
12
|
autoload :MapType, 'simple_form/map_type'
|
@@ -14,10 +15,18 @@ module SimpleForm
|
|
14
15
|
mattr_accessor :hint_tag
|
15
16
|
@@hint_tag = :span
|
16
17
|
|
18
|
+
# CSS class to add to all hint tags.
|
19
|
+
mattr_accessor :hint_class
|
20
|
+
@@hint_class = :hint
|
21
|
+
|
17
22
|
# Default tag used on errors.
|
18
23
|
mattr_accessor :error_tag
|
19
24
|
@@error_tag = :span
|
20
25
|
|
26
|
+
# CSS class to add to all error tags.
|
27
|
+
mattr_accessor :error_class
|
28
|
+
@@error_class = :error
|
29
|
+
|
21
30
|
# Method used to tidy up errors.
|
22
31
|
mattr_accessor :error_method
|
23
32
|
@@error_method = :first
|
@@ -26,9 +35,17 @@ module SimpleForm
|
|
26
35
|
mattr_accessor :error_notification_tag
|
27
36
|
@@error_notification_tag = :p
|
28
37
|
|
38
|
+
# CSS class to add for error notification helper.
|
39
|
+
mattr_accessor :error_notification_class
|
40
|
+
@@error_notification_class = :error_notification
|
41
|
+
|
42
|
+
# ID to add for error notification helper.
|
43
|
+
mattr_accessor :error_notification_id
|
44
|
+
@@error_notification_id = nil
|
45
|
+
|
29
46
|
# Components used by the form builder.
|
30
47
|
mattr_accessor :components
|
31
|
-
@@components = [ :label_input, :hint, :error ]
|
48
|
+
@@components = [ :placeholder, :label_input, :hint, :error ]
|
32
49
|
|
33
50
|
# Series of attemps to detect a default label method for collection.
|
34
51
|
mattr_accessor :collection_label_methods
|
@@ -38,6 +55,14 @@ module SimpleForm
|
|
38
55
|
mattr_accessor :collection_value_methods
|
39
56
|
@@collection_value_methods = [ :id, :to_s ]
|
40
57
|
|
58
|
+
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
|
59
|
+
mattr_accessor :collection_wrapper_tag
|
60
|
+
@@collection_wrapper_tag = nil
|
61
|
+
|
62
|
+
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
|
63
|
+
mattr_accessor :item_wrapper_tag
|
64
|
+
@@item_wrapper_tag = nil
|
65
|
+
|
41
66
|
# You can wrap all inputs in a pre-defined tag. Default is a div.
|
42
67
|
mattr_accessor :wrapper_tag
|
43
68
|
@@wrapper_tag = :div
|
@@ -46,7 +71,7 @@ module SimpleForm
|
|
46
71
|
mattr_accessor :wrapper_class
|
47
72
|
@@wrapper_class = :input
|
48
73
|
|
49
|
-
# You can define the class to add to the wrapper when the field has errors. Default is
|
74
|
+
# You can define the class to add to the wrapper when the field has errors. Default is field_with_errors.
|
50
75
|
mattr_accessor :wrapper_error_class
|
51
76
|
@@wrapper_error_class = :field_with_errors
|
52
77
|
|
@@ -62,6 +87,12 @@ module SimpleForm
|
|
62
87
|
mattr_accessor :file_methods
|
63
88
|
@@file_methods = [ :mounted_as, :file?, :public_filename ]
|
64
89
|
|
90
|
+
# Custom mappings for input types. This should be a hash containing a regexp
|
91
|
+
# to match as key, and the input type that will be used when the field name
|
92
|
+
# matches the regexp as value, such as { /count/ => :integer }.
|
93
|
+
mattr_accessor :input_mappings
|
94
|
+
@@input_mappings = nil
|
95
|
+
|
65
96
|
# Default priority for time_zone inputs.
|
66
97
|
mattr_accessor :time_zone_priority
|
67
98
|
@@time_zone_priority = nil
|
@@ -74,6 +105,11 @@ module SimpleForm
|
|
74
105
|
mattr_accessor :default_input_size
|
75
106
|
@@default_input_size = 50
|
76
107
|
|
108
|
+
# When off, do not use translations in hint, labels and placeholders.
|
109
|
+
# It is a small performance improvement if you are not using such features.
|
110
|
+
mattr_accessor :translate
|
111
|
+
@@translate = true
|
112
|
+
|
77
113
|
# Default way to setup SimpleForm. Run rails generate simple_form:install
|
78
114
|
# to create a fresh initializer with all configuration values.
|
79
115
|
def self.setup
|
@@ -29,16 +29,17 @@ module SimpleForm
|
|
29
29
|
# * disabled => the value or values that should be disabled. Accepts a single
|
30
30
|
# item or an array of items.
|
31
31
|
#
|
32
|
+
# * collection_wrapper_tag => the tag to wrap the entire collection.
|
33
|
+
#
|
34
|
+
# * item_wrapper_tag => the tag to wrap each item in the collection.
|
35
|
+
#
|
32
36
|
def collection_radio(attribute, collection, value_method, text_method, options={}, html_options={})
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
radio_button(attribute, value, default_html_options) <<
|
40
|
-
label("#{attribute}_#{value}", text, :class => "collection_radio")
|
41
|
-
end.join.html_safe
|
37
|
+
render_collection(
|
38
|
+
attribute, collection, value_method, text_method, options, html_options
|
39
|
+
) do |value, text, default_html_options|
|
40
|
+
radio = radio_button(attribute, value, default_html_options)
|
41
|
+
collection_label(attribute, value, radio, text, :class => "collection_radio")
|
42
|
+
end
|
42
43
|
end
|
43
44
|
|
44
45
|
# Creates a collection of check boxes for each item in the collection, associated
|
@@ -48,15 +49,15 @@ module SimpleForm
|
|
48
49
|
# == Examples
|
49
50
|
#
|
50
51
|
# form_for @user do |f|
|
51
|
-
# f.
|
52
|
+
# f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
52
53
|
# end
|
53
54
|
#
|
54
55
|
# <input name="user[options][]" type="hidden" value="" />
|
55
56
|
# <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
|
56
|
-
# <label class="
|
57
|
+
# <label class="collection_check_boxes" for="user_options_true">Yes</label>
|
57
58
|
# <input name="user[options][]" type="hidden" value="" />
|
58
59
|
# <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
|
59
|
-
# <label class="
|
60
|
+
# <label class="collection_check_boxes" for="user_options_false">No</label>
|
60
61
|
#
|
61
62
|
# == Options
|
62
63
|
#
|
@@ -68,17 +69,19 @@ module SimpleForm
|
|
68
69
|
# * disabled => the value or values that should be disabled. Accepts a single
|
69
70
|
# item or an array of items.
|
70
71
|
#
|
72
|
+
# * collection_wrapper_tag => the tag to wrap the entire collection.
|
73
|
+
#
|
74
|
+
# * item_wrapper_tag => the tag to wrap each item in the collection.
|
75
|
+
#
|
71
76
|
def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
default_html_options = default_html_options_for_collection(item, value, options, html_options)
|
77
|
+
render_collection(
|
78
|
+
attribute, collection, value_method, text_method, options, html_options
|
79
|
+
) do |value, text, default_html_options|
|
77
80
|
default_html_options[:multiple] = true
|
78
81
|
|
79
|
-
check_box(attribute, default_html_options, value, '')
|
80
|
-
|
81
|
-
end
|
82
|
+
check_box = check_box(attribute, default_html_options, value, '')
|
83
|
+
collection_label(attribute, value, check_box, text, :class => "collection_check_boxes")
|
84
|
+
end
|
82
85
|
end
|
83
86
|
|
84
87
|
# Wrapper for using simple form inside a default rails form.
|
@@ -98,6 +101,11 @@ module SimpleForm
|
|
98
101
|
|
99
102
|
private
|
100
103
|
|
104
|
+
# Wraps the given component in a label, for better accessibility with collections.
|
105
|
+
def collection_label(attribute, value, component_tag, label_text, html_options) #:nodoc:
|
106
|
+
label(sanitize_attribute_name(attribute, value), component_tag << label_text.to_s, html_options)
|
107
|
+
end
|
108
|
+
|
101
109
|
# Generate default options for collection helpers, such as :checked and
|
102
110
|
# :disabled.
|
103
111
|
def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
|
@@ -117,6 +125,31 @@ module SimpleForm
|
|
117
125
|
|
118
126
|
html_options
|
119
127
|
end
|
128
|
+
|
129
|
+
def sanitize_attribute_name(attribute, value)
|
130
|
+
"#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
|
131
|
+
end
|
132
|
+
|
133
|
+
def render_collection(attribute, collection, value_method, text_method, options={}, html_options={}) #:nodoc:
|
134
|
+
collection_wrapper_tag = options[:collection_wrapper_tag] || SimpleForm.collection_wrapper_tag
|
135
|
+
item_wrapper_tag = options[:item_wrapper_tag] || SimpleForm.item_wrapper_tag
|
136
|
+
|
137
|
+
rendered_collection = collection.map do |item|
|
138
|
+
value = value_for_collection(item, value_method)
|
139
|
+
text = value_for_collection(item, text_method)
|
140
|
+
default_html_options = default_html_options_for_collection(item, value, options, html_options)
|
141
|
+
|
142
|
+
rendered_item = yield value, text, default_html_options
|
143
|
+
|
144
|
+
item_wrapper_tag ? @template.content_tag(item_wrapper_tag, rendered_item) : rendered_item
|
145
|
+
end.join.html_safe
|
146
|
+
|
147
|
+
collection_wrapper_tag ? @template.content_tag(collection_wrapper_tag, rendered_collection) : rendered_collection
|
148
|
+
end
|
149
|
+
|
150
|
+
def value_for_collection(item, value) #:nodoc:
|
151
|
+
value.respond_to?(:call) ? value.call(item) : item.send(value)
|
152
|
+
end
|
120
153
|
end
|
121
154
|
end
|
122
155
|
end
|