simple_form 3.0.4 → 5.0.3

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 (107) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +199 -33
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +453 -128
  5. data/lib/generators/simple_form/install_generator.rb +4 -3
  6. data/lib/generators/simple_form/templates/README +3 -5
  7. data/lib/generators/simple_form/templates/_form.html.erb +2 -0
  8. data/lib/generators/simple_form/templates/_form.html.haml +2 -0
  9. data/lib/generators/simple_form/templates/_form.html.slim +1 -0
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +47 -16
  11. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +418 -23
  12. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +101 -5
  13. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  14. data/lib/simple_form/action_view_extensions/builder.rb +2 -0
  15. data/lib/simple_form/action_view_extensions/form_helper.rb +10 -3
  16. data/lib/simple_form/components/errors.rb +39 -6
  17. data/lib/simple_form/components/hints.rb +3 -2
  18. data/lib/simple_form/components/html5.rb +16 -5
  19. data/lib/simple_form/components/label_input.rb +21 -2
  20. data/lib/simple_form/components/labels.rb +22 -11
  21. data/lib/simple_form/components/maxlength.rb +9 -5
  22. data/lib/simple_form/components/min_max.rb +2 -1
  23. data/lib/simple_form/components/minlength.rb +38 -0
  24. data/lib/simple_form/components/pattern.rb +2 -1
  25. data/lib/simple_form/components/placeholders.rb +4 -3
  26. data/lib/simple_form/components/readonly.rb +2 -1
  27. data/lib/simple_form/components.rb +2 -0
  28. data/lib/simple_form/error_notification.rb +1 -0
  29. data/lib/simple_form/form_builder.rb +220 -89
  30. data/lib/simple_form/helpers/autofocus.rb +1 -0
  31. data/lib/simple_form/helpers/disabled.rb +1 -0
  32. data/lib/simple_form/helpers/readonly.rb +1 -0
  33. data/lib/simple_form/helpers/required.rb +1 -0
  34. data/lib/simple_form/helpers/validators.rb +2 -1
  35. data/lib/simple_form/helpers.rb +6 -5
  36. data/lib/simple_form/i18n_cache.rb +1 -0
  37. data/lib/simple_form/inputs/base.rb +62 -16
  38. data/lib/simple_form/inputs/block_input.rb +2 -1
  39. data/lib/simple_form/inputs/boolean_input.rb +40 -16
  40. data/lib/simple_form/inputs/collection_check_boxes_input.rb +3 -2
  41. data/lib/simple_form/inputs/collection_input.rb +37 -14
  42. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +9 -13
  43. data/lib/simple_form/inputs/collection_select_input.rb +5 -2
  44. data/lib/simple_form/inputs/color_input.rb +14 -0
  45. data/lib/simple_form/inputs/date_time_input.rb +24 -9
  46. data/lib/simple_form/inputs/file_input.rb +5 -2
  47. data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
  48. data/lib/simple_form/inputs/hidden_input.rb +5 -2
  49. data/lib/simple_form/inputs/numeric_input.rb +6 -4
  50. data/lib/simple_form/inputs/password_input.rb +6 -3
  51. data/lib/simple_form/inputs/priority_input.rb +5 -6
  52. data/lib/simple_form/inputs/range_input.rb +2 -1
  53. data/lib/simple_form/inputs/rich_text_area_input.rb +12 -0
  54. data/lib/simple_form/inputs/string_input.rb +7 -4
  55. data/lib/simple_form/inputs/text_input.rb +6 -3
  56. data/lib/simple_form/inputs.rb +3 -0
  57. data/lib/simple_form/map_type.rb +1 -0
  58. data/lib/simple_form/railtie.rb +8 -0
  59. data/lib/simple_form/tags.rb +13 -2
  60. data/lib/simple_form/version.rb +2 -1
  61. data/lib/simple_form/wrappers/builder.rb +7 -6
  62. data/lib/simple_form/wrappers/leaf.rb +29 -0
  63. data/lib/simple_form/wrappers/many.rb +7 -6
  64. data/lib/simple_form/wrappers/root.rb +10 -3
  65. data/lib/simple_form/wrappers/single.rb +7 -4
  66. data/lib/simple_form/wrappers.rb +2 -0
  67. data/lib/simple_form.rb +137 -21
  68. data/test/action_view_extensions/builder_test.rb +64 -45
  69. data/test/action_view_extensions/form_helper_test.rb +36 -16
  70. data/test/components/custom_components_test.rb +62 -0
  71. data/test/components/label_test.rb +70 -41
  72. data/test/form_builder/association_test.rb +85 -37
  73. data/test/form_builder/button_test.rb +11 -10
  74. data/test/form_builder/error_notification_test.rb +2 -1
  75. data/test/form_builder/error_test.rb +146 -33
  76. data/test/form_builder/general_test.rb +183 -81
  77. data/test/form_builder/hint_test.rb +24 -18
  78. data/test/form_builder/input_field_test.rb +105 -75
  79. data/test/form_builder/label_test.rb +68 -13
  80. data/test/form_builder/wrapper_test.rb +197 -22
  81. data/test/generators/simple_form_generator_test.rb +8 -7
  82. data/test/inputs/boolean_input_test.rb +97 -6
  83. data/test/inputs/collection_check_boxes_input_test.rb +117 -25
  84. data/test/inputs/collection_radio_buttons_input_test.rb +176 -54
  85. data/test/inputs/collection_select_input_test.rb +189 -77
  86. data/test/inputs/color_input_test.rb +10 -0
  87. data/test/inputs/datetime_input_test.rb +121 -50
  88. data/test/inputs/disabled_test.rb +29 -15
  89. data/test/inputs/discovery_test.rb +79 -6
  90. data/test/inputs/file_input_test.rb +3 -2
  91. data/test/inputs/general_test.rb +23 -22
  92. data/test/inputs/grouped_collection_select_input_test.rb +54 -17
  93. data/test/inputs/hidden_input_test.rb +5 -4
  94. data/test/inputs/numeric_input_test.rb +48 -44
  95. data/test/inputs/priority_input_test.rb +17 -16
  96. data/test/inputs/readonly_test.rb +20 -19
  97. data/test/inputs/required_test.rb +58 -13
  98. data/test/inputs/rich_text_area_input_test.rb +15 -0
  99. data/test/inputs/string_input_test.rb +58 -36
  100. data/test/inputs/text_input_test.rb +20 -7
  101. data/test/simple_form_test.rb +9 -0
  102. data/test/support/discovery_inputs.rb +40 -2
  103. data/test/support/misc_helpers.rb +113 -5
  104. data/test/support/mock_controller.rb +7 -1
  105. data/test/support/models.rb +162 -39
  106. data/test/test_helper.rb +19 -4
  107. metadata +51 -43
