simple_form 3.4.0 → 4.1.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.

Files changed (103) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +62 -0
  3. data/README.md +208 -36
  4. data/lib/generators/simple_form/install_generator.rb +1 -0
  5. data/lib/generators/simple_form/templates/README +3 -3
  6. data/lib/generators/simple_form/templates/_form.html.erb +2 -0
  7. data/lib/generators/simple_form/templates/_form.html.haml +2 -0
  8. data/lib/generators/simple_form/templates/_form.html.slim +1 -0
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +15 -5
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +358 -73
  11. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +20 -8
  12. data/lib/simple_form/action_view_extensions/builder.rb +1 -0
  13. data/lib/simple_form/action_view_extensions/form_helper.rb +1 -0
  14. data/lib/simple_form/components/errors.rb +15 -2
  15. data/lib/simple_form/components/hints.rb +1 -0
  16. data/lib/simple_form/components/html5.rb +1 -0
  17. data/lib/simple_form/components/label_input.rb +2 -1
  18. data/lib/simple_form/components/labels.rb +11 -4
  19. data/lib/simple_form/components/maxlength.rb +4 -13
  20. data/lib/simple_form/components/min_max.rb +1 -0
  21. data/lib/simple_form/components/minlength.rb +5 -14
  22. data/lib/simple_form/components/pattern.rb +1 -0
  23. data/lib/simple_form/components/placeholders.rb +2 -1
  24. data/lib/simple_form/components/readonly.rb +1 -0
  25. data/lib/simple_form/components.rb +1 -0
  26. data/lib/simple_form/error_notification.rb +1 -0
  27. data/lib/simple_form/form_builder.rb +81 -26
  28. data/lib/simple_form/helpers/autofocus.rb +1 -0
  29. data/lib/simple_form/helpers/disabled.rb +1 -0
  30. data/lib/simple_form/helpers/readonly.rb +1 -0
  31. data/lib/simple_form/helpers/required.rb +1 -0
  32. data/lib/simple_form/helpers/validators.rb +2 -1
  33. data/lib/simple_form/helpers.rb +1 -0
  34. data/lib/simple_form/i18n_cache.rb +1 -0
  35. data/lib/simple_form/inputs/base.rb +24 -2
  36. data/lib/simple_form/inputs/block_input.rb +1 -0
  37. data/lib/simple_form/inputs/boolean_input.rb +4 -2
  38. data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
  39. data/lib/simple_form/inputs/collection_input.rb +3 -2
  40. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +2 -1
  41. data/lib/simple_form/inputs/collection_select_input.rb +1 -0
  42. data/lib/simple_form/inputs/color_input.rb +14 -0
  43. data/lib/simple_form/inputs/date_time_input.rb +1 -0
  44. data/lib/simple_form/inputs/file_input.rb +1 -0
  45. data/lib/simple_form/inputs/grouped_collection_select_input.rb +1 -0
  46. data/lib/simple_form/inputs/hidden_input.rb +1 -0
  47. data/lib/simple_form/inputs/numeric_input.rb +1 -0
  48. data/lib/simple_form/inputs/password_input.rb +1 -0
  49. data/lib/simple_form/inputs/priority_input.rb +1 -0
  50. data/lib/simple_form/inputs/range_input.rb +1 -0
  51. data/lib/simple_form/inputs/string_input.rb +2 -1
  52. data/lib/simple_form/inputs/text_input.rb +1 -0
  53. data/lib/simple_form/inputs.rb +2 -0
  54. data/lib/simple_form/map_type.rb +1 -0
  55. data/lib/simple_form/railtie.rb +1 -0
  56. data/lib/simple_form/tags.rb +1 -0
  57. data/lib/simple_form/version.rb +2 -1
  58. data/lib/simple_form/wrappers/builder.rb +1 -0
  59. data/lib/simple_form/wrappers/leaf.rb +2 -1
  60. data/lib/simple_form/wrappers/many.rb +1 -0
  61. data/lib/simple_form/wrappers/root.rb +2 -0
  62. data/lib/simple_form/wrappers/single.rb +2 -1
  63. data/lib/simple_form/wrappers.rb +1 -0
  64. data/lib/simple_form.rb +58 -7
  65. data/test/action_view_extensions/builder_test.rb +6 -5
  66. data/test/action_view_extensions/form_helper_test.rb +3 -2
  67. data/test/components/custom_components_test.rb +62 -0
  68. data/test/components/label_test.rb +33 -4
  69. data/test/form_builder/association_test.rb +27 -2
  70. data/test/form_builder/button_test.rb +1 -0
  71. data/test/form_builder/error_notification_test.rb +1 -0
  72. data/test/form_builder/error_test.rb +12 -0
  73. data/test/form_builder/general_test.rb +67 -3
  74. data/test/form_builder/hint_test.rb +6 -0
  75. data/test/form_builder/input_field_test.rb +30 -10
  76. data/test/form_builder/label_test.rb +9 -3
  77. data/test/form_builder/wrapper_test.rb +24 -4
  78. data/test/generators/simple_form_generator_test.rb +4 -3
  79. data/test/inputs/boolean_input_test.rb +17 -0
  80. data/test/inputs/collection_check_boxes_input_test.rb +30 -14
  81. data/test/inputs/collection_radio_buttons_input_test.rb +40 -24
  82. data/test/inputs/collection_select_input_test.rb +40 -39
  83. data/test/inputs/color_input_test.rb +10 -0
  84. data/test/inputs/datetime_input_test.rb +7 -16
  85. data/test/inputs/disabled_test.rb +14 -0
  86. data/test/inputs/discovery_test.rb +22 -0
  87. data/test/inputs/file_input_test.rb +1 -0
  88. data/test/inputs/general_test.rb +3 -2
  89. data/test/inputs/grouped_collection_select_input_test.rb +11 -10
  90. data/test/inputs/hidden_input_test.rb +1 -0
  91. data/test/inputs/numeric_input_test.rb +2 -1
  92. data/test/inputs/priority_input_test.rb +3 -10
  93. data/test/inputs/readonly_test.rb +1 -0
  94. data/test/inputs/required_test.rb +1 -0
  95. data/test/inputs/string_input_test.rb +10 -16
  96. data/test/inputs/text_input_test.rb +1 -0
  97. data/test/simple_form_test.rb +1 -0
  98. data/test/support/discovery_inputs.rb +8 -0
  99. data/test/support/misc_helpers.rb +22 -1
  100. data/test/support/mock_controller.rb +7 -1
  101. data/test/support/models.rb +52 -16
  102. data/test/test_helper.rb +2 -0
  103. metadata +40 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 534c9396c2be271ccbb32518e9e5026b5ff523e1
