simple_form 2.0.0 → 3.5.1

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.

Potentially problematic release.


This version of simple_form might be problematic. Click here for more details.

Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +97 -198
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +572 -296
  5. data/lib/generators/simple_form/install_generator.rb +17 -7
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/_form.html.erb +1 -0
  8. data/lib/generators/simple_form/templates/_form.html.haml +1 -0
  9. data/lib/generators/simple_form/templates/config/initializers/{simple_form.rb.tt → simple_form.rb} +57 -63
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +155 -0
  11. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +111 -0
  12. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +14 -7
  13. data/lib/simple_form/action_view_extensions/builder.rb +5 -305
  14. data/lib/simple_form/action_view_extensions/form_helper.rb +18 -20
  15. data/lib/simple_form/components/errors.rb +30 -3
  16. data/lib/simple_form/components/hints.rb +10 -3
  17. data/lib/simple_form/components/html5.rb +17 -3
  18. data/lib/simple_form/components/label_input.rb +21 -2
  19. data/lib/simple_form/components/labels.rb +16 -11
  20. data/lib/simple_form/components/maxlength.rb +19 -12
  21. data/lib/simple_form/components/min_max.rb +4 -2
  22. data/lib/simple_form/components/minlength.rb +48 -0
  23. data/lib/simple_form/components/pattern.rb +5 -4
  24. data/lib/simple_form/components/placeholders.rb +3 -2
  25. data/lib/simple_form/components/readonly.rb +3 -2
  26. data/lib/simple_form/components.rb +15 -11
  27. data/lib/simple_form/error_notification.rb +4 -3
  28. data/lib/simple_form/form_builder.rb +283 -105
  29. data/lib/simple_form/helpers/autofocus.rb +1 -0
  30. data/lib/simple_form/helpers/disabled.rb +1 -0
  31. data/lib/simple_form/helpers/readonly.rb +1 -0
  32. data/lib/simple_form/helpers/required.rb +1 -0
  33. data/lib/simple_form/helpers/validators.rb +4 -3
  34. data/lib/simple_form/helpers.rb +7 -6
  35. data/lib/simple_form/i18n_cache.rb +1 -0
  36. data/lib/simple_form/inputs/base.rb +76 -23
  37. data/lib/simple_form/inputs/block_input.rb +3 -2
  38. data/lib/simple_form/inputs/boolean_input.rb +55 -16
  39. data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
  40. data/lib/simple_form/inputs/collection_input.rb +41 -18
  41. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +11 -19
  42. data/lib/simple_form/inputs/collection_select_input.rb +5 -2
  43. data/lib/simple_form/inputs/date_time_input.rb +23 -12
  44. data/lib/simple_form/inputs/file_input.rb +5 -2
  45. data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
  46. data/lib/simple_form/inputs/hidden_input.rb +5 -2
  47. data/lib/simple_form/inputs/numeric_input.rb +4 -8
  48. data/lib/simple_form/inputs/password_input.rb +6 -4
  49. data/lib/simple_form/inputs/priority_input.rb +5 -2
  50. data/lib/simple_form/inputs/range_input.rb +2 -1
  51. data/lib/simple_form/inputs/string_input.rb +6 -4
  52. data/lib/simple_form/inputs/text_input.rb +6 -3
  53. data/lib/simple_form/inputs.rb +20 -17
  54. data/lib/simple_form/map_type.rb +1 -0
  55. data/lib/simple_form/railtie.rb +15 -0
  56. data/lib/simple_form/tags.rb +69 -0
  57. data/lib/simple_form/version.rb +2 -1
  58. data/lib/simple_form/wrappers/builder.rb +12 -35
  59. data/lib/simple_form/wrappers/leaf.rb +29 -0
  60. data/lib/simple_form/wrappers/many.rb +12 -7
  61. data/lib/simple_form/wrappers/root.rb +7 -4
  62. data/lib/simple_form/wrappers/single.rb +12 -3
  63. data/lib/simple_form/wrappers.rb +3 -1
  64. data/lib/simple_form.rb +118 -63
  65. data/test/action_view_extensions/builder_test.rb +230 -164
  66. data/test/action_view_extensions/form_helper_test.rb +107 -39
  67. data/test/components/label_test.rb +105 -87
  68. data/test/form_builder/association_test.rb +131 -62
  69. data/test/form_builder/button_test.rb +15 -14
  70. data/test/form_builder/error_notification_test.rb +11 -10
  71. data/test/form_builder/error_test.rb +188 -34
  72. data/test/form_builder/general_test.rb +247 -102
  73. data/test/form_builder/hint_test.rb +59 -32
  74. data/test/form_builder/input_field_test.rb +138 -25
  75. data/test/form_builder/label_test.rb +84 -13
  76. data/test/form_builder/wrapper_test.rb +236 -33
  77. data/test/generators/simple_form_generator_test.rb +15 -4
  78. data/test/inputs/boolean_input_test.rb +147 -13
  79. data/test/inputs/collection_check_boxes_input_test.rb +166 -71
  80. data/test/inputs/collection_radio_buttons_input_test.rb +229 -113
  81. data/test/inputs/collection_select_input_test.rb +222 -85
  82. data/test/inputs/datetime_input_test.rb +134 -47
  83. data/test/inputs/disabled_test.rb +62 -21
  84. data/test/inputs/discovery_test.rb +70 -10
  85. data/test/inputs/file_input_test.rb +4 -3
  86. data/test/inputs/general_test.rb +90 -26
  87. data/test/inputs/grouped_collection_select_input_test.rb +88 -23
  88. data/test/inputs/hidden_input_test.rb +7 -5
  89. data/test/inputs/numeric_input_test.rb +56 -46
  90. data/test/inputs/priority_input_test.rb +31 -16
  91. data/test/inputs/readonly_test.rb +68 -27
  92. data/test/inputs/required_test.rb +63 -18
  93. data/test/inputs/string_input_test.rb +76 -51
  94. data/test/inputs/text_input_test.rb +21 -8
  95. data/test/simple_form_test.rb +9 -0
  96. data/test/support/discovery_inputs.rb +39 -2
  97. data/test/support/misc_helpers.rb +176 -20
  98. data/test/support/mock_controller.rb +13 -7
  99. data/test/support/models.rb +187 -71
  100. data/test/test_helper.rb +38 -39
  101. metadata +53 -39
  102. data/lib/simple_form/core_ext/hash.rb +0 -16
  103. data/test/support/mock_response.rb +0 -14
