simple_form 3.0.2 → 3.1.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 (86) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -34
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +204 -81
  5. data/lib/generators/simple_form/install_generator.rb +3 -3
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +28 -7
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +115 -24
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +86 -5
  10. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  11. data/lib/simple_form/action_view_extensions/builder.rb +1 -0
  12. data/lib/simple_form/action_view_extensions/form_helper.rb +6 -2
  13. data/lib/simple_form/components/errors.rb +28 -2
  14. data/lib/simple_form/components/hints.rb +2 -2
  15. data/lib/simple_form/components/html5.rb +1 -1
  16. data/lib/simple_form/components/label_input.rb +20 -2
  17. data/lib/simple_form/components/labels.rb +10 -6
  18. data/lib/simple_form/components/maxlength.rb +1 -1
  19. data/lib/simple_form/components/min_max.rb +1 -1
  20. data/lib/simple_form/components/pattern.rb +1 -1
  21. data/lib/simple_form/components/placeholders.rb +2 -2
  22. data/lib/simple_form/components/readonly.rb +1 -1
  23. data/lib/simple_form/form_builder.rb +123 -73
  24. data/lib/simple_form/helpers.rb +5 -5
  25. data/lib/simple_form/inputs/base.rb +33 -11
  26. data/lib/simple_form/inputs/block_input.rb +1 -1
  27. data/lib/simple_form/inputs/boolean_input.rb +28 -15
  28. data/lib/simple_form/inputs/collection_input.rb +30 -9
  29. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +6 -11
  30. data/lib/simple_form/inputs/collection_select_input.rb +4 -2
  31. data/lib/simple_form/inputs/date_time_input.rb +12 -2
  32. data/lib/simple_form/inputs/file_input.rb +4 -2
  33. data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
  34. data/lib/simple_form/inputs/hidden_input.rb +4 -2
  35. data/lib/simple_form/inputs/numeric_input.rb +5 -4
  36. data/lib/simple_form/inputs/password_input.rb +4 -2
  37. data/lib/simple_form/inputs/priority_input.rb +4 -2
  38. data/lib/simple_form/inputs/range_input.rb +1 -1
  39. data/lib/simple_form/inputs/string_input.rb +4 -2
  40. data/lib/simple_form/inputs/text_input.rb +4 -2
  41. data/lib/simple_form/railtie.rb +7 -0
  42. data/lib/simple_form/tags.rb +6 -0
  43. data/lib/simple_form/version.rb +1 -1
  44. data/lib/simple_form/wrappers/builder.rb +6 -6
  45. data/lib/simple_form/wrappers/leaf.rb +28 -0
  46. data/lib/simple_form/wrappers/many.rb +6 -6
  47. data/lib/simple_form/wrappers/root.rb +1 -1
  48. data/lib/simple_form/wrappers/single.rb +5 -3
  49. data/lib/simple_form/wrappers.rb +1 -0
  50. data/lib/simple_form.rb +59 -8
  51. data/test/action_view_extensions/builder_test.rb +36 -36
  52. data/test/action_view_extensions/form_helper_test.rb +33 -14
  53. data/test/components/label_test.rb +37 -37
  54. data/test/form_builder/association_test.rb +52 -35
  55. data/test/form_builder/button_test.rb +10 -10
  56. data/test/form_builder/error_notification_test.rb +1 -1
  57. data/test/form_builder/error_test.rb +144 -24
  58. data/test/form_builder/general_test.rb +89 -64
  59. data/test/form_builder/hint_test.rb +18 -18
  60. data/test/form_builder/input_field_test.rb +80 -16
  61. data/test/form_builder/label_test.rb +45 -13
  62. data/test/form_builder/wrapper_test.rb +135 -19
  63. data/test/generators/simple_form_generator_test.rb +4 -4
  64. data/test/inputs/boolean_input_test.rb +62 -6
  65. data/test/inputs/collection_check_boxes_input_test.rb +85 -17
  66. data/test/inputs/collection_radio_buttons_input_test.rb +134 -36
  67. data/test/inputs/collection_select_input_test.rb +146 -41
  68. data/test/inputs/datetime_input_test.rb +117 -50
  69. data/test/inputs/disabled_test.rb +15 -15
  70. data/test/inputs/discovery_test.rb +56 -6
  71. data/test/inputs/file_input_test.rb +2 -2
  72. data/test/inputs/general_test.rb +20 -20
  73. data/test/inputs/grouped_collection_select_input_test.rb +44 -8
  74. data/test/inputs/hidden_input_test.rb +4 -4
  75. data/test/inputs/numeric_input_test.rb +43 -43
  76. data/test/inputs/priority_input_test.rb +13 -13
  77. data/test/inputs/readonly_test.rb +19 -19
  78. data/test/inputs/required_test.rb +13 -13
  79. data/test/inputs/string_input_test.rb +50 -30
  80. data/test/inputs/text_input_test.rb +7 -7
  81. data/test/simple_form_test.rb +8 -0
  82. data/test/support/discovery_inputs.rb +32 -2
  83. data/test/support/misc_helpers.rb +77 -5
  84. data/test/support/models.rb +60 -24
  85. data/test/test_helper.rb +5 -1
  86. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0b1424f33f30519ecda1b748031fdbd3a3a5ac8