@@ -1,26 +1,122 @@
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/heartcombo/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
- config.wrappers :foundation, class: :input, hint_class: :field_with_hint, error_class: :error do |b|
11
+ # Don't forget to edit this file to adapt it to your needs (specially
12
+ # all the grid-related classes)
13
+ #
14
+ # Please note that hints are commented out by default since Foundation
15
+ # doesn't provide styles for hints. You will need to provide your own CSS styles for hints.
16
+ # Uncomment them to enable hints.
17
+
18
+ config.wrappers :vertical_form, class: :input, hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
4
19
  b.use :html5
5
20
  b.use :placeholder
6
21
  b.optional :maxlength
22
+ b.optional :minlength
7
23
  b.optional :pattern
8
24
  b.optional :min_max
9
25
  b.optional :readonly
10
26
  b.use :label_input
11
- b.use :error, wrap_with: { tag: :small }
27
+ b.use :error, wrap_with: { tag: :small, class: :error }
12
28
 
13
- # Uncomment the following line to enable hints. The line is commented out by default since Foundation
14
- # does't provide styles for hints. You will need to provide your own CSS styles for hints.
29
+ # b.use :hint, wrap_with: { tag: :span, class: :hint }
30
+ end
31
+
32
+ config.wrappers :horizontal_form, tag: 'div', class: 'row', hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
33
+ b.use :html5
34
+ b.use :placeholder
35
+ b.optional :maxlength
36
+ b.optional :minlength
37
+ b.optional :pattern
38
+ b.optional :min_max
39
+ b.optional :readonly
40
+
41
+ b.wrapper :label_wrapper, tag: :div, class: 'small-3 columns' do |ba|
42
+ ba.use :label, class: 'text-right inline'
43
+ end
44
+
45
+ b.wrapper :right_input_wrapper, tag: :div, class: 'small-9 columns' do |ba|
46
+ ba.use :input
47
+ ba.use :error, wrap_with: { tag: :small, class: :error }
48
+ # ba.use :hint, wrap_with: { tag: :span, class: :hint }
49
+ end
50
+ end
51
+
52
+ config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'row' do |b|
53
+ b.use :html5
54
+ b.optional :readonly
55
+
56
+ b.wrapper :container_wrapper, tag: 'div', class: 'small-offset-3 small-9 columns' do |ba|
57
+ ba.wrapper tag: 'label', class: 'checkbox' do |bb|
58
+ bb.use :input
59
+ bb.use :label_text
60
+ end
61
+
62
+ ba.use :error, wrap_with: { tag: :small, class: :error }
63
+ # ba.use :hint, wrap_with: { tag: :span, class: :hint }
64
+ end
65
+ end
66
+
67
+ # Foundation does not provide a way to handle inline forms
68
+ # This wrapper can be used to create an inline form
69
+ # by hiding that labels on every screen sizes ('hidden-for-small-up').
70
+ #
71
+ # Note that you need to adapt this wrapper to your needs. If you need a 4
72
+ # columns form then change the wrapper class to 'small-3', if you need
73
+ # only two use 'small-6' and so on.
74
+ config.wrappers :inline_form, tag: 'div', class: 'column small-4', hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
75
+ b.use :html5
76
+ b.use :placeholder
77
+ b.optional :maxlength
78
+ b.optional :minlength
79
+ b.optional :pattern
80
+ b.optional :min_max
81
+ b.optional :readonly
82
+
83
+ b.use :label, class: 'hidden-for-small-up'
84
+ b.use :input
85
+
86
+ b.use :error, wrap_with: { tag: :small, class: :error }
87
+ # b.use :hint, wrap_with: { tag: :span, class: :hint }
88
+ end
89
+
90
+ # Examples of use:
91
+ # - wrapper_html: {class: 'row'}, custom_wrapper_html: {class: 'column small-12'}
92
+ # - custom_wrapper_html: {class: 'column small-3 end'}
93
+ config.wrappers :customizable_wrapper, tag: 'div', error_class: :error, valid_class: :valid do |b|
94
+ b.use :html5
95
+ b.optional :readonly
96
+
97
+ b.wrapper :custom_wrapper, tag: :div do |ba|
98
+ ba.use :label_input
99
+ end
100
+
101
+ b.use :error, wrap_with: { tag: :small, class: :error }
15
102
  # b.use :hint, wrap_with: { tag: :span, class: :hint }
