simple_form 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/.gitignore +2 -0
  2. data/.gitmodules +3 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.rdoc +109 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +82 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +73 -12
  9. data/Rakefile +27 -0
  10. data/lib/generators/simple_form/templates/_form.html.erb +1 -1
  11. data/lib/generators/simple_form/templates/_form.html.haml +2 -10
  12. data/lib/generators/simple_form/templates/_form.html.slim +10 -0
  13. data/lib/generators/simple_form/templates/simple_form.rb +20 -4
  14. data/lib/simple_form/action_view_extensions/builder.rb +39 -15
  15. data/lib/simple_form/action_view_extensions/form_helper.rb +5 -5
  16. data/lib/simple_form/components/errors.rb +5 -1
  17. data/lib/simple_form/components/labels.rb +2 -2
  18. data/lib/simple_form/form_builder.rb +131 -38
  19. data/lib/simple_form/inputs/base.rb +52 -6
  20. data/lib/simple_form/inputs/collection_input.rb +5 -1
  21. data/lib/simple_form/inputs/date_time_input.rb +4 -0
  22. data/lib/simple_form/inputs/mapping_input.rb +0 -6
  23. data/lib/simple_form/inputs/numeric_input.rb +15 -7
  24. data/lib/simple_form/inputs/priority_input.rb +5 -1
  25. data/lib/simple_form/inputs/string_input.rb +14 -3
  26. data/lib/simple_form/map_type.rb +6 -3
  27. data/lib/simple_form/version.rb +1 -1
  28. data/lib/simple_form.rb +28 -1
  29. data/simple_form.gemspec +22 -0
  30. data/test/action_view_extensions/builder_test.rb +39 -7
  31. data/test/action_view_extensions/form_helper_test.rb +12 -0
  32. data/test/components/label_test.rb +54 -0
  33. data/test/discovery_inputs.rb +21 -0
  34. data/test/form_builder_test.rb +230 -0
  35. data/test/inputs_test.rb +234 -14
  36. data/test/support/misc_helpers.rb +26 -0
  37. data/test/support/models.rb +41 -3
  38. data/test/test_helper.rb +13 -4
  39. metadata +24 -27
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle/
2
+ pkg/
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "test/support/country_select"]
2
+ path = test/support/country_select
3
+ url = git://github.com/rails/country_select
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ script: "git submodule update --init && rake test"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - ree
6
+ - jruby
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,109 @@
1
+ == 1.4.0
2
+
3
+ * enhancements
4
+ * Add label class configuration option (by github.com/reu)
5
+ * Improve i18n lookup (labels/hints/placeholders) for nested models
6
+ * Use the given custom builder with simple_fields_for (by github.com/giniedp)
7
+ * Add slim form generator (by github.com/fagiani)
8
+ * Add form_class configuration option (by github.com/fagiani)
9
+ * Default step of "any" for number input with non integer attributes (by github.com/fedesoria)
10
+ * Add option to disable HTML5 browser validations on all forms (by github.com/coryschires)
11
+ * Add option to disable all HTML5 extensions (by github.com/wolframarnold)
12
+ * Add input_field helper to form builder (by github.com/jeroenhouben)
13
+ * Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic)
14
+ * Add full_error on that shows the error with the attribute name
15
+
16
+ * bug fix
17
+ * Fix for file attributes automatic detection, to work with virtual attributes
18
+ * Fix for numeric fields validation options using symbols and procs
19
+ * Fix password attributes to add size and maxlength options the same way as string (by github.com/fedesoria)
20
+ * Fix bug with custom form builders and new mappings being added to the superclass builder (by github.com/rdvdijk)
21
+ * Fix HTML validation issue with collection_check_boxes
22
+
23
+ == 1.3.1
24
+
25
+ * enhancements
26
+ * Add :autofocus HTML5 attribute support (by github.com/jpzwarte)
27
+ * Add possibility to specify custom builder and inherit mappings (by github.com/rejeep)
28
+ * Make custom mappings work with all attributes types (by github.com/rafaelfranca)
29
+ * Add support for procs/lambdas in text/value methods for collection_select
30
+
31
+ * deprecation
32
+ * removed the deprecated :remote_form_for
33
+
34
+ * bug fix
35
+ * Only add the "required" HTML 5 attribute for valid inputs, disable in selects (not allowed)
36
+ * Fix error when using hints without an attribute (by github.com/butsjoh and github.com/rafaelfranca)
37
+ * Fix messy html output for hint, error and label components (by github.com/butsjoh and github.com/rafaelfranca)
38
+ * Allow direct setting of for attribute on label (by github.com/Bertg)
39
+
40
+ == 1.3.0
41
+
42
+ * enhancements
43
+ * Allow collection input to accept a collection of symbols
44
+ * Add default css class to button
45
+ * Allow forms for objects that don't respond to the "errors" method
46
+ * collection_check_boxes and collection_radio now wrap the input in the label
47
+ * Automatic add min/max values for numeric attributes based on validations and step for integers - HTML5 (by github.com/dasch)
48
+ * Add :placeholder option for string inputs, allowing customization through I18n - HTML5 (by github.com/jonathan)
49
+ * Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5
50
+ * Add :required html attribute for required inputs - HTML5
51
+ * Add optional :components option to input to control component rendering (by github.com/khoan)
52
+ * Add SimpleForm.translate as an easy way to turn off SimpleForm internal translations
53
+ * Add :disabled option for all inputs (by github.com/fabiob)
54
+ * Add collection wrapper tag and item wrapper tag to wrap elements in collection helpers - radio / check boxes
55
+ * Add SimpleForm.input_mappings to allow configuring custom mappings for inputs (by github.com/TMaYaD)
56
+
57
+ * bug fix
58
+ * Search for validations on both association and attribute
59
+ * Use controller.action_name to lookup action only when available, to fix issue with Rspec views tests (by github.com/rafaelfranca)
60
+
61
+ == 1.2.2
62
+
63
+ * enhancements
64
+ * Compatibility with Rails 3 RC
65
+
66
+ == 1.2.1
67
+
68
+ * enhancements
69
+ * Added haml generator support (by github.com/grimen)
70
+ * Added error_notification message to form builder
71
+ * Added required by default as configuration option
72
+ * Added label_input as component, allowing boolean to change its order (input appearing first than label)
73
+ * Added error_method to tidy up how errors are exhibited
74
+ * Added error class on wrappers (by github.com/jduff)
75
+ * Changed numeric types to have type=number for HTML5
76
+
77
+ == 1.2.0
78
+
79
+ * deprecation
80
+ * Changed simple_form_install generator to simple_form:install
81
+
82
+ * enhancements
83
+ * Added support to presence validation to check if attribute is required or not (by github.com/gcirne)
84
+ * Added .input as class to wrapper tag
85
+ * Added config options for hint and error tags (by github.com/tjogin)
86
+
87
+ == 1.1.3
88
+
89
+ * deprecation
90
+ * removed :conditions, :order, :joins and :include support in f.association
91
+
92
+ == 1.1.2
93
+
94
+ * bug fix
95
+ * Ensure type is set to "text" and not "string"
96
+
97
+ == 1.1.1
98
+
99
+ * bug fix
100
+ * Fix some escaping issues
101
+
102
+ == 1.1.0
103
+
104
+ * enhancements
105
+ * Rails 3 support with generators, templates and HTML 5
106
+
107
+ == 1.0
108
+
109
+ * First release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "~> 3.0.7"
4
+
5
+ group :test do
6
+ gem "mocha", :require => false
7
+ gem "ruby-debug", :require => false, :platform => :ruby_18
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ arel (2.0.9)
32
+ builder (2.1.2)
33
+ columnize (0.3.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ i18n (0.5.0)
37
+ linecache (0.43)
38
+ mail (2.2.18)
39
+ activesupport (>= 2.3.6)
40
+ i18n (>= 0.4.0)
41
+ mime-types (~> 1.16)
42
+ treetop (~> 1.4.8)
43
+ mime-types (1.16)
44
+ mocha (0.9.10)
45
+ rake
46
+ polyglot (0.3.1)
47
+ rack (1.2.2)
48
+ rack-mount (0.6.14)
49
+ rack (>= 1.0.0)
50
+ rack-test (0.5.7)
51
+ rack (>= 1.0)
52
+ rails (3.0.7)
53
+ actionmailer (= 3.0.7)
54
+ actionpack (= 3.0.7)
55
+ activerecord (= 3.0.7)
56
+ activeresource (= 3.0.7)
57
+ activesupport (= 3.0.7)
58
+ bundler (~> 1.0)
59
+ railties (= 3.0.7)
60
+ railties (3.0.7)
61
+ actionpack (= 3.0.7)
62
+ activesupport (= 3.0.7)
63
+ rake (>= 0.8.7)
64
+ thor (~> 0.14.4)
65
+ rake (0.8.7)
66
+ ruby-debug (0.10.4)
67
+ columnize (>= 0.1)
68
+ ruby-debug-base (~> 0.10.4.0)
69
+ ruby-debug-base (0.10.4)
70
+ linecache (>= 0.3)
71
+ thor (0.14.6)
72
+ treetop (1.4.9)
73
+ polyglot (>= 0.3.1)
74
+ tzinfo (0.3.27)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ mocha
81
+ rails (~> 3.0.7)
82
+ ruby-debug
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 PlataformaTec http://blog.plataformatec.com.br/
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 CHANGED
@@ -127,17 +127,18 @@ SimpleForm accepts same options as their corresponding input type helper in Rail
127
127
  <%= f.button :submit %>
128
128
  <% end %>
129
129
 
130
- SimpleForm also allows you using label, hint and error helpers it provides:
130
+ SimpleForm also allows you to use label, hint, input_field, error and full_error helpers it provides (please take a look at the rdocs for each method for more info):
131
131
 
132
132
  <%= simple_form_for @user do |f| %>
133
133
  <%= f.label :username %>
134
- <%= f.text_field :username %>
135
- <%= f.error :username, :id => 'user_name_error' %>
134
+ <%= f.input_field :username %>
136
135
  <%= f.hint 'No special characters, please!' %>
136
+ <%= f.error :username, :id => 'user_name_error' %>
137
+ <%= f.full_error :token %>
137
138
  <%= f.submit 'Save' %>
138
139
  <% end %>
139
140
 
140
- Any extra option passed to label, hint or error will be rendered as html option.
141
+ Any extra option passed to these methods will be rendered as html option.
141
142
 
142
143
  === Collections
143
144
 
@@ -157,7 +158,7 @@ Collection inputs accepts two other options beside collections:
157
158
 
158
159
  * value_method => the value method to be applied to the collection to retrieve the value
159
160
 
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:
161
+ Those methods are useful to manipulate the given collection. Both of these options also except lambda/procs in case 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:
161
162
 
162
163
  f.input :age, :collection => 18..60, :prompt => "Select your age"
163
164
 
@@ -302,6 +303,50 @@ SimpleForm comes with a lot of default mappings:
302
303
  country country select string with name matching "country"
303
304
  time_zone time zone select string with name matching "time_zone"
304
305
 
306
+ == Custom inputs
307
+
308
+ It is very easy to add custom inputs to SimpleForm. For instance, if you want to add a custom input that extends the string one, you just need to add this file:
309
+
310
+ # app/inputs/currency_input.rb
311
+ class CurrencyInput < SimpleForm::Inputs::StringInput
312
+ def input
313
+ "$ #{super}".html_safe
314
+ end
315
+ end
316
+
317
+ And use it in your views:
318
+
319
+ f.input :money, :as => :currency
320
+
321
+ You can also redefine existing SimpleForm inputs by creating a new class with the same name. For instance, if you want to wrap date/time/datetime in a div, you can do:
322
+
323
+ # app/inputs/date_time_input.rb
324
+ class DateTimeInput < SimpleForm::Inputs::DateTimeInput
325
+ def input
326
+ "<div>#{super}</div>".html_safe
327
+ end
328
+ end
329
+
330
+ == Custom form builder
331
+
332
+ You can create a custom form builder that uses SimpleForm.
333
+
334
+ Create a helper method that calls simple_form_for with a custom builder:
335
+
336
+ def custom_form_for(object, *args, &block)
337
+ options = args.extract_options!
338
+ simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
339
+ end
340
+
341
+ Create a form builder class that inherits from SimpleForm::FormBuilder.
342
+
343
+ class CustomFormBuilder < SimpleForm::FormBuilder
344
+ def input(attribute_name, options = {}, &block)
345
+ options[:input_html].merge! :class => 'custom'
346
+ super
347
+ end
348
+ end
349
+
305
350
  == I18n
306
351
 
307
352
  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:
@@ -364,21 +409,37 @@ It's also possible to translate buttons, using Rails' built-in I18n support:
364
409
 
365
410
  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".
366
411
 
412
+ == HTML 5 Notice
413
+
414
+ By default, SimpleForm will generate input field types and attributes that are supported in HTML5, but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5 extensions include the new field types such as email, number, search, url, tel, and the new attributes such as required, autofocus, maxlength, min, max, step.
415
+
416
+ Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the required attribute to force a value into an input and will prevent form submission without it. Depending on the design of the application this may or may not be desired. In many cases it can break existing UI's.
417
+
418
+ It is possible to disable all HTML 5 extensions in SimpleForm with the following configuration:
419
+
420
+ SimpleForm.html5 = false # default is true
421
+
422
+ If you want to have all other HTML 5 features, such as the new field types, you can disable only the browser validation:
423
+
424
+ SimpleForm.browser_validations = false # default is true
425
+
426
+ This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5 validation. The inputs will still be generated with the required and other attributes, that might help you to use some generic javascript validation.
427
+
428
+ Please notice that any of the configurations above will disable the `placeholder` component, which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine, and if they aren't, any plugin you use would take of using the placeholder attribute to do it. However, you can disable it if you want, by removing the placeholder component from the components list in SimpleForm configuration file.
429
+
367
430
  == Configuration
368
431
 
369
432
  SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
370
433
 
371
434
  rails generate simple_form:install
372
435
 
373
- == TODO
374
-
375
- Please refer to TODO file.
376
-
377
436
  == Maintainers
378
437
 
379
- * José Valim (http://github.com/josevalim)
380
- * Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
438
+ * José Valim (https://github.com/josevalim)
439
+ * Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
440
+ * Rafael Mendonça França (https://github.com/rafaelfranca)
381
441
 
382
442
  == License
383
443
 
384
- MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
444
+ MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
445
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake/testtask'
7
+ require 'rake/rdoctask'
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => :test
11
+
12
+ desc 'Test the simple_form plugin.'
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'lib'
15
+ t.libs << 'test'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ end
19
+
20
+ desc 'Generate documentation for the simple_form plugin.'
21
+ Rake::RDocTask.new(:rdoc) do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'SimpleForm'
24
+ rdoc.options << '--line-numbers' << '--inline-source'
25
+ rdoc.rdoc_files.include('README.rdoc')
26
+ rdoc.rdoc_files.include('lib/**/*.rb')
27
+ end
@@ -1,4 +1,4 @@
1
- <%%= simple_form_for(@<%= singular_name %>) do |f| %>
1
+ <%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
2
2
  <%%= f.error_notification %>
3
3
 
4
4
  <div class="inputs">
@@ -1,13 +1,5 @@
1
- = simple_form_for(@<%= singular_name %>) do |f|
2
- - if @<%= singular_name %>.errors.any?
3
- #error_explanation
4
- %h2
5
- = pluralize(@<%= singular_name %>.errors.count, "error")
6
- prohibited this <%= singular_name %> from being saved:
7
-
8
- %ul
9
- - @<%= singular_name %>.errors.full_messages.each do |msg|
10
- %li= msg
1
+ = simple_form_for(@<%= singular_table_name %>) do |f|
2
+ = f.error_notification
11
3
 
12
4
  .inputs
13
5
  <%- attributes.each do |attribute| -%>
@@ -0,0 +1,10 @@
1
+ = simple_form_for(@<%= singular_table_name %>) do |f|
2
+ = f.error_notification
3
+
4
+ .inputs
5
+ | <%- attributes.each do |attribute| -%>
6
+ = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
7
+ | <%- end -%>
8
+
9
+ .actions
10
+ = f.button :submit
@@ -10,7 +10,7 @@ SimpleForm.setup do |config|
10
10
  # CSS class to add to all hint tags.
11
11
  # config.hint_class = :hint
12
12
 
13
- # Default tag used on errors.
13
+ # CSS class used on errors.
14
14
  # config.error_class = :error
15
15
 
16
16
  # Default tag used on errors.
@@ -40,8 +40,8 @@ SimpleForm.setup do |config|
40
40
  # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
41
41
  # config.collection_wrapper_tag = nil
42
42
 
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
43
+ # You can wrap each item in a collection of radio/check boxes with a tag, defaulting to span.
44
+ # config.item_wrapper_tag = :span
45
45
 
46
46
  # Series of attemps to detect a default label method for collection.
47
47
  # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
@@ -52,16 +52,32 @@ SimpleForm.setup do |config|
52
52
  # How the label text should be generated altogether with the required text.
53
53
  # config.label_text = lambda { |label, required| "#{required} #{label}" }
54
54
 
55
+ # You can define the class to use on all labels. Default is nil.
56
+ # config.label_class = nil
57
+
58
+ # You can define the class to use on all forms. Default is simple_form.
59
+ # config.form_class = :simple_form
60
+
55
61
  # Whether attributes are required by default (or not). Default is true.
56
62
  # config.required_by_default = true
57
63
 
64
+ # Tell browsers whether to use default HTML5 validations (novalidate option).
65
+ # Default is enabled.
66
+ # config.browser_validations = true
67
+
68
+ # Determines whether HTML5 types (:email, :url, :search, :tel) and attributes
69
+ # (e.g. required) are used or not. True by default.
70
+ # Having this on in non-HTML5 compliant sites can cause odd behavior in
71
+ # HTML5-aware browsers such as Chrome.
72
+ # config.html5 = true
73
+
58
74
  # Custom mappings for input types. This should be a hash containing a regexp
59
75
  # to match as key, and the input type that will be used when the field name
60
76
  # matches the regexp as value.
61
77
  # config.input_mappings = { /count/ => :integer }
62
78
 
63
79
  # Collection of methods to detect if a file type was given.
64
- # config.file_methods = [ :file?, :public_filename ]
80
+ # config.file_methods = [ :mounted_as, :file?, :public_filename ]
65
81
 
66
82
  # Default priority for time_zone inputs.
67
83
  # config.time_zone_priority = nil
@@ -7,7 +7,9 @@ module SimpleForm
7
7
  # Create a collection of radio inputs for the attribute. Basically this
8
8
  # helper will create a radio input associated with a label for each
9
9
  # text/value option in the collection, using value_method and text_method
10
- # to convert these text/value. Based on collection_select.
10
+ # to convert these text/value. You can give a symbol or a proc to both
11
+ # value_method and text_method, that will be evaluated for each item in
12
+ # the collection.
11
13
  #
12
14
  # == Examples
13
15
  #
@@ -37,14 +39,16 @@ module SimpleForm
37
39
  render_collection(
38
40
  attribute, collection, value_method, text_method, options, html_options
39
41
  ) 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
+ radio_button(attribute, value, default_html_options) +
43
+ label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio")
42
44
  end
43
45
  end
44
46
 
45
- # Creates a collection of check boxes for each item in the collection, associated
46
- # with a clickable label. Use value_method and text_method to convert items in
47
- # the collection for use as text/value in check boxes.
47
+ # Creates a collection of check boxes for each item in the collection,
48
+ # associated with a clickable label. Use value_method and text_method to
49
+ # convert items in the collection for use as text/value in check boxes.
50
+ # You can give a symbol or a proc to both value_method and text_method,
51
+ # that will be evaluated for each item in the collection.
48
52
  #
49
53
  # == Examples
50
54
  #
@@ -79,8 +83,8 @@ module SimpleForm
79
83
  ) do |value, text, default_html_options|
80
84
  default_html_options[:multiple] = true
81
85
 
82
- check_box = check_box(attribute, default_html_options, value, '')
83
- collection_label(attribute, value, check_box, text, :class => "collection_check_boxes")
86
+ check_box(attribute, default_html_options, value, '') +
87
+ label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes")
84
88
  end
85
89
  end
86
90
 
@@ -95,17 +99,16 @@ module SimpleForm
95
99
  # end
96
100
  def simple_fields_for(*args, &block)
97
101
  options = args.extract_options!
98
- options[:builder] = SimpleForm::FormBuilder
102
+ if self.class < ActionView::Helpers::FormBuilder
103
+ options[:builder] ||= self.class
104
+ else
105
+ options[:builder] ||= SimpleForm::FormBuilder
106
+ end
99
107
  fields_for(*(args << options), &block)
100
108
  end
101
109
 
102
110
  private
103
111
 
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
-
109
112
  # Generate default options for collection helpers, such as :checked and
110
113
  # :disabled.
111
114
  def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
@@ -154,4 +157,25 @@ module SimpleForm
154
157
  end
155
158
  end
156
159
 
157
- ActionView::Helpers::FormBuilder.send :include, SimpleForm::ActionViewExtensions::Builder
160
+ class ActionView::Helpers::FormBuilder
161
+ include SimpleForm::ActionViewExtensions::Builder
162
+
163
+ # Override default Rails collection_select helper to handle lambdas/procs in
164
+ # text and value methods, so it works the same way as collection_radio and
165
+ # collection_check_boxes in SimpleForm. If none of text/value methods is a
166
+ # callable object, then it just delegates back to original collection select.
167
+ #
168
+ alias :original_collection_select :collection_select
169
+ def collection_select(attribute, collection, value_method, text_method, options={}, html_options={})
170
+ if value_method.respond_to?(:call) || text_method.respond_to?(:call)
171
+ collection = collection.map do |item|
172
+ value = value_for_collection(item, value_method)
173
+ text = value_for_collection(item, text_method)
174
+ [value, text]
175
+ end
176
+ value_method, text_method = :first, :last
177
+ end
178
+
179
+ original_collection_select(attribute, collection, value_method, text_method, options, html_options)
180
+ end
181
+ end
@@ -1,7 +1,6 @@
1
1
  module SimpleForm
2
2
  module ActionViewExtensions
3
- # This modules create simple form wrappers around default form_for,
4
- # fields_for and remote_form_for.
3
+ # This module creates simple form wrappers around default form_for and fields_for.
5
4
  #
6
5
  # Example:
7
6
  #
@@ -30,18 +29,19 @@ module SimpleForm
30
29
  result
31
30
  end
32
31
 
33
- [:form_for, :fields_for, :remote_form_for].each do |helper|
32
+ [:form_for, :fields_for].each do |helper|
34
33
  class_eval <<-METHOD, __FILE__, __LINE__
35
34
  def simple_#{helper}(record_or_name_or_array, *args, &block)
36
35
  options = args.extract_options!
37
- options[:builder] = SimpleForm::FormBuilder
36
+ options[:builder] ||= SimpleForm::FormBuilder
38
37
  css_class = case record_or_name_or_array
39
38
  when String, Symbol then record_or_name_or_array.to_s
40
39
  when Array then dom_class(record_or_name_or_array.last)
41
40
  else dom_class(record_or_name_or_array)
42
41
  end
43
42
  options[:html] ||= {}
44
- options[:html][:class] = "simple_form \#{css_class} \#{options[:html][:class]}".strip
43
+ options[:html][:novalidate] = !SimpleForm.browser_validations
44
+ options[:html][:class] = "\#{SimpleForm.form_class} \#{css_class} \#{options[:html][:class]}".strip
45
45
 
46
46
  with_custom_field_error_proc do
47
47
  #{helper}(record_or_name_or_array, *(args << options), &block)
@@ -12,7 +12,11 @@ module SimpleForm
12
12
  end
13
13
 
14
14
  def error_text
15
- errors.send(error_method)
15
+ if options[:error_prefix]
16
+ options[:error_prefix] + " " + errors.send(error_method)
17
+ else
18
+ errors.send(error_method)
19
+ end
16
20
  end
17
21
 
18
22
  def error_method
@@ -36,8 +36,8 @@ module SimpleForm
36
36
  end
37
37
 
38
38
  def label_html_options
39
- label_options = html_options_for(:label, [input_type, required_class])
40
- label_options[:for] = options[:input_html][:id] if options.key?(:input_html)
39
+ label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class])
40
+ label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id)
41
41
  label_options
42
42
  end
43
43