simple_form 2.1.3 → 3.0.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.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -51
- data/README.md +195 -161
- data/lib/generators/simple_form/install_generator.rb +4 -4
- data/lib/generators/simple_form/templates/README +2 -2
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +16 -13
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +16 -16
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +3 -3
- data/lib/simple_form/action_view_extensions/builder.rb +1 -320
- data/lib/simple_form/action_view_extensions/builder.rb.orig +247 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +2 -9
- data/lib/simple_form/components/errors.rb +1 -7
- data/lib/simple_form/components/hints.rb +2 -7
- data/lib/simple_form/components/html5.rb +5 -2
- data/lib/simple_form/components/labels.rb +4 -4
- data/lib/simple_form/components/maxlength.rb +1 -8
- data/lib/simple_form/components/pattern.rb +2 -2
- data/lib/simple_form/components.rb +1 -1
- data/lib/simple_form/error_notification.rb +2 -2
- data/lib/simple_form/form_builder.rb +154 -50
- data/lib/simple_form/form_builder.rb.orig +486 -0
- data/lib/simple_form/helpers.rb +1 -1
- data/lib/simple_form/inputs/base.rb +7 -10
- data/lib/simple_form/inputs/block_input.rb +1 -1
- data/lib/simple_form/inputs/boolean_input.rb +6 -5
- data/lib/simple_form/inputs/collection_input.rb +7 -7
- data/lib/simple_form/inputs/date_time_input.rb +1 -1
- data/lib/simple_form/inputs/numeric_input.rb +0 -6
- data/lib/simple_form/inputs/password_input.rb +0 -1
- data/lib/simple_form/inputs/string_input.rb +0 -1
- data/lib/simple_form/railtie.rb +7 -0
- data/lib/simple_form/tags.rb +62 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/version.rb.orig +7 -0
- data/lib/simple_form/wrappers/builder.rb +5 -29
- data/lib/simple_form/wrappers/many.rb +1 -1
- data/lib/simple_form/wrappers/root.rb +1 -1
- data/lib/simple_form/wrappers.rb +1 -1
- data/lib/simple_form.rb +43 -47
- data/test/action_view_extensions/builder_test.rb +78 -99
- data/test/action_view_extensions/form_helper_test.rb +25 -16
- data/test/components/label_test.rb +46 -46
- data/test/form_builder/association_test.rb +47 -29
- data/test/form_builder/button_test.rb +4 -4
- data/test/form_builder/error_notification_test.rb +8 -8
- data/test/form_builder/error_test.rb +18 -65
- data/test/form_builder/general_test.rb +62 -63
- data/test/form_builder/hint_test.rb +23 -29
- data/test/form_builder/input_field_test.rb +29 -12
- data/test/form_builder/label_test.rb +7 -17
- data/test/form_builder/wrapper_test.rb +21 -21
- data/test/inputs/boolean_input_test.rb +24 -24
- data/test/inputs/collection_check_boxes_input_test.rb +66 -55
- data/test/inputs/collection_radio_buttons_input_test.rb +81 -79
- data/test/inputs/collection_select_input_test.rb +76 -51
- data/test/inputs/datetime_input_test.rb +17 -11
- data/test/inputs/disabled_test.rb +10 -10
- data/test/inputs/discovery_test.rb +4 -4
- data/test/inputs/file_input_test.rb +1 -1
- data/test/inputs/general_test.rb +28 -12
- data/test/inputs/grouped_collection_select_input_test.rb +33 -20
- data/test/inputs/hidden_input_test.rb +3 -2
- data/test/inputs/numeric_input_test.rb +3 -3
- data/test/inputs/priority_input_test.rb +9 -3
- data/test/inputs/readonly_test.rb +12 -12
- data/test/inputs/required_test.rb +5 -5
- data/test/inputs/string_input_test.rb +15 -25
- data/test/inputs/text_input_test.rb +1 -1
- data/test/support/misc_helpers.rb +46 -24
- data/test/support/mock_controller.rb +6 -6
- data/test/support/models.rb +77 -62
- data/test/test_helper.rb +17 -34
- metadata +39 -22
- data/lib/simple_form/core_ext/hash.rb +0 -16
@@ -0,0 +1,62 @@
|
|
1
|
+
module SimpleForm
|
2
|
+
module Tags
|
3
|
+
module CollectionExtensions
|
4
|
+
private
|
5
|
+
|
6
|
+
def render_collection
|
7
|
+
item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
|
8
|
+
item_wrapper_class = @options[:item_wrapper_class]
|
9
|
+
|
10
|
+
@collection.map do |item|
|
11
|
+
value = value_for_collection(item, @value_method)
|
12
|
+
text = value_for_collection(item, @text_method)
|
13
|
+
default_html_options = default_html_options_for_collection(item, value)
|
14
|
+
additional_html_options = option_html_attributes(item)
|
15
|
+
|
16
|
+
rendered_item = yield item, value, text, default_html_options.merge(additional_html_options)
|
17
|
+
|
18
|
+
item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item
|
19
|
+
end.join.html_safe
|
20
|
+
end
|
21
|
+
|
22
|
+
def wrap_rendered_collection(collection)
|
23
|
+
wrapper_tag = @options[:collection_wrapper_tag]
|
24
|
+
|
25
|
+
if wrapper_tag
|
26
|
+
wrapper_class = @options[:collection_wrapper_class]
|
27
|
+
@template_object.content_tag(wrapper_tag, collection, class: wrapper_class)
|
28
|
+
else
|
29
|
+
collection
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
|
35
|
+
include CollectionExtensions
|
36
|
+
|
37
|
+
def render
|
38
|
+
wrap_rendered_collection(super)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def render_component(builder)
|
44
|
+
builder.radio_button + builder.label(class: "collection_radio_buttons")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes
|
49
|
+
include CollectionExtensions
|
50
|
+
|
51
|
+
def render
|
52
|
+
wrap_rendered_collection(super)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def render_component(builder)
|
58
|
+
builder.check_box + builder.label(class: "collection_check_boxes")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/simple_form/version.rb
CHANGED
@@ -12,18 +12,18 @@ module SimpleForm
|
|
12
12
|
# b.optional :placeholder
|
13
13
|
#
|
14
14
|
# # Use a component with specific wrapper options
|
15
|
-
# b.use :error, :
|
15
|
+
# b.use :error, wrap_with: { tag: "span", class: "error" }
|
16
16
|
#
|
17
17
|
# # Use a set of components by wrapping them in a tag+class.
|
18
|
-
# b.wrapper :
|
18
|
+
# b.wrapper tag: "div", class: "another" do |ba|
|
19
19
|
# ba.use :label
|
20
20
|
# ba.use :input
|
21
21
|
# end
|
22
22
|
#
|
23
23
|
# # Use a set of components by wrapping them in a tag+class.
|
24
24
|
# # This wrapper is identified by :label_input, which means it can
|
25
|
-
# # be turned off on demand with `f.input :name, :
|
26
|
-
# b.wrapper :label_input, :
|
25
|
+
# # be turned off on demand with `f.input :name, label_input: false`
|
26
|
+
# b.wrapper :label_input, tag: "div", class: "another" do |ba|
|
27
27
|
# ba.use :label
|
28
28
|
# ba.use :input
|
29
29
|
# end
|
@@ -32,7 +32,7 @@ module SimpleForm
|
|
32
32
|
# The builder also accepts default options at the root level. This is usually
|
33
33
|
# used if you want a component to be disabled by default:
|
34
34
|
#
|
35
|
-
# config.wrappers :
|
35
|
+
# config.wrappers hint: false do |b|
|
36
36
|
# b.use :hint
|
37
37
|
# b.use :label_input
|
38
38
|
# end
|
@@ -46,18 +46,6 @@ module SimpleForm
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def use(name, options=nil, &block)
|
49
|
-
if block_given?
|
50
|
-
ActiveSupport::Deprecation.warn "Passing a block to use is deprecated. " \
|
51
|
-
"Please use wrapper instead of use."
|
52
|
-
return wrapper(name, options, &block)
|
53
|
-
end
|
54
|
-
|
55
|
-
if options && options.keys != [:wrap_with]
|
56
|
-
ActiveSupport::Deprecation.warn "Passing :tag, :class and others to use is deprecated. " \
|
57
|
-
"Please invoke b.use #{name.inspect}, :wrap_with => #{options.inspect} instead."
|
58
|
-
options = { :wrap_with => options }
|
59
|
-
end
|
60
|
-
|
61
49
|
if options && wrapper = options[:wrap_with]
|
62
50
|
@components << Single.new(name, wrapper)
|
63
51
|
else
|
@@ -66,18 +54,6 @@ module SimpleForm
|
|
66
54
|
end
|
67
55
|
|
68
56
|
def optional(name, options=nil, &block)
|
69
|
-
if block_given?
|
70
|
-
ActiveSupport::Deprecation.warn "Passing a block to optional is deprecated. " \
|
71
|
-
"Please use wrapper instead of optional."
|
72
|
-
return wrapper(name, options, &block)
|
73
|
-
end
|
74
|
-
|
75
|
-
if options && options.keys != [:wrap_with]
|
76
|
-
ActiveSupport::Deprecation.warn "Passing :tag, :class and others to optional is deprecated. " \
|
77
|
-
"Please invoke b.optional #{name.inspect}, :wrap_with => #{options.inspect} instead."
|
78
|
-
options = { :wrap_with => options }
|
79
|
-
end
|
80
|
-
|
81
57
|
@options[name] = false
|
82
58
|
use(name, options, &block)
|
83
59
|
end
|
@@ -23,7 +23,7 @@ module SimpleForm
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def html_classes(input, options)
|
26
|
-
css = options[:wrapper_class] ? Array
|
26
|
+
css = options[:wrapper_class] ? Array(options[:wrapper_class]) : @defaults[:class]
|
27
27
|
css += SimpleForm.additional_classes_for(:wrapper) do
|
28
28
|
input.additional_classes + [input.input_class]
|
29
29
|
end
|
data/lib/simple_form/wrappers.rb
CHANGED
data/lib/simple_form.rb
CHANGED
@@ -18,6 +18,12 @@ module SimpleForm
|
|
18
18
|
autoload :Inputs
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.eager_load!
|
22
|
+
super
|
23
|
+
SimpleForm::Inputs.eager_load!
|
24
|
+
SimpleForm::Components.eager_load!
|
25
|
+
end
|
26
|
+
|
21
27
|
## CONFIGURATION OPTIONS
|
22
28
|
|
23
29
|
# Method used to tidy up errors.
|
@@ -34,11 +40,11 @@ module SimpleForm
|
|
34
40
|
|
35
41
|
# Series of attemps to detect a default label method for collection.
|
36
42
|
mattr_accessor :collection_label_methods
|
37
|
-
@@collection_label_methods = [
|
43
|
+
@@collection_label_methods = [:to_label, :name, :title, :to_s]
|
38
44
|
|
39
45
|
# Series of attemps to detect a default value method for collection.
|
40
46
|
mattr_accessor :collection_value_methods
|
41
|
-
@@collection_value_methods = [
|
47
|
+
@@collection_value_methods = [:id, :to_s]
|
42
48
|
|
43
49
|
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
|
44
50
|
mattr_accessor :collection_wrapper_tag
|
@@ -49,7 +55,7 @@ module SimpleForm
|
|
49
55
|
@@collection_wrapper_class = nil
|
50
56
|
|
51
57
|
# You can wrap each item in a collection of radio/check boxes with a tag,
|
52
|
-
# defaulting to
|
58
|
+
# defaulting to span. Please note that when using :boolean_style = :nested,
|
53
59
|
# SimpleForm will force this option to be a :label.
|
54
60
|
mattr_accessor :item_wrapper_tag
|
55
61
|
@@item_wrapper_tag = :span
|
@@ -62,25 +68,25 @@ module SimpleForm
|
|
62
68
|
mattr_accessor :label_text
|
63
69
|
@@label_text = lambda { |label, required| "#{required} #{label}" }
|
64
70
|
|
65
|
-
# You can define the class to
|
71
|
+
# You can define the class to be used on all labels. Defaults to none.
|
66
72
|
mattr_accessor :label_class
|
67
73
|
@@label_class = nil
|
68
74
|
|
69
75
|
# Define the way to render check boxes / radio buttons with labels.
|
70
|
-
# :
|
71
|
-
# :
|
76
|
+
# inline: input + label (default)
|
77
|
+
# nested: label > input
|
72
78
|
mattr_accessor :boolean_style
|
73
79
|
@@boolean_style = :inline
|
74
80
|
|
75
|
-
# You can define the class to
|
81
|
+
# You can define the class to be used on all forms. Default is simple_form.
|
76
82
|
mattr_accessor :form_class
|
77
83
|
@@form_class = :simple_form
|
78
84
|
|
79
|
-
# You can define which elements should obtain additional classes
|
85
|
+
# You can define which elements should obtain additional classes.
|
80
86
|
mattr_accessor :generate_additional_classes_for
|
81
87
|
@@generate_additional_classes_for = [:wrapper, :label, :input]
|
82
88
|
|
83
|
-
# Whether attributes are required by default
|
89
|
+
# Whether attributes are required by default or not.
|
84
90
|
mattr_accessor :required_by_default
|
85
91
|
@@required_by_default = true
|
86
92
|
|
@@ -90,7 +96,7 @@ module SimpleForm
|
|
90
96
|
|
91
97
|
# Collection of methods to detect if a file type was given.
|
92
98
|
mattr_accessor :file_methods
|
93
|
-
@@file_methods = [
|
99
|
+
@@file_methods = [:mounted_as, :file?, :public_filename]
|
94
100
|
|
95
101
|
# Custom mappings for input types. This should be a hash containing a regexp
|
96
102
|
# to match as key, and the input type that will be used when the field name
|
@@ -100,7 +106,7 @@ module SimpleForm
|
|
100
106
|
|
101
107
|
# Custom wrappers for input types. This should be a hash containing an input
|
102
108
|
# type as key and the wrapper that will be used for all inputs with specified type.
|
103
|
-
# e.g { :
|
109
|
+
# e.g { string: :string_wrapper, boolean: :boolean_wrapper }
|
104
110
|
mattr_accessor :wrapper_mappings
|
105
111
|
@@wrapper_mappings = nil
|
106
112
|
|
@@ -112,9 +118,9 @@ module SimpleForm
|
|
112
118
|
mattr_accessor :country_priority
|
113
119
|
@@country_priority = nil
|
114
120
|
|
115
|
-
# Maximum size allowed for inputs.
|
121
|
+
# DEPRECATED: Maximum size allowed for inputs.
|
116
122
|
mattr_accessor :default_input_size
|
117
|
-
@@default_input_size =
|
123
|
+
@@default_input_size = nil
|
118
124
|
|
119
125
|
# When off, do not use translations in labels. Disabling translation in
|
120
126
|
# hints and placeholders can be done manually in the wrapper API.
|
@@ -125,14 +131,26 @@ module SimpleForm
|
|
125
131
|
mattr_accessor :inputs_discovery
|
126
132
|
@@inputs_discovery = true
|
127
133
|
|
128
|
-
# Cache SimpleForm inputs discovery
|
134
|
+
# Cache SimpleForm inputs discovery.
|
129
135
|
mattr_accessor :cache_discovery
|
130
136
|
@@cache_discovery = defined?(Rails) && !Rails.env.development?
|
131
137
|
|
132
|
-
# Adds a class to each generated button, mostly for compatiblity
|
138
|
+
# Adds a class to each generated button, mostly for compatiblity.
|
133
139
|
mattr_accessor :button_class
|
134
140
|
@@button_class = 'button'
|
135
141
|
|
142
|
+
# Override the default ActiveModelHelper behaviour of wrapping the input.
|
143
|
+
# This gets taken care of semantically by adding an error class to the wrapper tag
|
144
|
+
# containing the input.
|
145
|
+
mattr_accessor :field_error_proc
|
146
|
+
@@field_error_proc = proc do |html_tag, instance_tag|
|
147
|
+
html_tag
|
148
|
+
end
|
149
|
+
|
150
|
+
# Adds a class to each generated inputs
|
151
|
+
mattr_accessor :input_class
|
152
|
+
@@input_class = nil
|
153
|
+
|
136
154
|
## WRAPPER CONFIGURATION
|
137
155
|
# The default wrapper to be used by the FormBuilder.
|
138
156
|
mattr_accessor :default_wrapper
|
@@ -168,7 +186,7 @@ module SimpleForm
|
|
168
186
|
SimpleForm::Wrappers::Root.new(builder.to_a, options)
|
169
187
|
end
|
170
188
|
|
171
|
-
wrappers :
|
189
|
+
wrappers class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b|
|
172
190
|
b.use :html5
|
173
191
|
|
174
192
|
b.use :min_max
|
@@ -178,47 +196,25 @@ module SimpleForm
|
|
178
196
|
b.optional :readonly
|
179
197
|
|
180
198
|
b.use :label_input
|
181
|
-
b.use :hint, :
|
182
|
-
b.use :error, :
|
199
|
+
b.use :hint, wrap_with: { tag: :span, class: :hint }
|
200
|
+
b.use :error, wrap_with: { tag: :span, class: :error }
|
183
201
|
end
|
184
202
|
|
185
|
-
|
186
|
-
|
187
|
-
DEPRECATED = %w(hint_tag hint_class error_tag error_class error_notification_id wrapper_tag wrapper_class wrapper_error_class components html5)
|
188
|
-
@@deprecated = []
|
189
|
-
|
190
|
-
DEPRECATED.each do |method|
|
191
|
-
class_eval "def self.#{method}=(*); @@deprecated << :#{method}=; end"
|
192
|
-
class_eval "def self.#{method}; deprecation_warn 'SimpleForm.#{method} is deprecated and has no effect'; end"
|
193
|
-
end
|
194
|
-
|
195
|
-
def self.translate=(value)
|
196
|
-
deprecation_warn "SimpleForm.translate= is disabled in favor of translate_labels="
|
197
|
-
self.translate_labels = value
|
198
|
-
end
|
199
|
-
|
200
|
-
def self.translate
|
201
|
-
deprecation_warn "SimpleForm.translate is disabled in favor of translate_labels"
|
202
|
-
self.translate_labels
|
203
|
+
def self.additional_classes_for(component)
|
204
|
+
generate_additional_classes_for.include?(component) ? yield : []
|
203
205
|
end
|
204
206
|
|
205
|
-
|
206
|
-
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] #{message}", caller
|
207
|
-
end
|
207
|
+
## SETUP
|
208
208
|
|
209
|
-
def self.
|
210
|
-
|
209
|
+
def self.default_input_size=(*)
|
210
|
+
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
|
211
211
|
end
|
212
212
|
|
213
213
|
# Default way to setup SimpleForm. Run rails generate simple_form:install
|
214
214
|
# to create a fresh initializer with all configuration values.
|
215
215
|
def self.setup
|
216
216
|
yield self
|
217
|
-
|
218
|
-
unless @@deprecated.empty?
|
219
|
-
raise "[SIMPLE FORM] Your SimpleForm initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
|
220
|
-
"Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
|
221
|
-
"Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
|
222
|
-
end
|
223
217
|
end
|
224
218
|
end
|
219
|
+
|
220
|
+
require 'simple_form/railtie' if defined?(Rails)
|