16
103
  end
17
104
 
18
105
  # CSS class for buttons
19
106
  config.button_class = 'button'
20
107
 
108
+ # Set this to div to make the checkbox and radio properly work
109
+ # otherwise simple_form adds a label tag instead of a div around
110
+ # the nested label
111
+ config.item_wrapper_tag = :div
112
+
21
113
  # CSS class to add for error notification helper.
22
114
  config.error_notification_class = 'alert-box alert'
23
115
 
24
116
  # The default wrapper to be used by the FormBuilder.
25
- config.default_wrapper = :foundation
117
+ config.default_wrapper = :vertical_form
118
+
119
+ # Defines validation classes to the input_field. By default it's nil.
120
+ # config.input_field_valid_class = 'is-valid'
121
+ # config.input_field_error_class = 'is-invalid'
26
122
  end
@@ -10,7 +10,7 @@ en:
10
10
  # html: '<abbr title="required">*</abbr>'
11
11
  error_notification:
12
12
  default_message: "Please review the problems below:"
13
- # Labels and hints examples
13
+ # Examples
14
14
  # labels:
15
15
  # defaults:
16
16
  # password: 'Password'
@@ -23,4 +23,9 @@ en:
23
23
  # defaults:
24
24
  # username: 'User name to sign in.'
25
25
  # password: 'No special characters, please.'
26
-
26
+ # include_blanks:
27
+ # defaults:
28
+ # age: 'Rather not say'
29
+ # prompts:
30
+ # defaults:
31
+ # age: 'Select your age'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module ActionViewExtensions
3
4
  # A collection of methods required by simple_form but added to rails default form.
@@ -17,6 +18,7 @@ module SimpleForm
17
18
  options = args.extract_options!
18
19
  options[:wrapper] = self.options[:wrapper] if options[:wrapper].nil?
19
20
  options[:defaults] ||= self.options[:defaults]
21
+ options[:wrapper_mappings] ||= self.options[:wrapper_mappings]
20
22
 
21
23
  if self.class < ActionView::Helpers::FormBuilder
22
24
  options[:builder] ||= self.class
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module ActionViewExtensions
3
4
  # This module creates SimpleForm wrappers around default form_for and fields_for.
@@ -10,13 +11,17 @@ module SimpleForm
10
11
  #