data/lib/simple_form.rb CHANGED
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  require 'action_view'
3
+ require 'action_pack'
2
4
  require 'simple_form/action_view_extensions/form_helper'
3
5
  require 'simple_form/action_view_extensions/builder'
4
6
  require 'active_support/core_ext/hash/slice'
@@ -6,14 +8,41 @@ require 'active_support/core_ext/hash/except'
6
8
  require 'active_support/core_ext/hash/reverse_merge'
7
9
 
8
10
  module SimpleForm
9
- autoload :Components, 'simple_form/components'
10
- autoload :ErrorNotification, 'simple_form/error_notification'
11
- autoload :FormBuilder, 'simple_form/form_builder'
12
- autoload :Helpers, 'simple_form/helpers'
13
- autoload :I18nCache, 'simple_form/i18n_cache'
14
- autoload :Inputs, 'simple_form/inputs'
15
- autoload :MapType, 'simple_form/map_type'
16
- autoload :Wrappers, 'simple_form/wrappers'
11
+ extend ActiveSupport::Autoload
12
+
13
+ autoload :Helpers
14
+ autoload :Wrappers
15
+
16
+ eager_autoload do
17
+ autoload :Components
18
+ autoload :ErrorNotification
19
+ autoload :FormBuilder
20
+ autoload :Inputs
21
+ end
22
+
23
+ def self.eager_load!
24
+ super
25
+ SimpleForm::Inputs.eager_load!
26
+ SimpleForm::Components.eager_load!
27
+ end
28
+
29
+ CUSTOM_INPUT_DEPRECATION_WARN = <<-WARN
30
+ %{name} method now accepts a `wrapper_options` argument. The method definition without the argument is deprecated and will be removed in the next Simple Form version. Change your code from:
31
+
32
+ def %{name}
33
+
34
+ to
35
+
36
+ def %{name}(wrapper_options)
37
+
38
+ See https://github.com/plataformatec/simple_form/pull/997 for more information.
39
+ WARN
40
+
41
+ @@configured = false
42
+
43
+ def self.configured? #:nodoc:
44
+ @@configured
45
+ end
17
46
 