4
- data.tar.gz: aebd1dbf4c0537ed49040cd5641a892f116b2bc8
3
+ metadata.gz: 275599af691b7ba0bd40146940b8936b7b00bb65
4
+ data.tar.gz: bf12768bc9960272a7823e62276f8191a5b293d7
5
5
  SHA512:
6
- metadata.gz: 94c159b4f89974808ddd180935003b32f9023beb9e32878a5c0fdfbd82ef88f3d14f4efc5d748a13f8ce05b3490f05c7ce36002cd9b343486929b214281f811b
7
- data.tar.gz: f003707ac3883c392678b6423cb7912668e18b37473a2c4425cfd2a3e9f4f94e0173152f5bc00aa5aade4dad17b4975960b171e49e157c68a1c4a649307aa8f0
6
+ metadata.gz: f62bec40ad89719416a5010cd0ec3eab18a6322289cd817e732061d4b53dd01e6f749a3a1a89caabe2fa233749d9841dea374365572fa91f8dc70755382d6811
7
+ data.tar.gz: 82930fed0d675a74d7e226ebf39e78ebf86807ba583988959be4f633c50f76e3cbc2e2b924d243ce87e5423089610962dab3f1795eb242ce544d985d702c8f36
data/CHANGELOG.md CHANGED
@@ -1,38 +1,49 @@
1
- ## 3.0.2
1
+ ## 3.1.0
2
2
 
3
3
  ### enhancements