11
12
  module FormHelper
12
13
 
13
- def simple_form_for(record, options={}, &block)
14
+ def simple_form_for(record, options = {}, &block)
14
15
  options[:builder] ||= SimpleForm::FormBuilder
15
16
  options[:html] ||= {}
16
17
  unless options[:html].key?(:novalidate)
17
18
  options[:html][:novalidate] = !SimpleForm.browser_validations
18
19
  end
19
- options[:html][:class] = [SimpleForm.form_class, simple_form_css_class(record, options)].compact.join(" ")
20
+ if options[:html].key?(:class)
21
+ options[:html][:class] = [SimpleForm.form_class, options[:html][:class]].compact
22
+ else
23
+ options[:html][:class] = [SimpleForm.form_class, SimpleForm.default_form_class, simple_form_css_class(record, options)].compact
24
+ end
20
25
 
21
26
  with_simple_form_field_error_proc do
22
27
  form_for(record, options, &block)
@@ -62,4 +67,6 @@ module SimpleForm
62
67
  end
63
68
  end
64
69
 
65
- ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper
70
+ ActiveSupport.on_load(:action_view) do
71
+ include SimpleForm::ActionViewExtensions::FormHelper
72
+ end
@@ -1,22 +1,43 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  module Errors
4
- def error
5
+ def error(wrapper_options = nil)
5
6
  error_text if has_errors?
6
7
  end
7
8
 
9
+ def full_error(wrapper_options = nil)
10
+ full_error_text if options[:error] != false && has_errors?
11
+ end
12
+
8
13
  def has_errors?
9
- object && object.respond_to?(:errors) && errors.present?
14
+ object_with_errors? || object.nil? && has_custom_error?
15
+ end
16
+
17
+ def has_value?
18
+ object && object.respond_to?(attribute_name) && object.send(attribute_name).present?
19
+ end
20
+
21
+ def valid?
22
+ !has_errors? && has_value?
10
23
  end
11
24
 
12
25
  protected
13
26
 
14
27
  def error_text
15
- text = has_error_in_options? ? options[:error] : errors.send(error_method)
28
+ text = has_custom_error? ? options[:error] : errors.send(error_method)
16
29
 
17
30
  "#{html_escape(options[:error_prefix])} #{html_escape(text)}".lstrip.html_safe
18
31
  end
19
32
 
33
+ def full_error_text
34
+ has_custom_error? ? options[:error] : full_errors.send(error_method)
35
+ end
36
+
37
+ def object_with_errors?
38
+ object && object.respond_to?(:errors) && errors.present?
39
+ end
40
+
20
41
  def error_method
21
42
  options[:error_method] || SimpleForm.error_method
22
43
  end
@@ -25,16 +46,28 @@ module SimpleForm
25
46
  @errors ||= (errors_on_attribute + errors_on_association).compact
26
47
  end
27
48
 
49
+ def full_errors
50
+ @full_errors ||= (full_errors_on_attribute + full_errors_on_association).compact
51
+ end
52
+
28
53
  def errors_on_attribute
29
- object.errors[attribute_name]
54
+ object.errors[attribute_name] || []
55
+ end
56
+
57
+ def full_errors_on_attribute
58
+ object.errors.full_messages_for(attribute_name)
30
59
  end
31
60
 
32
61
  def errors_on_association
33
62
  reflection ? object.errors[reflection.name] : []
34
63
  end
35
64
 
36
- def has_error_in_options?
37
- options[:error] && !options[:error].nil?
65
+ def full_errors_on_association
66
+ reflection ? object.errors.full_messages_for(reflection.name) : []
67
+ end
68
+
69
+ def has_custom_error?
70
+ options[:error].is_a?(String)
38
71
  end
39
72
  end
40
73
  end
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  # Needs to be enabled in order to do automatic lookups.
4
5
  module Hints
5
- def hint
6
+ def hint(wrapper_options = nil)
6
7
  @hint ||= begin
7
8
  hint = options[:hint]
8
9
 
9
10
  if hint.is_a?(String)
10
11
  html_escape(hint)
11
12
  else
12
- content = translate(:hints)
13
+ content = translate_from_namespace(:hints)
13
14
  content.html_safe if content
14
15
  end
15
16
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  module HTML5
@@ -5,12 +6,14 @@ module SimpleForm
5
6
  @html5 = false
6
7
  end
7
8
 
8
- def html5
9
+ def html5(wrapper_options = nil)
9
10
  @html5 = true