18
47
  ## CONFIGURATION OPTIONS
19
48
 
@@ -31,11 +60,11 @@ module SimpleForm
31
60
 
32
61
  # Series of attemps to detect a default label method for collection.
33
62
  mattr_accessor :collection_label_methods
34
- @@collection_label_methods = [ :to_label, :name, :title, :to_s ]
63
+ @@collection_label_methods = %i[to_label name title to_s]
35
64
 
36
65
  # Series of attemps to detect a default value method for collection.
37
66
  mattr_accessor :collection_value_methods
38
- @@collection_value_methods = [ :id, :to_s ]
67
+ @@collection_value_methods = %i[id to_s]
39
68
 
40
69
  # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
41
70
  mattr_accessor :collection_wrapper_tag
@@ -46,7 +75,7 @@ module SimpleForm
46
75
  @@collection_wrapper_class = nil
47
76
 
48
77
  # You can wrap each item in a collection of radio/check boxes with a tag,
49
- # defaulting to none. Please note that when using :boolean_style = :nested,
78
+ # defaulting to span. Please note that when using :boolean_style = :nested,
50
79
  # SimpleForm will force this option to be a :label.
51
80
  mattr_accessor :item_wrapper_tag
52
81
  @@item_wrapper_tag = :span
@@ -57,27 +86,33 @@ module SimpleForm
57
86
 
58
87
  # How the label text should be generated altogether with the required text.
59
88
  mattr_accessor :label_text
60
- @@label_text = lambda { |label, required| "#{required} #{label}" }
89
+ @@label_text = ->(label, required, explicit_label) { "#{required} #{label}" }
61
90
 
62
- # You can define the class to use on all labels. Default is nil.
91
+ # You can define the class to be used on all labels. Defaults to none.
63
92
  mattr_accessor :label_class
64
93
  @@label_class = nil
65
94
 
66
95
  # Define the way to render check boxes / radio buttons with labels.
67
- # :inline => input + label (default)
68
- # :nested => label > input
96
+ # inline: input + label (default)
97
+ # nested: label > input
69
98
  mattr_accessor :boolean_style
70
99
  @@boolean_style = :inline
71
100
 
72
- # You can define the class to use on all forms. Default is simple_form.
73
- mattr_accessor :form_class
101
+ # DEPRECATED: You can define the class to be used on all forms. Default is
102
+ # simple_form.
103
+ mattr_reader :form_class
74
104
  @@form_class = :simple_form
75
105
 
76
- # You can define which elements should obtain additional classes
106
+ # You can define the default class to be used on all forms. Can be overriden
107
+ # with `html: { :class }`. Defaults to none.
108
+ mattr_accessor :default_form_class
109
+ @@default_form_class = nil
110
+
111
+ # You can define which elements should obtain additional classes.
77
112
  mattr_accessor :generate_additional_classes_for
78
- @@generate_additional_classes_for = [:wrapper, :label, :input]
113
+ @@generate_additional_classes_for = %i[wrapper label input]
79
114
 
80
- # Whether attributes are required by default (or not).
115
+ # Whether attributes are required by default or not.
81
116
  mattr_accessor :required_by_default
82
117
  @@required_by_default = true
83
118
 
@@ -87,7 +122,7 @@ module SimpleForm
87
122
 