4
-
5
- * Support Rails 4.1
6
-
7
- ## 3.0.1
8
-
9
- ### bug fix
10
- * Fix XSS vulnerability on label, hint and error components.
11
-
12
- ## 3.0.0
13
-
14
- ### enhancements
15
- * New `input_class` global config option to set a class to be generated in all inputs.
16
- * Collection tags accept html attributes as the last element of collection [@nashby](https://github.com/nashby)
17
- * Change default `:value_method` of collection tags from `:last` to `:second` [@nashby](https://github.com/nashby)
18
- * Support `Proc` object in `:conditions` option of associations [@bradly](https://github.com/bradly)
19
- * `input_field` supports `html5` component [@nashby](https://github.com/nashby)
20
- * Make `field_error_proc` configurable [@dfens](https://github.com/dfens)
21
- * Support to Rails 4.
22
- * Removed deprecated methods.
23
- * SimpleForm no longer sets the `size` attribute automatically and the `default_input_size` setting
24
- is now deprecated.
25
- * Support to aria-required attribute to required fields [@ckundo](https://github.com/ckundo)
4
+ * Update foundation generator to version 5. [@jorge-d](https://github.com/jorge-d)
5
+ * Add mapping to `uuid` columns.
6
+ * Add custom namespaces for custom inputs feature. [@vala](https://github.com/vala)
7
+ * Add `:unless_blank` option to the wrapper API. [@IanVaughan](https://github.com/IanVaughan)
8
+ * Add support to html markup in the I18n options. [@laurocaetano](https://github.com/laurocaetano)
9
+ * Add the `full_error` component. [@laurocaetano](https://github.com/laurocaetano)
10
+ * Add support to `scope` to be used on associations. [@laurocaetano](https://github.com/laurocaetano)
11
+ * Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano)
12
+ * Check if the given association responds to `order` before calling it. [@laurocaetano](https://github.com/laurocaetano)
13
+ * Add Bootstrap 3 initializer template.
14
+ * For radio or checkbox collection always use `:item_wrapper_tag` to wrap the content and add `label` when using `boolean_style` with `:nested` [@kassio](https://github.com/kassio) and [@erichkist](https://github.com/erichkist)
15
+ * `input_field` uses the same wrapper as input but only with attribute components. [@nashby](https://github.com/nashby)
16
+ * Add wrapper mapping per form basis [@rcillo](https://github.com/rcillo) and [@bernardoamc](https://github.com/bernardoamc)
17
+ * Add `for` attribute to `label` when collections are rendered as radio or checkbox [@erichkist](https://github.com/erichkist), [@ulissesalmeida](https://github.com/ulissesalmeida) and [@fabioyamate](https://github.com/fabioyamate)
18
+ * Add `include_default_input_wrapper_class` config [@luizcosta](https://github.com/luizcosta)
19
+ * Map `datetime`, `date` and `time` input types to their respective HTML5 input tags
20
+ when the `:html5` is set to `true` [@volmer](https://github.com/volmer)
21
+ * Add `boolean_label_class` config.
22
+ * Add `:html` option to include additional attributes on custom wrappers [@remofritzsche](https://github.com/remofritzsche) and [@ulissesalmeida](https://github.com/ulissesalmeida)
23
+ * Make possible to use the Wrappers API to define attributes for the components.
24
+ See https://github.com/plataformatec/simple_form/pull/997 for more information.
25
+ * Put a whitespace before the `inline_label` options of boolean input if it is present.
26
+ * Add support to configure the `label_text` proc at the wrapper level. [@NOX73](https://github.com/NOX73)
27
+ * `label_text` proc now receive three arguments (label, request, and if the label was explicit). [@timscott](https://github.com/timscott)
28
+ * Add I18n support to `:include_blank` and `:prompt` when `:translate` is used as value. [@haines](https://github.com/plataformatec/simple_form/pull/616)
29
+ * Add support to define custom error messages for the attributes.
30
+ * Add support to change the I18n scope to be used in Simple Form. [@nielsbuus](https://github.com/nielsbuus)
31
+ * The default form class can now be overridden with `html: { :class }`. [@rmm5t](https://github.com/rmm5t)
26
32
 
27
33
  ### bug fix
28
- * Make `DateTimeInput#label_target` method to work with string values in `I18n.t('date.order')` (default
29
- behaviour in Rails 4)
30
- Closes [#846](https://github.com/plataformatec/simple_form/issues/846) [@mjankowski](https://github.com/mjankowski)
31
- * Add "checkbox" class to the label of boolean input when there is no `:label`
32
- in `generate_additional_classes_for` config option [@nashby](https://github.com/nashby)
33
- * Support models with digits in their names [@webgago](https://github.com/webgago)
34
- * Remove deprecation warnings related to `Relation#all` from Rails 4.
35
- * Form builder can be used outside the context of a controller [@jasonwebster](https://github.com/jasonwebster)
36
- * Skip pattern attribute when using `validates_format_of` with `:without` option [@glebm](https://github.com/glebm)
37
-
38
- Please check [v2.1](https://github.com/plataformatec/simple_form/blob/v2.1/CHANGELOG.md) for previous changes.
34
+ * Fix `full_error` when the attribute is an association. [@mvdamme](https://github.com/jorge-d)
35
+ * Fix suppport to `:namespace` and `:index` options for nested check boxes and radio buttons when the attribute is an association.
36
+ * Collection input that uses automatic collection translation properly sets checked values.
37
+ Closes [#971](https://github.com/plataformatec/simple_form/issues/971) [@nashby](https://github.com/nashby)
38
+ * Collection input generates `required` attribute if it has `prompt` option. [@nashby](https://github.com/nashby)
39
+ * Grouped collection uses the first non-empty object to detect label and value methods.
40
+
41
+ ## deprecation
42
+ * Methods on custom inputs now accept a required argument with the wrapper options.
43
+ See https://github.com/plataformatec/simple_form/pull/997 for more information.
44
+ * SimpleForm.form_class is deprecated in favor of SimpleForm.default_form_class.
45
+ Future versions of Simple Form will not generate `simple_form` class for the form
46
+ element.
47
+ See https://github.com/plataformatec/simple_form/pull/1109 for more information.
48
+
49
+ Please check [v3.0](https://github.com/plataformatec/simple_form/blob/v3.0/CHANGELOG.md) for previous changes.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2013 Plataformatec http://plataformatec.com.br/
1
+ Copyright (c) 2009-2014 Plataformatec http://plataformatec.com.br/
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -2,10 +2,6 @@
2
2
 
3
3
  By [Plataformatec](http://plataformatec.com.br/).
4
4
 
5
- [![Gem Version](https://fury-badge.herokuapp.com/rb/simple_form.png)](http://badge.fury.io/rb/simple_form)
6
- [![Build Status](https://api.travis-ci.org/plataformatec/simple_form.png?branch=master)](http://travis-ci.org/plataformatec/simple_form)
7
- [![Code Climate](https://codeclimate.com/github/plataformatec/simple_form.png)](https://codeclimate.com/github/plataformatec/simple_form)
8
-
9
5
  Rails forms made easy.
10
6
 
11
7
  **Simple Form** aims to be as flexible as possible while helping you with powerful components to create
@@ -14,7 +10,7 @@ you find the better design for your eyes. Most of the DSL was inherited from For
14
10
  which we are thankful for and should make you feel right at home.
15
11
 
16
12
  INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
17
- and refers to **Simple Form** 3.0. For older releases, check the related branch for your version.
13
+ and refers to **Simple Form** 3.1. For older releases, check the related branch for your version.
18
14
 
19
15
  ## Installation
20
16
 
@@ -43,27 +39,25 @@ Also, if you want to use the country select, you will need the
43
39
  gem 'country_select'
44
40
  ```
45
41
 
46
- ### Twitter Bootstrap
42
+ ### Bootstrap
47
43
 
48
- **Simple Form** can be easily integrated to the [Twitter Bootstrap](http://twitter.github.com/bootstrap).
44
+ **Simple Form** can be easily integrated to the [Bootstrap](http://getbootstrap.com/).
49
45
  To do that you have to use the `bootstrap` option in the install generator, like this:
50
46
 
51
47
  ```console
52
48
  rails generate simple_form:install --bootstrap
53
49
  ```
54
50
 
55
- You have to be sure that you added a copy of the [Twitter Bootstrap](http://twitter.github.com/bootstrap)
51
+ You have to be sure that you added a copy of the [Bootstrap](http://getbootstrap.com/)
56
52
  assets on your application.
57
53
 
58
54
  For more information see the generator output, our
59
55
  [example application code](https://github.com/rafaelfranca/simple_form-bootstrap) and
60
56
  [the live example app](http://simple-form-bootstrap.plataformatec.com.br/).
61
57
 
62
- **NOTE**: **Simple Form** integration requires Twitter Bootstrap version 2.0 or higher.
58
+ ### Zurb Foundation 5
63
59
 
64
- ### Zurb Foundation 3
65
-
66
- To generate wrappers that are compatible with [Zurb Foundation 3](http://foundation.zurb.com/), pass
60
+ To generate wrappers that are compatible with [Zurb Foundation 5](http://foundation.zurb.com/), pass
67
61
  the `foundation` option to the generator, like this:
68
62
 
69
63
  ```console
@@ -74,14 +68,14 @@ Please note that the Foundation wrapper does not support the `:hint` option by d
74
68
  enable hints, please uncomment the appropriate line in `config/initializers/simple_form_foundation.rb`.
75
69
  You will need to provide your own CSS styles for hints.
76
70
 
77
- Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/old-docs/f3/rails.php).
71
+ Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/docs/applications.html).
78
72
 
79
73
  ## Usage
80
74
 
81
75
  **Simple Form** was designed to be customized as you need to. Basically it's a stack of components that
82
76
  are invoked to create a complete html input for you, which by default contains label, hints, errors
83
77
  and the input itself. It does not aim to create a lot of different logic from the default Rails
84
- form helpers, as they do a great work by themselves. Instead, **Simple Form** acts as a DSL and just
78
+ form helpers, as they do a great job by themselves. Instead, **Simple Form** acts as a DSL and just
85
79
  maps your input type (retrieved from the column definition in the database) to a specific helper method.
86
80
 
87
81
  To start using **Simple Form** you just have to use the helper it provides:
@@ -176,6 +170,13 @@ And of course, the `required` property of any input can be overwritten as needed
176
170
  <% end %>
177
171
  ```
178
172
 
173
+ By default, **Simple Form** will look at the column type in the database and use an
174
+ appropriate input for the column. For example, a column created with type
175
+ `:text` in the database will use a `textarea` input by default. See the section
176
+ [Available input types and defaults for each column
177
+ type](https://github.com/plataformatec/simple_form#available-input-types-and-defaults-for-each-column-type)
178
+ for a complete list of defaults.
179
+
179
180
  **Simple Form** also lets you overwrite the default input type it creates:
180
181
 
181
182
  ```erb
@@ -189,11 +190,11 @@ And of course, the `required` property of any input can be overwritten as needed
189
190
  ```
190
191
 
191
192
  So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
192
- labels and a text area instead of a text field for the description. You can also render boolean
193
+ labels and a textarea instead of a text field for the description. You can also render boolean
193
194
  attributes using `as: :select` to show a dropdown.
194
195
 
195
196
  It is also possible to give the `:disabled` option to **Simple Form**, and it'll automatically mark
196
- the wrapper as disabled with a css class, so you can style labels, hints and other components inside
197
+ the wrapper as disabled with a CSS class, so you can style labels, hints and other components inside
197
198
  the wrapper as well:
198
199
 
199
200
  ```erb
@@ -210,6 +211,7 @@ the wrapper as well:
210
211
  <%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
211
212
  end_year: Date.today.year - 12, discard_day: true,
212
213
  order: [:month, :year] %>
214
+ <%= f.input :accepts, as: :boolean, checked_value: true, unchecked_value: false %>
213
215
  <%= f.button :submit %>
214
216
  <% end %>
215
217
  ```
@@ -241,9 +243,42 @@ Example:
241
243
  ```ruby
242
244
  simple_form_for @user do |f|
243
245
  f.input_field :name
246
+ f.input_field :remember_me, as: :boolean
244
247
  end
245
248
  ```
246
249
 
250
+ ```html
251
+ <form>
252
+ ...
253
+ <input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
254
+ <input name="user[remember_me]" type="hidden" value="0">
255
+ <label class="checkbox">
256
+ <input class="boolean optional" id="user_published" name="user[remember_me]" type="checkbox" value="1">
257
+ </label>
258
+ </form>
259
+ ```
260
+
261
+ For check boxes and radio buttons you can remove the label changing `boolean_style` from default value `:nested` to `:inline`.
262
+ Also, `item_wrapper_tag` will not work when `boolean_style` is set to `:nested`.
263
+
264
+ Example:
265
+
266
+ ```ruby
267
+ simple_form_for @user do |f|
268
+ f.input_field :name
269
+ f.input_field :remember_me, as: :boolean, boolean_style: :inline
270
+ end
271
+ ```
272
+
273
+ ```html
274
+ <form>
275
+ ...
276
+ <input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
277
+ <input name="user[remember_me]" type="hidden" value="0">
278
+ <input class="boolean optional" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
279
+ </form>
280
+ ```
281
+
247
282
  Produces:
248
283
 
249
284
  ```html
@@ -269,14 +304,14 @@ overriding the `:collection` option:
269
304
  Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
270
305
  rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
271
306
  are `:radio_buttons` and `:check_boxes`. Those are added by **Simple Form** to Rails set of form
272
- helpers (read Extra Helpers session below for more information).
307
+ helpers (read Extra Helpers section below for more information).
273
308
 
274
309
  Collection inputs accept two other options beside collections:
275
310
 
276
- * _label_method_ => the label method to be applied to the collection to retrieve the label (use this
311
+ * *label_method* => the label method to be applied to the collection to retrieve the label (use this
277
312
  instead of the `text_method` option in `collection_select`)
278
313
 
279
- * _value_method_ => the value method to be applied to the collection to retrieve the value
314
+ * *value_method* => the value method to be applied to the collection to retrieve the value
280
315
 
281
316
  Those methods are useful to manipulate the given collection. Both of these options also accept
282
317
  lambda/procs in case you want to calculate the value or label in a special way eg. custom
@@ -296,24 +331,24 @@ f.input :country_id, collection: @continents, as: :grouped_select, group_method:
296
331
  Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
297
332
  used to retrieve label/value attributes for the `option` tags. Besides that, you can give:
298
333
 
299
- * _group_method_ => the method to be called on the given collection to generate the options for
334
+ * *group_method* => the method to be called on the given collection to generate the options for
300
335
  each group (required)
301
336
 
302
- * _group_label_method_ => the label method to be applied on the given collection to retrieve the label
337
+ * *group_label_method* => the label method to be applied on the given collection to retrieve the label
303
338
  for the _optgroup_ (**Simple Form** will attempt to guess the best one the same way it does with
304
339
  `:label_method`)
305
340
 
306
341
  ### Priority
307
342
 
308
343
  **Simple Form** also supports `:time_zone` and `:country`. When using such helpers, you can give
309
- `:priority` as option to select which time zones and/or countries should be given higher priority:
344
+ `:priority` as an option to select which time zones and/or countries should be given higher priority:
310
345
 
311
346
  ```ruby
312
347
  f.input :residence_country, priority: [ "Brazil" ]
313
348
  f.input :time_zone, priority: /US/
314
349
  ```
315
350
 
316
- Those values can also be configured with a default value to be used site use through the
351
+ Those values can also be configured with a default value to be used on the site through the
317
352
  `SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
318
353
 
319
354
  Note: While using `country_select` if you want to restrict to only a subset of countries for a specific
@@ -325,8 +360,8 @@ f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "B
325
360
 
326
361
  ### Associations
327
362
 
328
- To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or check boxes.
329
- Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
363
+ To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or checkboxes.
364
+ Lets see how it works: imagine you have a user model that belongs to a company and `has_and_belongs_to_many`
330
365
  roles. The structure would be something like:
331
366
 
332
367
  ```ruby
@@ -357,7 +392,7 @@ Now we have the user form:
357
392
 
358
393
  Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another
359
394
  `:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio
360
- buttons and check boxes as well:
395
+ buttons and checkboxes as well:
361
396
 
362
397
  ```ruby
363
398
  f.association :company, as: :radio_buttons
@@ -394,6 +429,12 @@ All web forms need buttons, right? **Simple Form** wraps them in the DSL, acting
394
429
 
395
430
  The above will simply call submit. You choose to use it or not, it's just a question of taste.
396
431
 
432
+ The button method also accepts optional parameters, that are delegated to the underlying submit call:
433
+
434
+ ```erb
435
+ <%= f.button :submit, "Custom Button Text", class: "my-button" %>
436
+ ```
437
+
397
438
  ### Wrapping Rails Form Helpers
398
439
 
399
440
  Say you wanted to use a rails form helper but still wrap it in **Simple Form** goodness? You can, by
@@ -446,7 +487,7 @@ end
446
487
 
447
488
  #### Collection Check Boxes
448
489
 
449
- Creates a collection of check boxes with labels associated (same API as `collection_select`):
490
+ Creates a collection of checkboxes with labels associated (same API as `collection_select`):
450
491
 
451
492
  ```ruby
452
493
  form_for @user do |f|
@@ -471,36 +512,37 @@ form_for @user do |f|
471
512
  end
472
513
  ```
473
514
 
474
- ## Mappings/Inputs available
475
-
476
- **Simple Form** comes with a lot of default mappings:
477
-
478
- ```text
479
- Mapping Input Column Type
480
-
481
- boolean check box boolean
482
- string text field string
483
- email email field string with name matching "email"
484
- url url field string with name matching "url"
485
- tel tel field string with name matching "phone"
486
- password password field string with name matching "password"
487
- search search -
488
- text text area text
489
- file file field string, responding to file methods
490
- hidden hidden field -
491
- integer number field integer
492
- float number field float
493
- decimal number field decimal
494
- range range field -
495
- datetime datetime select datetime/timestamp
496
- date date select date
497
- time time select time
498
- select collection select belongs_to/has_many/has_and_belongs_to_many associations
499
- radio_buttons collection radio buttons belongs_to
500
- check_boxes collection check boxes has_many/has_and_belongs_to_many associations
501
- country country select string with name matching "country"
502
- time_zone time zone select string with name matching "time_zone"
503
- ```
515
+ ## Available input types and defaults for each column type
516
+
517
+ The following table shows the html element you will get for each attribute
518
+ according to its database definition. These defaults can be changed by
519
+ specifying the helper method in the column `Mapping` as the `as:` option.
520
+
521
+ Mapping | Generated HTML Element | Database Column Type
522
+ --------------- |:-------------------------------------|:--------------------
523
+ `boolean` | `input[type=checkbox]` | `boolean`
524
+ `string` | `input[type=text]` | `string`
525
+ `email` | `input[type=email]` | `string` with `name =~ /email/`
526
+ `url` | `input[type=url]` | `string` with `name =~ /url/`
527
+ `tel` | `input[type=tel]` | `string` with `name =~ /phone/`
528
+ `password` | `input[type=password]` | `string` with `name =~ /password/`
529
+ `search` | `input[type=search]` | -
530
+ `uuid` | `input[type=text]` | `uuid`
531
+ `text` | `textarea` | `text`
532
+ `file` | `input[type=file]` | `string` responding to file methods
533
+ `hidden` | `input[type=hidden]` | -
534
+ `integer` | `input[type=number]` | `integer`
535
+ `float` | `input[type=number]` | `float`
536
+ `decimal` | `input[type=number]` | `decimal`
537
+ `range` | `input[type=range]` | -
538
+ `datetime` | `datetime select` | `datetime/timestamp`
539
+ `date` | `date select` | `date`
540
+ `time` | `time select` | `time`
541
+ `select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
542
+ `radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
543
+ `check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
544
+ `country` | `select` (countries as options) | `string` with `name =~ /country/`
545
+ `time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
504
546
 
505
547
  ## Custom inputs
506
548
 
@@ -510,8 +552,10 @@ that extends the string one, you just need to add this file:
510
552
  ```ruby
511
553
  # app/inputs/currency_input.rb
512
554
  class CurrencyInput < SimpleForm::Inputs::Base
513
- def input
514
- "$ #{@builder.text_field(attribute_name, input_html_options)}".html_safe
555
+ def input(wrapper_options)
556
+ merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
557
+
558
+ "$ #{@builder.text_field(attribute_name, merged_input_options)}".html_safe
515
559
  end
516
560
  end
517
561
  ```
@@ -521,6 +565,7 @@ And use it in your views:
521
565
  ```ruby
522
566
  f.input :money, as: :currency
523
567
  ```
568
+ Note, you may have to create the `app/inputs/` directory and restart your webserver.
524
569
 
525
570
  You can also redefine existing **Simple Form** inputs by creating a new class with the same name. For
526
571
  instance, if you want to wrap date/time/datetime in a div, you can do:
@@ -528,7 +573,7 @@ instance, if you want to wrap date/time/datetime in a div, you can do:
528
573
  ```ruby
529
574
  # app/inputs/date_time_input.rb
530
575
  class DateTimeInput < SimpleForm::Inputs::DateTimeInput
531
- def input
576
+ def input(wrapper_options)
532
577
  template.content_tag(:div, super)
533
578
  end
534
579
  end
@@ -545,6 +590,28 @@ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
545
590
  end
546
591
  ```
547
592
 
593
+ If needed, you can namespace your custom inputs in a module and tell **Simple Form** to look for
594
+ their definitions in this module. This can avoid conflicts with other form libraries (like Formtastic) that look up
595
+ the global context to find inputs definition too.
596
+
597
+ ```ruby
598
+ # app/inputs/custom_inputs/numeric_input
599
+ module CustomInputs
600
+ class NumericInput < SimpleForm::Inputs::NumericInput
601
+ def input_html_classes
602
+ super.push('no-spinner')
603
+ end
604
+ end
605
+ end
606
+ ```
607
+
608
+ And in the **SimpleForm** initializer :
609
+
610
+ ```ruby
611
+ # config/simple_form.rb
612
+ config.custom_inputs_namespaces << "CustomInputs"
613
+ ```
614
+
548
615
  ## Custom form builder
549
616
 
550
617
  You can create a custom form builder that uses **Simple Form**.
@@ -563,15 +630,14 @@ Create a form builder class that inherits from `SimpleForm::FormBuilder`.
563
630
  ```ruby
564
631
  class CustomFormBuilder < SimpleForm::FormBuilder
565
632
  def input(attribute_name, options = {}, &block)
566
- options[:input_html].merge! class: 'custom'
567
- super
633
+ super(attribute_name, options.merge(label: false), &block)
568
634
  end
569
635
  end
570
636
  ```
571
637
 
572
638
  ## I18n
573
639
 
574
- **Simple Form** uses all power of I18n API to lookup labels, hints and placeholders. To customize your
640
+ **Simple Form** uses all power of I18n API to lookup labels, hints, prompts and placeholders. To customize your
575
641
  forms you can create a locale file like this:
576
642
 
577
643
  ```yaml
@@ -589,12 +655,18 @@ en:
589
655
  user:
590
656
  username: 'Your username'
591
657
  password: '****'
658
+ include_blanks:
659
+ user:
660
+ age: 'Rather not say'
661
+ prompts:
662
+ user:
663
+ gender: 'Select your gender'
592
664
  ```
593
665
 
594
666
  And your forms will use this information to render the components for you.
595
667
 
596
- **Simple Form** also lets you be more specific, separating lookups through actions for labels, hints and
597
- placeholders. Let's say you want a different label for new and edit actions, the locale file would
668
+ **Simple Form** also lets you be more specific, separating lookups through actions.
669
+ Let's say you want a different label for new and edit actions, the locale file would
598
670
  be something like:
599
671
 
600
672
  ```yaml
@@ -633,13 +705,19 @@ en:
633
705
  ```
634
706
 
635
707
  **Simple Form** will always look for a default attribute translation under the "defaults" key if no
636
- specific is found inside the model key. Note that this syntax is different from 1.x. To migrate to
637
- the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
708
+ specific is found inside the model key.
638
709
 
639
- In addition, **Simple Form** will fallback to default human_attribute_name from Rails when no other
710
+ In addition, **Simple Form** will fallback to default `human_attribute_name` from Rails when no other
640
711
  translation is found for labels. Finally, you can also overwrite any label, hint or placeholder
641
712
  inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
642
713
 
714
+ For `:prompt` and `:include_blank` the I18n lookup is optional and to enable it is necessary to pass
715
+ `:translate` as value.
716
+
717
+ ```ruby
718
+ f.input :gender, prompt: :translate
719
+ ```
720
+
643
721
  **Simple Form** also has support for translating options in collection helpers. For instance, given a
644
722
  User with a `:gender` attribute, you might want to create a select box showing translated labels
645
723
  that would post either `male` or `female` as value. With **Simple Form** you could create an input
@@ -770,13 +848,23 @@ end
770
848
 
771
849
  this will wrap the hint and error components within a `div` tag using the class `'separator'`.
772
850
 
851
+ You can customize _Form components_ passing options to them:
852
+
853
+ ```ruby
854
+ config.wrappers do |b|
855
+ b.use :label_input, class: 'label-input-class'
856
+ end
857
+ ```
858
+
859
+ This you set the input and label class to `'label-input-class'`.
860
+
773
861
  If you want to customize the custom _Form components_ on demand you can give it a name like this:
774
862
 
775
863
  ```ruby
776
864
  config.wrappers do |b|
777
865
  b.use :placeholder
778
866
  b.use :label_input
779
- b.wrapper :my_wrapper, tag: :div, class: 'separator' do |component|
867
+ b.wrapper :my_wrapper, tag: :div, class: 'separator', html: { id: 'my_wrapper_id' } do |component|
780
868
  component.use :hint, wrap_with: { tag: :span, class: :hint }
781
869
  component.use :error, wrap_with: { tag: :span, class: :error }
782
870
  end
@@ -838,6 +926,15 @@ end
838
926
  By setting it as `optional`, a hint will only be generated when `hint: true` is explicitly used.
839
927
  The same for placeholder.
840
928
 
929
+ It is also possible to give the option `:unless_blank` to the wrapper if you want to render it only
930
+ when the content is present.
931
+
932
+ ```ruby
933
+ b.wrapper tag: :span, class: 'hint', unless_blank: true do |component|
934
+ component.optional :hint
935
+ end
936
+ ```
937
+
841
938
  ## HTML 5 Notice
842
939
 
843
940
  By default, **Simple Form** will generate input field types and attributes that are supported in HTML5,
@@ -850,10 +947,25 @@ required attribute to force a value into an input and will prevent form submissi
850
947
  Depending on the design of the application this may or may not be desired. In many cases it can
851
948
  break existing UI's.
852
949
 
853
- It is possible to disable all HTML 5 extensions in **Simple Form** with the following configuration:
950
+ It is possible to disable all HTML 5 extensions in **Simple Form** removing the `html5` component
951
+ from the wrapper used to render the inputs.
952
+
953
+ For example, change:
854
954
 
855
955
  ```ruby
856
- SimpleForm.html5 = false # default is true
956
+ config.wrappers tag: :div do |b|
957
+ b.use :html5
958
+
959
+ b.use :label_input
960
+ end
961
+ ```
962
+
963
+ To:
964
+
965
+ ```ruby
966
+ config.wrappers tag: :div do |b|
967
+ b.use :label_input
968
+ end
857
969
  ```
858
970
 
859
971
  If you want to have all other HTML 5 features, such as the new field types, you can disable only
@@ -870,14 +982,23 @@ help you to use some generic javascript validation.
870
982
  You can also add `novalidate` to a specific form by setting the option on the form itself:
871
983
 
872
984
  ```erb
873
- <%= simple_form_for(resource, html: {novalidate: true}) do |form| %>
985
+ <%= simple_form_for(resource, html: { novalidate: true }) do |form| %>
874
986
  ```
875
987
 
876
- Please notice that any of the configurations above will not disable the `placeholder` component,
877
- which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine,
878
- and if they aren't, any plugin you use would take of using the placeholder attribute to do it.
879
- However, you can disable it if you want, by removing the placeholder component from the components
880
- list in **Simple Form** configuration file.
988
+ Please notice that none of the configurations above will disable the `placeholder` component,
989
+ which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute
990
+ just fine, and if they aren't, any plugin you use would take care of applying the placeholder.
991
+ In any case, you can disable it if you really want to, by removing the placeholder component
992
+ from the components list in the **Simple Form** configuration file.
993
+
994
+ HTML 5 date / time inputs are not generated by **Simple Form** by default, so using `date`,
995
+ `time` or `datetime` will all generate select boxes using normal Rails helpers. We believe
996
+ browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis
997
+ by passing the html5 option:
998
+
999
+ ```erb
1000
+ <%= f.input :expires_at, as: :date, html5: true %>
1001
+ ```
881
1002
 
882
1003
  ## Information
883
1004
 
@@ -894,9 +1015,6 @@ You can view the **Simple Form** documentation in RDoc format here:
894
1015
 
895
1016
  http://rubydoc.info/github/plataformatec/simple_form/master/frames
896
1017
 
897
- If you need to use **Simple Form** with Rails 2.3, you can always run `gem server` from the command line
898
- after you install the gem to access the old documentation.
899
-
900
1018
  ### Bug reports
901
1019
 
902
1020
  If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
@@ -912,9 +1030,14 @@ https://github.com/plataformatec/simple_form/issues
912
1030
  * Rafael Mendonça França (https://github.com/rafaelfranca)
913
1031
  * Vasiliy Ermolovich (https://github.com/nashby)
914
1032
 
1033
+ [![Gem Version](https://fury-badge.herokuapp.com/rb/simple_form.png)](http://badge.fury.io/rb/simple_form)
1034
+ [![Build Status](https://api.travis-ci.org/plataformatec/simple_form.svg?branch=master)](http://travis-ci.org/plataformatec/simple_form)
1035
+ [![Code Climate](https://codeclimate.com/github/plataformatec/simple_form.png)](https://codeclimate.com/github/plataformatec/simple_form)
1036
+ [![Inline docs](http://inch-ci.org/github/plataformatec/simple_form.png)](http://inch-ci.org/github/plataformatec/simple_form)
1037
+
915
1038
  ## License
916
1039
 
917
- MIT License. Copyright 2009-2013 Plataformatec. http://plataformatec.com.br
1040
+ MIT License. Copyright 2009-2014 Plataformatec. http://plataformatec.com.br
918
1041
 
919
1042
  You are not granted rights or licenses to the trademarks of the Plataformatec, including without
920
1043
  limitation the Simple Form name or logo.