10
- if has_required?
11
- input_html_options[:required] = true
12
- input_html_options[:'aria-required'] = true
13
- end
11
+
12
+ input_html_options[:required] = input_html_required_option
13
+ input_html_options[:'aria-required'] = input_html_aria_required_option
14
+
15
+ input_html_options[:'aria-invalid'] = has_errors? || nil
16
+
14
17
  nil
15
18
  end
16
19
 
@@ -18,6 +21,14 @@ module SimpleForm
18
21
  @html5
19
22
  end
20
23
 
24
+ def input_html_required_option
25
+ !options[:required].nil? ? required_field? : has_required?
26
+ end
27
+
28
+ def input_html_aria_required_option
29
+ !options[:required].nil? ? (required_field? || nil) : (has_required? || nil)
30
+ end
31
+
21
32
  def has_required?
22
33
  # We need to check browser_validations because
23
34
  # some browsers are still checking required even
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  module LabelInput
@@ -7,8 +8,26 @@ module SimpleForm
7
8
  include SimpleForm::Components::Labels
8
9
  end
9
10
 
10
- def label_input
11
- options[:label] == false ? input : (label + input)
11
+ def label_input(wrapper_options = nil)
12
+ if options[:label] == false
13
+ deprecated_component(:input, wrapper_options)
14
+ else
15
+ deprecated_component(:label, wrapper_options) + deprecated_component(:input, wrapper_options)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def deprecated_component(namespace, wrapper_options)
22
+ method = method(namespace)
23
+
24
+ if method.arity.zero?
25
+ ActiveSupport::Deprecation.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: namespace })
26
+
27
+ method.call
28
+ else
29
+ method.call(wrapper_options)
30
+ end
12
31
  end
13
32
  end
14
33
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  module Labels
@@ -6,31 +7,40 @@ module SimpleForm
6
7
  module ClassMethods #:nodoc:
7
8
  def translate_required_html
8
9
  i18n_cache :translate_required_html do