4
- data.tar.gz: bb8085d4a664110a941f6cd7e732db16f9eef1ec
2
+ SHA256:
3
+ metadata.gz: 1461390ccbd2b434d67a43a84ada68f255d08bc48a9e23d7734075c8f105b400
4
+ data.tar.gz: c81c4b918e6f9e467081e8b534273ef8807ef6e508da40077fe4c91b5b85a66f
5
5
  SHA512:
6
- metadata.gz: d8e81c8af57b65150d3b9a53433c9d9adb3c5d64fa00ae70b1263a33ce05173d2dc65775ab66e91507d1db452e0f59cb254c0b5eb1ca905ec4b95bbc4eb98282
7
- data.tar.gz: 249e0e56f352267ab529db921125ac214d6baeb17153278cba26c61951ce31867cf9d94b4188dab9e50457d2e23d6aebabacf29e475b31c8f7c85c382ab318f2
6
+ metadata.gz: 71d69573ae8125232a436ea4c23115584497647de1a3b5fa996c9c95f78599b3a3e9ff07ce0812cd5370a1e2c0351dc8fdc43bbea30b7f35d6c68213c68a5f84
7
+ data.tar.gz: dacdc01de808196a155ad1219f913bafff072578ad0bb3c0a9602a9f0377d4359f03e54880dd77a3e27307a35754568064d9dd5dcec95abf8a93f5f0164149a8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,67 @@
1
1
  ## Unreleased