88
123
  # Collection of methods to detect if a file type was given.
89
124
  mattr_accessor :file_methods
90
- @@file_methods = [ :mounted_as, :file?, :public_filename ]
125
+ @@file_methods = %i[mounted_as file? public_filename]
91
126
 
92
127
  # Custom mappings for input types. This should be a hash containing a regexp
93
128
  # to match as key, and the input type that will be used when the field name
@@ -95,6 +130,22 @@ module SimpleForm
95
130
  mattr_accessor :input_mappings
96
131
  @@input_mappings = nil
97
132
 
133
+ # Custom wrappers for input types. This should be a hash containing an input
134
+ # type as key and the wrapper that will be used for all inputs with specified type.
135
+ # e.g { string: :string_wrapper, boolean: :boolean_wrapper }
136
+ # You can also set a wrapper mapping per form basis.
137
+ # e.g simple_form_for(@foo, wrapper_mappings: { check_boxes: :bootstrap_checkbox })
138
+ mattr_accessor :wrapper_mappings
139
+ @@wrapper_mappings = nil
140
+
141
+ # Namespaces where SimpleForm should look for custom input classes that override
142
+ # default inputs. Namespaces are given as string to allow lazy loading inputs.
143
+ # e.g. config.custom_inputs_namespaces << "CustomInputs"
144
+ # will try to find CustomInputs::NumericInput when an :integer
145
+ # field is called.
146
+ mattr_accessor :custom_inputs_namespaces
147
+ @@custom_inputs_namespaces = []
148
+
98
149
  # Default priority for time_zone inputs.
99
150
  mattr_accessor :time_zone_priority
100
151
  @@time_zone_priority = nil
@@ -103,10 +154,6 @@ module SimpleForm
103
154
  mattr_accessor :country_priority
104
155
  @@country_priority = nil
105
156
 
106
- # Maximum size allowed for inputs.
107
- mattr_accessor :default_input_size
108
- @@default_input_size = 50
109
-
110
157
  # When off, do not use translations in labels. Disabling translation in
111
158
  # hints and placeholders can be done manually in the wrapper API.
112
159
  mattr_accessor :translate_labels
@@ -116,23 +163,46 @@ module SimpleForm
116
163
  mattr_accessor :inputs_discovery
117
164
  @@inputs_discovery = true
118
165
 
119
- # Cache SimpleForm inputs discovery
166
+ # Cache SimpleForm inputs discovery.
120
167
  mattr_accessor :cache_discovery
121
- @@cache_discovery = defined?(Rails) && !Rails.env.development?
168
+ @@cache_discovery = defined?(Rails.env) && !Rails.env.development?
122
169
 
123
- # Adds a class to each generated button, mostly for compatiblity
170
+ # Adds a class to each generated button, mostly for compatiblity.
124
171
  mattr_accessor :button_class
125
172
  @@button_class = 'button'
126
173
 
174
+ # Override the default ActiveModelHelper behaviour of wrapping the input.
175
+ # This gets taken care of semantically by adding an error class to the wrapper tag
176
+ # containing the input.
177
+ mattr_accessor :field_error_proc
178
+ @@field_error_proc = proc do |html_tag, instance_tag|
179
+ html_tag
180
+ end
181
+
182
+ # Adds a class to each generated inputs
183
+ mattr_accessor :input_class
184
+ @@input_class = nil
185
+
186
+ # Defines if an input wrapper class should be included or not
187
+ mattr_accessor :include_default_input_wrapper_class
188
+ @@include_default_input_wrapper_class = true
189
+
190
+ # Define the default class of the input wrapper of the boolean input.
191
+ mattr_accessor :boolean_label_class
192
+ @@boolean_label_class = 'checkbox'
193
+
127
194
  ## WRAPPER CONFIGURATION
128
195
  # The default wrapper to be used by the FormBuilder.
129
196
  mattr_accessor :default_wrapper
130
197
  @@default_wrapper = :default