9
- I18n.t(:"simple_form.required.html", default:
10
- %[<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr>]
10
+ I18n.t(:"required.html", scope: i18n_scope, default:
11
+ %(<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr>)
11
12
  )
12
13
  end
13
14
  end
14
15
 
15
16
  def translate_required_text
16
- I18n.t(:"simple_form.required.text", default: 'required')
17
+ I18n.t(:"required.text", scope: i18n_scope, default: 'required')
17
18
  end
18
19
 
19
20
  def translate_required_mark
20
- I18n.t(:"simple_form.required.mark", default: '*')
21
+ I18n.t(:"required.mark", scope: i18n_scope, default: '*')
22
+ end
23
+
24
+ private
25
+
26
+ def i18n_scope
27
+ SimpleForm.i18n_scope
21
28
  end
22
29
  end
23
30
 
24
- def label
31
+ def label(wrapper_options = nil)
32
+ label_options = merge_wrapper_options(label_html_options, wrapper_options)
33
+
25
34
  if generate_label_for_attribute?
26
- @builder.label(label_target, label_text, label_html_options)
35
+ @builder.label(label_target, label_text, label_options)
27
36
  else
28
- template.label_tag(nil, label_text, label_html_options)
37
+ template.label_tag(nil, label_text, label_options)
29
38
  end
30
39
  end
31
40
 
32
- def label_text
33
- SimpleForm.label_text.call(html_escape(raw_label_text), required_label_text).strip.html_safe
41
+ def label_text(wrapper_options = nil)
42
+ label_text = options[:label_text] || SimpleForm.label_text
43
+ label_text.call(html_escape(raw_label_text), required_label_text, options[:label].present?).strip.html_safe
34
44
  end
35
45
 
36
46
  def label_target
@@ -39,13 +49,14 @@ module SimpleForm
39
49
 
40
50
  def label_html_options
41
51
  label_html_classes = SimpleForm.additional_classes_for(:label) {
42
- [input_type, required_class, SimpleForm.label_class].compact
52
+ [input_type, required_class, disabled_class, SimpleForm.label_class].compact
43
53
  }
44
54
 
45
55
  label_options = html_options_for(:label, label_html_classes)
46
56
  if options.key?(:input_html) && options[:input_html].key?(:id)
47
57
  label_options[:for] = options[:input_html][:id]
48
58
  end
59
+
49
60
  label_options
50
61
  end
51
62
 
@@ -62,7 +73,7 @@ module SimpleForm
62
73
 
63
74
  # First check labels translation and then human attribute name.
64
75
  def label_translation #:nodoc:
65
- if SimpleForm.translate_labels && (translated_label = translate(:labels))
76
+ if SimpleForm.translate_labels && (translated_label = translate_from_namespace(:labels))
66
77
  translated_label
67
78
  elsif object.class.respond_to?(:human_attribute_name)
68
79
  object.class.human_attribute_name(reflection_or_attribute_name.to_s)
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  # Needs to be enabled in order to do automatic lookups.
4
5
  module Maxlength
5
- def maxlength
6
+ def maxlength(wrapper_options = nil)
6
7
  input_html_options[:maxlength] ||= maximum_length_from_validation || limit
7
8
  nil
8
9
  end
@@ -15,10 +16,7 @@ module SimpleForm
15
16
  maxlength
16
17
  else
17
18
  length_validator = find_length_validator
18
-
19
- if length_validator && !has_tokenizer?(length_validator)
20
- length_validator.options[:is] || length_validator.options[:maximum]
21
- end
19
+ maximum_length_value_from(length_validator)
22
20
  end
23
21
  end
24
22
 
@@ -29,6 +27,12 @@ module SimpleForm
29
27
  def has_tokenizer?(length_validator)
30
28
  length_validator.options[:tokenizer]
31
29
  end
30
+
31
+ def maximum_length_value_from(length_validator)
32
+ if length_validator
33
+ length_validator.options[:is] || length_validator.options[:maximum]
34
+ end
35
+ end
32
36
  end
33
37
  end
34
38
  end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  module MinMax
4
- def min_max
5
+ def min_max(wrapper_options = nil)
5
6
  if numeric_validator = find_numericality_validator
6
7
  validator_options = numeric_validator.options
7
8
  input_html_options[:min] ||= minimum_value(validator_options)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ module SimpleForm
3
+ module Components
4
+ # Needs to be enabled in order to do automatic lookups.
5
+ module Minlength
6
+ def minlength(wrapper_options = nil)
7
+ input_html_options[:minlength] ||= minimum_length_from_validation
8
+ nil
9
+ end
10
+
11
+ private
12
+
13
+ def minimum_length_from_validation
14
+ minlength = options[:minlength]
15
+ if minlength.is_a?(String) || minlength.is_a?(Integer)
16
+ minlength
17
+ else
18
+ length_validator = find_length_validator
19
+ minimum_length_value_from(length_validator)
20
+ end
21
+ end
22
+
23
+ def find_length_validator
24
+ find_validator(:length)
25
+ end
26
+
27
+ def has_tokenizer?(length_validator)
28
+ length_validator.options[:tokenizer]
29
+ end
30
+
31
+ def minimum_length_value_from(length_validator)
32
+ if length_validator
33
+ length_validator.options[:is] || length_validator.options[:minimum]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  # Needs to be enabled in order to do automatic lookups.
4
5
  module Pattern
5
- def pattern
6
+ def pattern(wrapper_options = nil)
6
7
  input_html_options[:pattern] ||= pattern_source
7
8
  nil
8
9
  end
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  # Needs to be enabled in order to do automatic lookups.
4
5
  module Placeholders
5
- def placeholder
6
+ def placeholder(wrapper_options = nil)
6
7
  input_html_options[:placeholder] ||= placeholder_text
7
8
  nil
8
9
  end
9
10
 
10
- def placeholder_text
11
+ def placeholder_text(wrapper_options = nil)
11
12
  placeholder = options[:placeholder]
12
- placeholder.is_a?(String) ? placeholder : translate(:placeholders)
13
+ placeholder.is_a?(String) ? placeholder : translate_from_namespace(:placeholders)
13
14
  end
14
15
  end
15
16
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  module Components
3
4
  # Needs to be enabled in order to do automatic lookups.
4
5
  module Readonly
5
- def readonly
6
+ def readonly(wrapper_options = nil)
6
7
  if readonly_attribute? && !has_readonly?
7
8
  input_html_options[:readonly] ||= true
8
9
  input_html_classes << :readonly
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  # Components are a special type of helpers that can work on their own.
3
4
  # For example, by using a component, it will automatically change the
@@ -15,6 +16,7 @@ module SimpleForm
15
16
  autoload :Labels
16
17
  autoload :MinMax
17
18
  autoload :Maxlength
19
+ autoload :Minlength
18
20
  autoload :Pattern
19
21
  autoload :Placeholders
20
22
  autoload :Readonly
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleForm
2
3
  class ErrorNotification
3
4
  delegate :object, :object_name, :template, to: :@builder