2
2
 
3
+ ### Enhancements
4
+ * Guess input type more carefully. [@sringling](https://github.com/sringling)
5
+ * Allow custom error on forms without model. [@victorperez](https://github.com/victorperez)
6
+ * Do not support Ruby < 2.3 anymore. [@gssbzn](https://github.com/gssbzn)
7
+ * Add color inout type. [@gssbzn](https://github.com/gssbzn)
8
+
9
+ ### Bug fix
10
+ * Improve disabled option to input_field. [@betelgeuse](https://github.com/betelgeuse)
11
+ * Memoize `input_html_classes` in `SimpleForm::Inputs::Base`. [@RigoTheDev](https://github.com/RigoTheDev)
12
+ * Fix column type citext HTML5 input type bug. [@brucew](https://github.com/brucew)
13
+ * Use form attribute in the nested boolean hidden field when it is given. [@feliperenan](https://github.com/feliperenan)
14
+
15
+ ## 4.0.1
16
+
17
+ ### Bug fix
18
+ * Do not support Rails 4 anymore. [@rafaelfranca](https://github.com/rafaelfranca)
19
+ * Add missing comma. [@vill](https://github.com/vill)
20
+
21
+ ## 4.0.0
22
+
23
+ ### Enhancements
24
+ * Add bootstrap v4.1 generator template. [@m5o](https://github.com/m5o)
25
+ * Add Rails 5.2 support. [@gobijan](https://github.com/gobijan)
26
+ * Add API to register custom components.[@feliperenan](https://github.com/feliperenan)
27
+ * Allow custom errors classes to inputs.[@feliperenan](https://github.com/feliperenan)
28
+ * Remove support from Rails 4.0, 4.1 and 4.2. [@feliperenan](https://github.com/feliperenan)
29
+ * Add support for citext, hstore, json & jsonb column types. [@swrobel](https://github.com/swrobel)
30
+ * Add :valid_class on input wrapper when value is present and valid [@aeberlin](https://github.com/aeberlin), [@m5o](https://github.com/m5o)
31
+ * Allow :valid_class to inputs when value is present and valid. [@m5o](https://github.com/m5o)
32
+ * Allow validation classes on input_field. [@feliperenan](https://github.com/feliperenan)
33
+ * Add basic ActiveStorage support. [@murb](https://github.com/murb)
34
+
35
+ ### Bug fix
36
+ * Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo)
37
+ * Add base error display alongside existing errors. [@bluefalcon26](https://github.com/bluefalcon26)
38
+ * Silent deprication warning for placeholder_text. [@moofkit](https://github.com/moofkit)
39
+ * Use custom i18n scope for label required html. [@tvdeyen](https://github.com/tvdeyen)
40
+
41
+ ## 3.5.1
42
+
43
+ ### Enhancements
44
+ * Exclude hidden field when unchecked_value: false. [@fschwahn](https://github.com/fschwahn)
45
+ * Add frozen_string_literal magic comment to several files. [@oniofchaos](https://github.com/oniofchaos)
46
+ * Try convert @object to model in case we got decorated object [@timurvafin](https://github.com/timurvafin)
47
+ - From now, if you are using some object that inherits from `SimpleDelegator`, you must implement
48
+ `def to_model; self; end`. Otherwise, *Simple Form* will convert the decorated object to the model
49
+ since `SimpleDelegator` will delegate it to the model.
50
+ * Code cleanup [@Fornacula](https://github.com/Fornacula)
51
+
52
+ ### Bug fix
53
+ * Fix error when the scope from association has parameter. [@feliperenan](https://github.com/feliperenan)
54
+ * Only call `where` on associations when they respond to it. [@anicholson](https://github.com/anicholson)
55
+ * require 'action_pack' before using it. [@etagwerker](https://github.com/etagwerker)
56
+ * Check if Rails.env is defined. [@etagwerker](https://github.com/etagwerker)
57
+ * Fix minlength. [@mameier](https://github.com/mameier)
58
+ * Make errors_on_attribute return [] when not present. [@redrick](https://github.com/redrick)
59
+ * Fix boolean inputs in nested style for label non-string. [@feliperenan](https://github.com/feliperenan)
60
+
61
+ ## 3.5.0
62
+
63
+ * Updated gem dependency to support Rails 5.1.x.
64
+
3
65
  ## 3.4.0
4
66
 
5
67
  * Removed Ruby 2.4.0 `Integer` unification deprecation warning.
data/README.md CHANGED
@@ -113,7 +113,7 @@ an error, or even a placeholder. For boolean inputs, you can add an inline label
113
113
  <% end %>
114
114
  ```
115
115
 
116
- In some cases you may want to disable labels, hints or error. Or you may want to configure the html
116
+ In some cases you may want to disable labels, hints or errors. Or you may want to configure the html
117
117
  of any of them:
118
118
 
119
119
  ```erb
@@ -164,7 +164,7 @@ any html attribute to that wrapper as well using the `:wrapper_html` option, lik
164
164
 
165
165
  Required fields are marked with an * prepended to their labels.
166
166
 
167
- By default all inputs are required. When the form object includes `ActiveModel::Validations`
167
+ By default all inputs are required. When the form object includes `ActiveModel::Validations`
168
168
  (which, for example, happens with Active Record models), fields are required only when there is `presence` validation.
169
169
  Otherwise, **Simple Form** will mark fields as optional. For performance reasons, this
170
170
  detection is skipped on validations that make use of conditional options, such as `:if` and `:unless`.
@@ -214,7 +214,7 @@ the wrapper as well:
214
214
  <% end %>
215
215
  ```
216
216
 
217
- **Simple Form** accepts same options as their corresponding input type helper in Rails:
217
+ **Simple Form** inputs accept the same options as their corresponding input type helper in Rails:
218
218
 
219
219
  ```erb
220
220
  <%= simple_form_for @user do |f| %>
@@ -325,8 +325,18 @@ Collection inputs accept two other options beside collections:
325
325
  Those methods are useful to manipulate the given collection. Both of these options also accept
326
326
  lambda/procs in case you want to calculate the value or label in a special way eg. custom
327
327
  translation. You can also define a `to_label` method on your model as **Simple Form** will search for
328
- and use `:to_label` as a `:label_method` first if it is found. All other options given are sent
329
- straight to the underlying helper. For example, you can give prompt as:
328
+ and use `:to_label` as a `:label_method` first if it is found.
329
+
330
+ By default, **Simple Form** will use the first item from an array as the label and the second one as the value.
331
+ If you want to change this behavior you must make it explicit, like this:
332
+
333
+ ```erb
334
+ <%= simple_form_for @user do |f| %>
335
+ <%= f.input :gender, as: :radio_buttons, collection: [['0', 'female'], ['1', 'male']], label_method: :second, value_method: :first %>
336
+ <% end %>
337
+ ```
338
+
339
+ All other options given are sent straight to the underlying helper. For example, you can give prompt as:
330
340
 
331
341
  ```ruby
332
342
  f.input :age, collection: 18..60, prompt: "Select your age", selected: 21
@@ -541,31 +551,35 @@ The following table shows the html element you will get for each attribute
541
551
  according to its database definition. These defaults can be changed by
542
552
  specifying the helper method in the column `Mapping` as the `as:` option.
543
553
 
544
- Mapping | Generated HTML Element | Database Column Type
545
- --------------- |:-------------------------------------|:--------------------
546
- `boolean` | `input[type=checkbox]` | `boolean`
547
- `string` | `input[type=text]` | `string`
548
- `email` | `input[type=email]` | `string` with `name =~ /email/`
549
- `url` | `input[type=url]` | `string` with `name =~ /url/`
550
- `tel` | `input[type=tel]` | `string` with `name =~ /phone/`
551
- `password` | `input[type=password]` | `string` with `name =~ /password/`
552
- `search` | `input[type=search]` | -
553
- `uuid` | `input[type=text]` | `uuid`
554
- `text` | `textarea` | `text`
555
- `file` | `input[type=file]` | `string` responding to file methods
556
- `hidden` | `input[type=hidden]` | -
557
- `integer` | `input[type=number]` | `integer`
558
- `float` | `input[type=number]` | `float`
559
- `decimal` | `input[type=number]` | `decimal`
560
- `range` | `input[type=range]` | -
561
- `datetime` | `datetime select` | `datetime/timestamp`
562
- `date` | `date select` | `date`
563
- `time` | `time select` | `time`
564
- `select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
565
- `radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
566
- `check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
567
- `country` | `select` (countries as options) | `string` with `name =~ /country/`
568
- `time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
554
+ Mapping | Generated HTML Element | Database Column Type
555
+ --------------- |--------------------------------------|---------------------
556
+ `boolean` | `input[type=checkbox]` | `boolean`
557
+ `string` | `input[type=text]` | `string`
558
+ `citext` | `input[type=text]` | `citext`
559
+ `email` | `input[type=email]` | `string` with `name =~ /email/`
560
+ `url` | `input[type=url]` | `string` with `name =~ /url/`
561
+ `tel` | `input[type=tel]` | `string` with `name =~ /phone/`
562
+ `password` | `input[type=password]` | `string` with `name =~ /password/`
563
+ `search` | `input[type=search]` | -
564
+ `uuid` | `input[type=text]` | `uuid`
565
+ `text` | `textarea` | `text`
566
+ `hstore` | `textarea` | `hstore`
567
+ `json` | `textarea` | `json`
568
+ `jsonb` | `textarea` | `jsonb`
569
+ `file` | `input[type=file]` | `string` responding to file methods
570
+ `hidden` | `input[type=hidden]` | -
571
+ `integer` | `input[type=number]` | `integer`
572
+ `float` | `input[type=number]` | `float`
573
+ `decimal` | `input[type=number]` | `decimal`
574
+ `range` | `input[type=range]` | -
575
+ `datetime` | `datetime select` | `datetime/timestamp`
576
+ `date` | `date select` | `date`
577
+ `time` | `time select` | `time`
578
+ `select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
579
+ `radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
580
+ `check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
581
+ `country` | `select` (countries as options) | `string` with `name =~ /country/`
582
+ `time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
569
583
 
570
584
  ## Custom inputs
571
585
 
@@ -812,7 +826,7 @@ object itself. Thus, similarly, if a form for an `Admin::User` object is defined
812
826
  `simple_form_for @admin_user, as: :some_user`, **Simple Form** will look for translations
813
827
  under `some_user` instead of `admin_user`.
814
828
 
815
- When translate `simple_fields_for` attributes be sure to use the same name you pass to it, e.g. `simple_fields_for :posts` should be placed under `posts` not `post`:
829
+ When translating `simple_fields_for` attributes be sure to use the same name you pass to it, e.g. `simple_fields_for :posts` should be placed under `posts` not `post`:
816
830
 
817
831
  ```yaml
818
832
  en:
@@ -842,7 +856,8 @@ The syntax looks like this:
842
856
 
843
857
  ```ruby
844
858
  config.wrappers tag: :div, class: :input,
845
- error_class: :field_with_errors do |b|
859
+ error_class: :field_with_errors,
860
+ valid_class: :field_without_errors do |b|
846
861
 
847
862
  # Form extensions
848
863
  b.use :html5
@@ -891,11 +906,12 @@ You can customize _Form components_ passing options to them:
891
906
 
892
907
  ```ruby
893
908
  config.wrappers do |b|
894
- b.use :label_input, class: 'label-input-class'
909
+ b.use :label_input, class: 'label-input-class', error_class: 'is-invalid', valid_class: 'is-valid'
895
910
  end
896
911
  ```
897
912
 
898
- This you set the input and label class to `'label-input-class'`.
913
+ This you set the input and label class to `'label-input-class'` and will set the class `'is-invalid'`
914
+ when the input has errors and `'is-valid'` if the input is valid.
899
915
 
900
916
  If you want to customize the custom _Form components_ on demand you can give it a name like this:
901
917
 
@@ -974,6 +990,92 @@ when the content is present.
974
990
  end
975
991
  ```
976
992
 
993
+ ## Custom Components
994
+
995
+ When you use custom wrappers, you might also be looking for a way to add custom components to your
996
+ wrapper. The default components are:
997
+
998
+ ```ruby
999
+ :label # The <label> tag alone
1000
+ :input # The <input> tag alone
1001
+ :label_input # The <label> and the <input> tags
1002
+ :hint # The hint for the input
1003
+ :error # The error for the input
1004
+ ```
1005
+
1006
+ A custom component might be interesting for you if your views look something like this:
1007
+
1008
+ ```erb
1009
+ <%= simple_form_for @blog do |f| %>
1010
+ <div class="row">
1011
+ <div class="span1 number">
1012
+ 1
1013
+ </div>
1014
+ <div class="span8">
1015
+ <%= f.input :title %>
1016
+ </div>
1017
+ </div>
1018
+ <div class="row">
1019
+ <div class="span1 number">
1020
+ 2
1021
+ </div>
1022
+ <div class="span8">
1023
+ <%= f.input :body, as: :text %>
1024
+ </div>
1025
+ </div>
1026
+ <% end %>
1027
+ ```
1028
+
1029
+ A cleaner method to create your views would be:
1030
+
1031
+ ```erb
1032
+ <%= simple_form_for @blog, wrapper: :with_numbers do |f| %>
1033
+ <%= f.input :title, number: 1 %>
1034
+ <%= f.input :body, as: :text, number: 2 %>
1035
+ <% end %>
1036
+ ```
1037
+
1038
+ To use the number option on the input, first, tells to Simple Form the place where the components
1039
+ will be:
1040
+
1041
+ ``` ruby
1042
+ # config/initializers/simple_form.rb
1043
+ Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
1044
+ ```
1045
+
1046
+ Create a new component within the path specified above:
1047
+
1048
+ ```ruby
1049
+ # lib/components/numbers_component.rb
1050
+ module NumbersComponent
1051
+ # To avoid deprecation warning, you need to make the wrapper_options explicit
1052
+ # even when they won't be used.
1053
+ def number(wrapper_options = nil)
1054
+ @number ||= begin
1055
+ options[:number].to_s.html_safe if options[:number].present?
1056
+ end
1057
+ end
1058
+ end
1059
+
1060
+ SimpleForm.include_component(NumbersComponent)
1061
+ ```
1062
+
1063
+ Finally, add a new wrapper to the config/initializers/simple_form.rb file:
1064
+
1065
+ ```ruby
1066
+ config.wrappers :with_numbers, tag: 'div', class: 'row', error_class: 'error' do |b|
1067
+ b.use :html5
1068
+ b.use :number, wrap_with: { tag: 'div', class: 'span1 number'}
1069
+ b.wrapper tag: 'div', class: 'span8' do |ba|
1070
+ ba.use :placeholder
1071
+ ba.use :label
1072
+ ba.use :input
1073
+ ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
1074
+ ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
1075
+ end
1076
+ end
1077
+ ```
1078
+
977
1079
  ## HTML 5 Notice
978
1080
 
979
1081
  By default, **Simple Form** will generate input field types and attributes that are supported in HTML5,
@@ -1039,6 +1141,76 @@ by passing the html5 option:
1039
1141
  <%= f.input :expires_at, as: :date, html5: true %>
1040
1142
  ```
1041
1143
 
1144
+ ### Using non Active Record objects
1145
+
1146
+ There are few ways to build forms with objects that don't inherit from Active Record, as
1147
+ follow:
1148
+
1149
+ You can include the module `ActiveModel::Model`.
1150
+
1151
+ ```ruby
1152
+ class User
1153
+ include ActiveModel::Model
1154
+
1155
+ attr_accessor :id, :name
1156
+ end
1157
+ ```
1158
+
1159
+ If you are using Presenters or Decorators that inherit from `SimpleDelegator` you can delegate
1160
+ it to the model.
1161
+
1162
+ ```ruby
1163
+ class UserPresenter < SimpleDelegator
1164
+ # Without that, Simple Form will consider the user model as the object.
1165
+ def to_model
1166
+ self
1167
+ end
1168
+ end
1169
+ ```
1170
+
1171
+ You can define all methods required by the helpers.
1172
+
1173
+ ```ruby
1174
+ class User
1175
+ extend ActiveModel::Naming
1176
+
1177
+ attr_accessor :id, :name
1178
+
1179
+ def to_model
1180
+ self
1181
+ end
1182
+
1183
+ def to_key
1184
+ id
1185
+ end
1186
+
1187
+ def persisted?
1188
+ false
1189
+ end
1190
+ end
1191
+ ```
1192
+
1193
+ If your object doesn't implement those methods, you must make explicit it when you are
1194
+ building the form
1195
+
1196
+ ```ruby
1197
+ class User
1198
+ attr_accessor :id, :name
1199
+
1200
+ # The only method required to use the f.submit helper.
1201
+ def persisted?
1202
+ false
1203
+ end
1204
+ end
1205
+ ```
1206
+
1207
+ ```erb
1208
+ <%= simple_form_for(@user, as: :user, method: :post, url: users_path) do |f| %>
1209
+ <%= f.input :name %>
1210
+ <%= f.submit 'New user' %>
1211
+ <% end %>
1212
+ ```
1213
+
1042
1214
  ## Information
1043
1215
 
1044
1216
  ### Google Group
@@ -1057,7 +1229,7 @@ http://rubydoc.info/github/plataformatec/simple_form/master/frames
1057
1229
  ### Bug reports
1058
1230
 
1059
1231
  If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
1060
- possible to help us fixing the possible bug. We also encourage you to help even more by forking and
1232
+ possible to help us in fixing the potential bug. We also encourage you to help even more by forking and
1061
1233
  sending us a pull request.
1062
1234
 
1063
1235
  https://github.com/plataformatec/simple_form/issues
@@ -1076,7 +1248,7 @@ https://github.com/plataformatec/simple_form/issues
1076
1248
 
1077
1249
  ## License
1078
1250
 
1079
- MIT License. Copyright 2009-2016 Plataformatec. http://plataformatec.com.br
1251
+ MIT License. Copyright 2009-2018 Plataformatec. http://plataformatec.com.br
1080
1252
 
1081
1253
  You are not granted rights or licenses to the trademarks of the Plataformatec, including without
1082
1254
  limitation the Simple Form name or logo.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Generators
3
4
  class InstallGenerator < Rails::Generators::Base
@@ -3,9 +3,9 @@
3
3
  Be sure to have a copy of the Bootstrap stylesheet available on your
4
4
  application, you can get it on http://getbootstrap.com/.
5
5
 
6
- Inside your views, use the 'simple_form_for' with one of the Bootstrap form
7
- classes, '.form-horizontal' or '.form-inline', as the following:
6
+ Inside your views, use the 'simple_form_for' with the Bootstrap form
7
+ class, '.form-inline', as the following:
8
8
 
9
- = simple_form_for(@user, html: { class: 'form-horizontal' }) do |form|
9
+ = simple_form_for(@user, html: { class: 'form-inline' }) do |form|
10
10
 
11
11
  ===============================================================================
@@ -1,5 +1,7 @@
1
+ <%# frozen_string_literal: true %>
1
2
  <%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
2
3
  <%%= f.error_notification %>
4
+ <%%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
3
5
 
4
6
  <div class="form-inputs">
5
7
  <%- attributes.each do |attribute| -%>
@@ -1,5 +1,7 @@
1
+ -# frozen_string_literal: true
1
2
  = simple_form_for(@<%= singular_table_name %>) do |f|
2
3
  = f.error_notification
4
+ = f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
3
5
 
4
6
  .form-inputs
5
7
  <%- attributes.each do |attribute| -%>
@@ -1,5 +1,6 @@
1
1
  = simple_form_for(@<%= singular_table_name %>) do |f|
2
2
  = f.error_notification
3
+ = f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
3
4
 
4
5
  .form-inputs
5
6
  <%- attributes.each do |attribute| -%>
@@ -1,3 +1,11 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Uncomment this and change the path if necessary to include your own
4
+ # components.
5
+ # See https://github.com/plataformatec/simple_form#custom-components to know
6
+ # more about custom components.
7
+ # Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
8
+ #
1
9
  # Use this setup block to configure all options available in SimpleForm.
2
10
  SimpleForm.setup do |config|
3
11
  # Wrappers are used by the form builder to generate a
@@ -6,7 +14,7 @@ SimpleForm.setup do |config|
6
14
  # stack. The options given below are used to wrap the
7
15
  # whole input.
8
16
  config.wrappers :default, class: :input,
9
- hint_class: :field_with_hint, error_class: :field_with_errors do |b|
17
+ hint_class: :field_with_hint, error_class: :field_with_errors, valid_class: :field_without_errors do |b|
10
18
  ## Extensions enabled by default
11
19
  # Any of these extensions can be disabled for a
12
20
  # given input by passing: `f.input EXTENSION_NAME => false`.
@@ -44,6 +52,7 @@ SimpleForm.setup do |config|
44
52
  b.optional :readonly
45
53
 
46
54
  ## Inputs
55
+ # b.use :input, class: 'input', error_class: 'is-invalid', valid_class: 'is-valid'
47
56
  b.use :label_input
48
57
  b.use :hint, wrap_with: { tag: :span, class: :hint }
49
58
  b.use :error, wrap_with: { tag: :span, class: :error }
@@ -78,9 +87,6 @@ SimpleForm.setup do |config|
78
87
  # CSS class to add for error notification helper.
79
88
  config.error_notification_class = 'error_notification'
80
89
 
81
- # ID to add for error notification helper.
82
- # config.error_notification_id = nil
83
-
84
90
  # Series of attempts to detect a default label method for collection.
85
91
  # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
86
92
 
@@ -124,7 +130,7 @@ SimpleForm.setup do |config|
124
130
  config.browser_validations = false
125
131
 
126
132
  # Collection of methods to detect if a file type was given.
127
- # config.file_methods = [ :mounted_as, :file?, :public_filename ]
133
+ # config.file_methods = [ :mounted_as, :file?, :public_filename, :attached? ]
128
134
 
129
135
  # Custom mappings for input types. This should be a hash containing a regexp
130
136
  # to match as key, and the input type that will be used when the field name
@@ -166,4 +172,8 @@ SimpleForm.setup do |config|
166
172
 
167
173
  # Defines which i18n scope will be used in Simple Form.
168
174
  # config.i18n_scope = 'simple_form'
175
+
176
+ # Defines validation classes to the input_field. By default it's nil.
177
+ # config.input_field_valid_class = 'is-valid'
178
+ # config.input_field_error_class = 'is-invalid'
169
179
  end