131
- @@wrappers = {}
198
+ @@wrappers = {} #:nodoc:
199
+
200
+ mattr_accessor :i18n_scope
201
+ @@i18n_scope = 'simple_form'
132
202
 
133
203
  # Retrieves a given wrapper
134
204
  def self.wrapper(name)
135
- @@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
205
+ @@wrappers[name.to_s] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
136
206
  end
137
207
 
138
208
  # Raised when fails to find a given wrapper name
@@ -145,71 +215,56 @@ module SimpleForm
145
215
  if block_given?
146
216
  options = args.extract_options!
147
217
  name = args.first || :default
148
- @@wrappers[name.to_sym] = build(options, &block)
218
+ @@wrappers[name.to_s] = build(options, &block)
149
219
  else
150
220
  @@wrappers
151
221
  end
152
222
  end
153
223
 
154
224
  # Builds a new wrapper using SimpleForm::Wrappers::Builder.
155
- def self.build(options={})
225
+ def self.build(options = {})
156
226
  options[:tag] = :div if options[:tag].nil?
157
227
  builder = SimpleForm::Wrappers::Builder.new(options)
158
228
  yield builder
159
229
  SimpleForm::Wrappers::Root.new(builder.to_a, options)
160
230
  end
161
231
 
162
- wrappers :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
232
+ wrappers class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b|
163
233
  b.use :html5
164
234
 
165
235
  b.use :min_max
166
236
  b.use :maxlength
237
+ b.use :minlength
167
238
  b.use :placeholder
168
239
  b.optional :pattern
169
240
  b.optional :readonly
170
241
 
171
242
  b.use :label_input
172
- b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
173
- b.use :error, :wrap_with => { :tag => :span, :class => :error }
174
- end
175
-
176
- ## SETUP
177
-
178
- DEPRECATED = %w(hint_tag hint_class error_tag error_class error_notification_id wrapper_tag wrapper_class wrapper_error_class components html5)
179
- @@deprecated = []
180
-
181
- DEPRECATED.each do |method|
182
- class_eval "def self.#{method}=(*); @@deprecated << :#{method}=; end"
183
- class_eval "def self.#{method}; deprecation_warn 'SimpleForm.#{method} is deprecated and has no effect'; end"
243
+ b.use :hint, wrap_with: { tag: :span, class: :hint }
244
+ b.use :error, wrap_with: { tag: :span, class: :error }
184
245
  end
185
246
 
186
- def self.translate=(value)
187
- deprecation_warn "SimpleForm.translate= is disabled in favor of translate_labels="
188
- self.translate_labels = value
247
+ def self.additional_classes_for(component)
248
+ generate_additional_classes_for.include?(component) ? yield : []
189
249
  end
190
250
 
191
- def self.translate
192
- deprecation_warn "SimpleForm.translate is disabled in favor of translate_labels"
193
- self.translate_labels
194
- end
251
+ ## SETUP
195
252
 
196
- def self.deprecation_warn(message)
197
- ActiveSupport::Deprecation.warn "[SIMPLE_FORM] #{message}", caller
253
+ def self.default_input_size=(*)
254
+ ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
198
255
  end
199
256
 
200
- def self.additional_classes_for(component)
201
- generate_additional_classes_for.include?(component) ? yield : []
257
+ def self.form_class=(value)
258
+ ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead", caller
259
+ @@form_class = value
202
260
  end
203
261
 
204
- # Default way to setup SimpleForm. Run rails generate simple_form:install
262
+ # Default way to setup Simple Form. Run rails generate simple_form:install
205
263
  # to create a fresh initializer with all configuration values.
206
264
  def self.setup
265
+ @@configured = true
207
266
  yield self
208
-
209
- unless @@deprecated.empty?
210
- raise "[SIMPLE FORM] Your SimpleForm initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
211
- "Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
212
- "Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
213
- end
214
267
  end
215
268
  end
269
+
270
+ require 'simple_form/railtie' if defined?(Rails)