simple_form 3.0.1 → 3.1.0.rc2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -27
- data/MIT-LICENSE +1 -1
- data/README.md +179 -73
- data/lib/generators/simple_form/install_generator.rb +2 -2
- data/lib/generators/simple_form/templates/README +3 -4
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +25 -5
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +108 -24
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +1 -1
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/form_helper.rb +1 -1
- data/lib/simple_form/components/errors.rb +30 -2
- data/lib/simple_form/components/hints.rb +2 -2
- data/lib/simple_form/components/html5.rb +1 -1
- data/lib/simple_form/components/label_input.rb +20 -2
- data/lib/simple_form/components/labels.rb +10 -6
- data/lib/simple_form/components/maxlength.rb +1 -1
- data/lib/simple_form/components/min_max.rb +1 -1
- data/lib/simple_form/components/pattern.rb +1 -1
- data/lib/simple_form/components/placeholders.rb +2 -2
- data/lib/simple_form/components/readonly.rb +1 -1
- data/lib/simple_form/form_builder.rb +121 -72
- data/lib/simple_form/helpers.rb +5 -5
- data/lib/simple_form/inputs/base.rb +33 -11
- data/lib/simple_form/inputs/block_input.rb +1 -1
- data/lib/simple_form/inputs/boolean_input.rb +27 -14
- data/lib/simple_form/inputs/collection_input.rb +32 -9
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +6 -11
- data/lib/simple_form/inputs/collection_select_input.rb +4 -2
- data/lib/simple_form/inputs/date_time_input.rb +12 -2
- data/lib/simple_form/inputs/file_input.rb +4 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
- data/lib/simple_form/inputs/hidden_input.rb +4 -2
- data/lib/simple_form/inputs/numeric_input.rb +5 -4
- data/lib/simple_form/inputs/password_input.rb +4 -2
- data/lib/simple_form/inputs/priority_input.rb +4 -2
- data/lib/simple_form/inputs/range_input.rb +1 -1
- data/lib/simple_form/inputs/string_input.rb +4 -2
- data/lib/simple_form/inputs/text_input.rb +4 -2
- data/lib/simple_form/railtie.rb +7 -0
- data/lib/simple_form/tags.rb +8 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +6 -6
- data/lib/simple_form/wrappers/leaf.rb +28 -0
- data/lib/simple_form/wrappers/many.rb +6 -6
- data/lib/simple_form/wrappers/root.rb +1 -1
- data/lib/simple_form/wrappers/single.rb +5 -3
- data/lib/simple_form/wrappers.rb +1 -0
- data/lib/simple_form.rb +46 -6
- data/test/action_view_extensions/builder_test.rb +5 -5
- data/test/action_view_extensions/form_helper_test.rb +13 -13
- data/test/components/label_test.rb +36 -36
- data/test/form_builder/association_test.rb +21 -4
- data/test/form_builder/button_test.rb +5 -5
- data/test/form_builder/error_notification_test.rb +1 -1
- data/test/form_builder/error_test.rb +78 -17
- data/test/form_builder/general_test.rb +65 -60
- data/test/form_builder/hint_test.rb +15 -15
- data/test/form_builder/input_field_test.rb +37 -13
- data/test/form_builder/label_test.rb +44 -12
- data/test/form_builder/wrapper_test.rb +120 -19
- data/test/generators/simple_form_generator_test.rb +2 -2
- data/test/inputs/boolean_input_test.rb +54 -6
- data/test/inputs/collection_check_boxes_input_test.rb +63 -17
- data/test/inputs/collection_radio_buttons_input_test.rb +112 -36
- data/test/inputs/collection_select_input_test.rb +141 -36
- data/test/inputs/datetime_input_test.rb +116 -49
- data/test/inputs/disabled_test.rb +15 -15
- data/test/inputs/discovery_test.rb +56 -6
- data/test/inputs/file_input_test.rb +2 -2
- data/test/inputs/general_test.rb +20 -20
- data/test/inputs/grouped_collection_select_input_test.rb +38 -2
- data/test/inputs/hidden_input_test.rb +4 -4
- data/test/inputs/numeric_input_test.rb +24 -24
- data/test/inputs/priority_input_test.rb +7 -7
- data/test/inputs/readonly_test.rb +19 -19
- data/test/inputs/required_test.rb +13 -13
- data/test/inputs/string_input_test.rb +41 -21
- data/test/inputs/text_input_test.rb +4 -4
- data/test/simple_form_test.rb +8 -0
- data/test/support/discovery_inputs.rb +32 -2
- data/test/support/misc_helpers.rb +71 -5
- data/test/support/models.rb +50 -24
- data/test/test_helper.rb +2 -0
- metadata +23 -34
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Inputs
|
|
3
3
|
class CollectionSelectInput < CollectionInput
|
|
4
|
-
def input
|
|
4
|
+
def input(wrapper_options = nil)
|
|
5
5
|
label_method, value_method = detect_collection_methods
|
|
6
6
|
|
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
8
|
+
|
|
7
9
|
@builder.collection_select(
|
|
8
10
|
attribute_name, collection, value_method, label_method,
|
|
9
|
-
input_options,
|
|
11
|
+
input_options, merged_input_options
|
|
10
12
|
)
|
|
11
13
|
end
|
|
12
14
|
end
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Inputs
|
|
3
3
|
class DateTimeInput < Base
|
|
4
|
-
def input
|
|
5
|
-
|
|
4
|
+
def input(wrapper_options = nil)
|
|
5
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
6
|
+
|
|
7
|
+
if use_html5_inputs?
|
|
8
|
+
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options)
|
|
9
|
+
else
|
|
10
|
+
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options)
|
|
11
|
+
end
|
|
6
12
|
end
|
|
7
13
|
|
|
8
14
|
private
|
|
@@ -19,6 +25,10 @@ module SimpleForm
|
|
|
19
25
|
position = ActionView::Helpers::DateTimeSelector::POSITION[position]
|
|
20
26
|
"#{attribute_name}_#{position}i"
|
|
21
27
|
end
|
|
28
|
+
|
|
29
|
+
def use_html5_inputs?
|
|
30
|
+
input_options[:html5]
|
|
31
|
+
end
|
|
22
32
|
end
|
|
23
33
|
end
|
|
24
34
|
end
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Inputs
|
|
3
3
|
class FileInput < Base
|
|
4
|
-
def input
|
|
5
|
-
|
|
4
|
+
def input(wrapper_options = nil)
|
|
5
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
6
|
+
|
|
7
|
+
@builder.file_field(attribute_name, merged_input_options)
|
|
6
8
|
end
|
|
7
9
|
end
|
|
8
10
|
end
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Inputs
|
|
3
3
|
class GroupedCollectionSelectInput < CollectionInput
|
|
4
|
-
def input
|
|
4
|
+
def input(wrapper_options = nil)
|
|
5
5
|
label_method, value_method = detect_collection_methods
|
|
6
|
+
|
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
8
|
+
|
|
6
9
|
@builder.grouped_collection_select(attribute_name, grouped_collection,
|
|
7
10
|
group_method, group_label_method, value_method, label_method,
|
|
8
|
-
input_options,
|
|
11
|
+
input_options, merged_input_options)
|
|
9
12
|
end
|
|
10
13
|
|
|
11
14
|
private
|
|
@@ -19,7 +22,7 @@ module SimpleForm
|
|
|
19
22
|
|
|
20
23
|
# Sample collection
|
|
21
24
|
def collection
|
|
22
|
-
@collection ||= grouped_collection.
|
|
25
|
+
@collection ||= grouped_collection.map { |collection| collection.try(:send, group_method) }.detect(&:present?) || []
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
def group_method
|
|
@@ -36,6 +39,15 @@ module SimpleForm
|
|
|
36
39
|
|
|
37
40
|
label
|
|
38
41
|
end
|
|
42
|
+
|
|
43
|
+
def detect_method_from_class(collection_classes)
|
|
44
|
+
return {} if collection_classes.empty?
|
|
45
|
+
|
|
46
|
+
sample = collection_classes.first
|
|
47
|
+
|
|
48
|
+
{ label: SimpleForm.collection_label_methods.find { |m| sample.instance_methods.include?(m) },
|
|
49
|
+
value: SimpleForm.collection_value_methods.find { |m| sample.instance_methods.include?(m) } }
|
|
50
|
+
end
|
|
39
51
|
end
|
|
40
52
|
end
|
|
41
53
|
end
|
|
@@ -3,8 +3,10 @@ module SimpleForm
|
|
|
3
3
|
class HiddenInput < Base
|
|
4
4
|
disable :label, :errors, :hint, :required
|
|
5
5
|
|
|
6
|
-
def input
|
|
7
|
-
|
|
6
|
+
def input(wrapper_options = nil)
|
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
8
|
+
|
|
9
|
+
@builder.hidden_field(attribute_name, merged_input_options)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
private
|
|
@@ -3,16 +3,17 @@ module SimpleForm
|
|
|
3
3
|
class NumericInput < Base
|
|
4
4
|
enable :placeholder, :min_max
|
|
5
5
|
|
|
6
|
-
def input
|
|
6
|
+
def input(wrapper_options = nil)
|
|
7
7
|
input_html_classes.unshift("numeric")
|
|
8
8
|
if html5?
|
|
9
9
|
input_html_options[:type] ||= "number"
|
|
10
10
|
input_html_options[:step] ||= integer? ? 1 : "any"
|
|
11
11
|
end
|
|
12
|
-
@builder.text_field(attribute_name, input_html_options)
|
|
13
|
-
end
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
14
|
+
|
|
15
|
+
@builder.text_field(attribute_name, merged_input_options)
|
|
16
|
+
end
|
|
16
17
|
end
|
|
17
18
|
end
|
|
18
19
|
end
|
|
@@ -3,8 +3,10 @@ module SimpleForm
|
|
|
3
3
|
class PasswordInput < Base
|
|
4
4
|
enable :placeholder, :maxlength
|
|
5
5
|
|
|
6
|
-
def input
|
|
7
|
-
|
|
6
|
+
def input(wrapper_options = nil)
|
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
8
|
+
|
|
9
|
+
@builder.password_field(attribute_name, merged_input_options)
|
|
8
10
|
end
|
|
9
11
|
end
|
|
10
12
|
end
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Inputs
|
|
3
3
|
class PriorityInput < CollectionSelectInput
|
|
4
|
-
def input
|
|
4
|
+
def input(wrapper_options = nil)
|
|
5
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
6
|
+
|
|
5
7
|
@builder.send(:"#{input_type}_select", attribute_name, input_priority,
|
|
6
|
-
input_options,
|
|
8
|
+
input_options, merged_input_options)
|
|
7
9
|
end
|
|
8
10
|
|
|
9
11
|
def input_priority
|
|
@@ -3,13 +3,15 @@ module SimpleForm
|
|
|
3
3
|
class StringInput < Base
|
|
4
4
|
enable :placeholder, :maxlength, :pattern
|
|
5
5
|
|
|
6
|
-
def input
|
|
6
|
+
def input(wrapper_options = nil)
|
|
7
7
|
unless string?
|
|
8
8
|
input_html_classes.unshift("string")
|
|
9
9
|
input_html_options[:type] ||= input_type if html5?
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
13
|
+
|
|
14
|
+
@builder.text_field(attribute_name, merged_input_options)
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
private
|
|
@@ -3,8 +3,10 @@ module SimpleForm
|
|
|
3
3
|
class TextInput < Base
|
|
4
4
|
enable :placeholder, :maxlength
|
|
5
5
|
|
|
6
|
-
def input
|
|
7
|
-
|
|
6
|
+
def input(wrapper_options = nil)
|
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
8
|
+
|
|
9
|
+
@builder.text_area(attribute_name, merged_input_options)
|
|
8
10
|
end
|
|
9
11
|
end
|
|
10
12
|
end
|
data/lib/simple_form/railtie.rb
CHANGED
|
@@ -3,5 +3,12 @@ require 'rails/railtie'
|
|
|
3
3
|
module SimpleForm
|
|
4
4
|
class Railtie < Rails::Railtie
|
|
5
5
|
config.eager_load_namespaces << SimpleForm
|
|
6
|
+
|
|
7
|
+
config.after_initialize do
|
|
8
|
+
unless SimpleForm.configured?
|
|
9
|
+
warn '[Simple Form] Simple Form is not configured in the application and will use the default values.' +
|
|
10
|
+
' Use `rails generate simple_form:install` to generate the Simple Form configuration.'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
6
13
|
end
|
|
7
14
|
end
|
data/lib/simple_form/tags.rb
CHANGED
|
@@ -15,6 +15,14 @@ module SimpleForm
|
|
|
15
15
|
|
|
16
16
|
rendered_item = yield item, value, text, default_html_options.merge(additional_html_options)
|
|
17
17
|
|
|
18
|
+
if @options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested
|
|
19
|
+
label_options = {}
|
|
20
|
+
add_default_name_and_id_for_value(value, label_options)
|
|
21
|
+
label_options['for'] = label_options.delete('id')
|
|
22
|
+
label_options['class'] = @options[:item_label_class]
|
|
23
|
+
rendered_item = content_tag(:label, rendered_item, label_options)
|
|
24
|
+
end
|
|
25
|
+
|
|
18
26
|
item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item
|
|
19
27
|
end.join.html_safe
|
|
20
28
|
end
|
data/lib/simple_form/version.rb
CHANGED
|
@@ -45,20 +45,20 @@ module SimpleForm
|
|
|
45
45
|
@components = []
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
def use(name, options=
|
|
48
|
+
def use(name, options = {})
|
|
49
49
|
if options && wrapper = options[:wrap_with]
|
|
50
|
-
@components << Single.new(name, wrapper)
|
|
50
|
+
@components << Single.new(name, wrapper, options.except(:wrap_with))
|
|
51
51
|
else
|
|
52
|
-
@components << name
|
|
52
|
+
@components << Leaf.new(name, options)
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
def optional(name, options=
|
|
56
|
+
def optional(name, options = {}, &block)
|
|
57
57
|
@options[name] = false
|
|
58
|
-
use(name, options
|
|
58
|
+
use(name, options)
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
def wrapper(name, options=nil)
|
|
61
|
+
def wrapper(name, options = nil)
|
|
62
62
|
if block_given?
|
|
63
63
|
name, options = nil, name if name.is_a?(Hash)
|
|
64
64
|
builder = self.class.new(@options)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module SimpleForm
|
|
2
|
+
module Wrappers
|
|
3
|
+
class Leaf
|
|
4
|
+
attr_reader :namespace
|
|
5
|
+
|
|
6
|
+
def initialize(namespace, options = {})
|
|
7
|
+
@namespace = namespace
|
|
8
|
+
@options = options
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def render(input)
|
|
12
|
+
method = input.method(@namespace)
|
|
13
|
+
|
|
14
|
+
if method.arity == 0
|
|
15
|
+
ActiveSupport::Deprecation.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: @namespace })
|
|
16
|
+
|
|
17
|
+
method.call
|
|
18
|
+
else
|
|
19
|
+
method.call(@options)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def find(name)
|
|
24
|
+
self if @namespace == name
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module SimpleForm
|
|
2
2
|
module Wrappers
|
|
3
3
|
# A wrapper is an object that holds several components and render them.
|
|
4
|
-
# A component may
|
|
4
|
+
# A component may be any object that responds to `render`.
|
|
5
5
|
# This API allows inputs/components to be easily wrapped, removing the
|
|
6
6
|
# need to modify the code only to wrap input in an extra tag.
|
|
7
7
|
#
|
|
@@ -10,9 +10,8 @@ module SimpleForm
|
|
|
10
10
|
# on demand on input generation.
|
|
11
11
|
class Many
|
|
12
12
|
attr_reader :namespace, :defaults, :components
|
|
13
|
-
alias :to_sym :namespace
|
|
14
13
|
|
|
15
|
-
def initialize(namespace, components, defaults={})
|
|
14
|
+
def initialize(namespace, components, defaults = {})
|
|
16
15
|
@namespace = namespace
|
|
17
16
|
@components = components
|
|
18
17
|
@defaults = defaults
|
|
@@ -25,8 +24,8 @@ module SimpleForm
|
|
|
25
24
|
options = input.options
|
|
26
25
|
|
|
27
26
|
components.each do |component|
|
|
28
|
-
next if options[component] == false
|
|
29
|
-
rendered = component.
|
|
27
|
+
next if options[component.namespace] == false
|
|
28
|
+
rendered = component.render(input)
|
|
30
29
|
content.safe_concat rendered.to_s if rendered
|
|
31
30
|
end
|
|
32
31
|
|
|
@@ -51,6 +50,7 @@ module SimpleForm
|
|
|
51
50
|
|
|
52
51
|
def wrap(input, options, content)
|
|
53
52
|
return content if options[namespace] == false
|
|
53
|
+
return if defaults[:unless_blank] && content.empty?
|
|
54
54
|
|
|
55
55
|
tag = (namespace && options[:"#{namespace}_tag"]) || @defaults[:tag]
|
|
56
56
|
return content unless tag
|
|
@@ -62,7 +62,7 @@ module SimpleForm
|
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def html_options(options)
|
|
65
|
-
options[:"#{namespace}_html"] || {}
|
|
65
|
+
(@defaults[:html] || {}).merge(options[:"#{namespace}_html"] || {})
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def html_classes(input, options)
|
|
@@ -2,14 +2,16 @@ module SimpleForm
|
|
|
2
2
|
module Wrappers
|
|
3
3
|
# `Single` is an optimization for a wrapper that has only one component.
|
|
4
4
|
class Single < Many
|
|
5
|
-
def initialize(name, options={})
|
|
6
|
-
|
|
5
|
+
def initialize(name, wrapper_options = {}, options = {})
|
|
6
|
+
@component = Leaf.new(name, options)
|
|
7
|
+
|
|
8
|
+
super(name, [@component], wrapper_options)
|
|
7
9
|
end
|
|
8
10
|
|
|
9
11
|
def render(input)
|
|
10
12
|
options = input.options
|
|
11
13
|
if options[namespace] != false
|
|
12
|
-
content =
|
|
14
|
+
content = @component.render(input)
|
|
13
15
|
wrap(input, options, content) if content
|
|
14
16
|
end
|
|
15
17
|
end
|
data/lib/simple_form/wrappers.rb
CHANGED
data/lib/simple_form.rb
CHANGED
|
@@ -24,6 +24,24 @@ module SimpleForm
|
|
|
24
24
|
SimpleForm::Components.eager_load!
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
CUSTOM_INPUT_DEPRECATION_WARN = <<-WARN
|
|
28
|
+
%{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:
|
|
29
|
+
|
|
30
|
+
def %{name}
|
|
31
|
+
|
|
32
|
+
to
|
|
33
|
+
|
|
34
|
+
def %{name}(wrapper_options)
|
|
35
|
+
|
|
36
|
+
See https://github.com/plataformatec/simple_form/pull/997 for more information.
|
|
37
|
+
WARN
|
|
38
|
+
|
|
39
|
+
@@configured = false
|
|
40
|
+
|
|
41
|
+
def self.configured? #:nodoc:
|
|
42
|
+
@@configured
|
|
43
|
+
end
|
|
44
|
+
|
|
27
45
|
## CONFIGURATION OPTIONS
|
|
28
46
|
|
|
29
47
|
# Method used to tidy up errors.
|
|
@@ -66,7 +84,7 @@ module SimpleForm
|
|
|
66
84
|
|
|
67
85
|
# How the label text should be generated altogether with the required text.
|
|
68
86
|
mattr_accessor :label_text
|
|
69
|
-
@@label_text = lambda { |label, required| "#{required} #{label}" }
|
|
87
|
+
@@label_text = lambda { |label, required, explicit_label| "#{required} #{label}" }
|
|
70
88
|
|
|
71
89
|
# You can define the class to be used on all labels. Defaults to none.
|
|
72
90
|
mattr_accessor :label_class
|
|
@@ -107,9 +125,19 @@ module SimpleForm
|
|
|
107
125
|
# Custom wrappers for input types. This should be a hash containing an input
|
|
108
126
|
# type as key and the wrapper that will be used for all inputs with specified type.
|
|
109
127
|
# e.g { string: :string_wrapper, boolean: :boolean_wrapper }
|
|
128
|
+
# You can also set a wrapper mapping per form basis.
|
|
129
|
+
# e.g simple_form_for(@foo, wrapper_mappings: { check_boxes: :bootstrap_checkbox })
|
|
110
130
|
mattr_accessor :wrapper_mappings
|
|
111
131
|
@@wrapper_mappings = nil
|
|
112
132
|
|
|
133
|
+
# Namespaces where SimpleForm should look for custom input classes that override
|
|
134
|
+
# default inputs. Namespaces are given as string to allow lazy loading inputs.
|
|
135
|
+
# e.g. config.custom_inputs_namespaces << "CustomInputs"
|
|
136
|
+
# will try to find CustomInputs::NumericInput when an :integer
|
|
137
|
+
# field is called.
|
|
138
|
+
mattr_accessor :custom_inputs_namespaces
|
|
139
|
+
@@custom_inputs_namespaces = []
|
|
140
|
+
|
|
113
141
|
# Default priority for time_zone inputs.
|
|
114
142
|
mattr_accessor :time_zone_priority
|
|
115
143
|
@@time_zone_priority = nil
|
|
@@ -151,15 +179,26 @@ module SimpleForm
|
|
|
151
179
|
mattr_accessor :input_class
|
|
152
180
|
@@input_class = nil
|
|
153
181
|
|
|
182
|
+
# Defines if an input wrapper class should be included or not
|
|
183
|
+
mattr_accessor :include_default_input_wrapper_class
|
|
184
|
+
@@include_default_input_wrapper_class = true
|
|
185
|
+
|
|
186
|
+
# Define the default class of the input wrapper of the boolean input.
|
|
187
|
+
mattr_accessor :boolean_label_class
|
|
188
|
+
@@boolean_label_class = 'checkbox'
|
|
189
|
+
|
|
154
190
|
## WRAPPER CONFIGURATION
|
|
155
191
|
# The default wrapper to be used by the FormBuilder.
|
|
156
192
|
mattr_accessor :default_wrapper
|
|
157
193
|
@@default_wrapper = :default
|
|
158
|
-
@@wrappers = {}
|
|
194
|
+
@@wrappers = {} #:nodoc:
|
|
195
|
+
|
|
196
|
+
mattr_accessor :i18n_scope
|
|
197
|
+
@@i18n_scope = 'simple_form'
|
|
159
198
|
|
|
160
199
|
# Retrieves a given wrapper
|
|
161
200
|
def self.wrapper(name)
|
|
162
|
-
@@wrappers[name.
|
|
201
|
+
@@wrappers[name.to_s] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
|
|
163
202
|
end
|
|
164
203
|
|
|
165
204
|
# Raised when fails to find a given wrapper name
|
|
@@ -172,14 +211,14 @@ module SimpleForm
|
|
|
172
211
|
if block_given?
|
|
173
212
|
options = args.extract_options!
|
|
174
213
|
name = args.first || :default
|
|
175
|
-
@@wrappers[name.
|
|
214
|
+
@@wrappers[name.to_s] = build(options, &block)
|
|
176
215
|
else
|
|
177
216
|
@@wrappers
|
|
178
217
|
end
|
|
179
218
|
end
|
|
180
219
|
|
|
181
220
|
# Builds a new wrapper using SimpleForm::Wrappers::Builder.
|
|
182
|
-
def self.build(options={})
|
|
221
|
+
def self.build(options = {})
|
|
183
222
|
options[:tag] = :div if options[:tag].nil?
|
|
184
223
|
builder = SimpleForm::Wrappers::Builder.new(options)
|
|
185
224
|
yield builder
|
|
@@ -210,9 +249,10 @@ module SimpleForm
|
|
|
210
249
|
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
|
|
211
250
|
end
|
|
212
251
|
|
|
213
|
-
# Default way to setup
|
|
252
|
+
# Default way to setup Simple Form. Run rails generate simple_form:install
|
|
214
253
|
# to create a fresh initializer with all configuration values.
|
|
215
254
|
def self.setup
|
|
255
|
+
@@configured = true
|
|
216
256
|
yield self
|
|
217
257
|
end
|
|
218
258
|
end
|
|
@@ -8,13 +8,13 @@ class BuilderTest < ActionView::TestCase
|
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
|
|
11
|
+
def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
|
12
12
|
with_concat_form_for(object) do |f|
|
|
13
13
|
f.collection_radio_buttons attribute, collection, value_method, text_method, options, html_options, &block
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
|
|
17
|
+
def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
|
18
18
|
with_concat_form_for(object) do |f|
|
|
19
19
|
f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block
|
|
20
20
|
end
|
|
@@ -42,7 +42,7 @@ class BuilderTest < ActionView::TestCase
|
|
|
42
42
|
assert_select 'form label.collection_radio_buttons[for=user_active_no]', 'No'
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
test "collection radio
|
|
45
|
+
test "collection radio sanitizes collection values for labels correctly" do
|
|
46
46
|
with_collection_radio_buttons @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
|
|
47
47
|
assert_select 'label.collection_radio_buttons[for=user_name_099]', '$0.99'
|
|
48
48
|
assert_select 'label.collection_radio_buttons[for=user_name_199]', '$1.99'
|
|
@@ -289,7 +289,7 @@ class BuilderTest < ActionView::TestCase
|
|
|
289
289
|
assert_select 'form label.collection_check_boxes[for=user_active_no]', 'No'
|
|
290
290
|
end
|
|
291
291
|
|
|
292
|
-
test "collection check box
|
|
292
|
+
test "collection check box sanitizes collection values for labels correctly" do
|
|
293
293
|
with_collection_check_boxes @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
|
|
294
294
|
assert_select 'label.collection_check_boxes[for=user_name_099]', '$0.99'
|
|
295
295
|
assert_select 'label.collection_check_boxes[for=user_name_199]', '$1.99'
|
|
@@ -593,7 +593,7 @@ class BuilderTest < ActionView::TestCase
|
|
|
593
593
|
end
|
|
594
594
|
end
|
|
595
595
|
|
|
596
|
-
test "fields
|
|
596
|
+
test "fields inherits wrapper option from the parent form" do
|
|
597
597
|
swap_wrapper :another do
|
|
598
598
|
simple_form_for(:user, wrapper: :another) do |f|
|
|
599
599
|
f.simple_fields_for(:company) do |company|
|
|
@@ -8,17 +8,17 @@ class FormHelperTest < ActionView::TestCase
|
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
test 'SimpleForm
|
|
11
|
+
test 'SimpleForm adds default class to form' do
|
|
12
12
|
with_concat_form_for(:user)
|
|
13
13
|
assert_select 'form.simple_form'
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
test 'SimpleForm
|
|
16
|
+
test 'SimpleForm uses default browser validations by default' do
|
|
17
17
|
with_concat_form_for(:user)
|
|
18
18
|
assert_no_select 'form[novalidate]'
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
test 'SimpleForm
|
|
21
|
+
test 'SimpleForm does not use default browser validations if specified in the configuration options' do
|
|
22
22
|
swap SimpleForm, browser_validations: false do
|
|
23
23
|
with_concat_form_for(:user)
|
|
24
24
|
assert_select 'form[novalidate="novalidate"]'
|
|
@@ -37,54 +37,54 @@ class FormHelperTest < ActionView::TestCase
|
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
test 'SimpleForm
|
|
40
|
+
test 'SimpleForm adds object name as css class to form when object is not present' do
|
|
41
41
|
with_concat_form_for(:user, html: { novalidate: true })
|
|
42
42
|
assert_select 'form.simple_form.user'
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
test 'SimpleForm
|
|
45
|
+
test 'SimpleForm adds :as option as css class to form when object is not present' do
|
|
46
46
|
with_concat_form_for(:user, as: 'superuser')
|
|
47
47
|
assert_select 'form.simple_form.superuser'
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
test 'SimpleForm
|
|
50
|
+
test 'SimpleForm adds object class name with new prefix as css class to form if record is not persisted' do
|
|
51
51
|
@user.new_record!
|
|
52
52
|
with_concat_form_for(@user)
|
|
53
53
|
assert_select 'form.simple_form.new_user'
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
test 'SimpleForm
|
|
56
|
+
test 'SimpleForm adds :as option with new prefix as css class to form if record is not persisted' do
|
|
57
57
|
@user.new_record!
|
|
58
58
|
with_concat_form_for(@user, as: 'superuser')
|
|
59
59
|
assert_select 'form.simple_form.new_superuser'
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
test 'SimpleForm
|
|
62
|
+
test 'SimpleForm adds edit class prefix as css class to form if record is persisted' do
|
|
63
63
|
with_concat_form_for(@user)
|
|
64
64
|
assert_select 'form.simple_form.edit_user'
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
test 'SimpleForm
|
|
67
|
+
test 'SimpleForm adds :as options with edit prefix as css class to form if record is persisted' do
|
|
68
68
|
with_concat_form_for(@user, as: 'superuser')
|
|
69
69
|
assert_select 'form.simple_form.edit_superuser'
|
|
70
70
|
end
|
|
71
71
|
|
|
72
|
-
test 'SimpleForm
|
|
72
|
+
test 'SimpleForm adds last object name as css class to form when there is array of objects' do
|
|
73
73
|
with_concat_form_for([Company.new, @user])
|
|
74
74
|
assert_select 'form.simple_form.edit_user'
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
-
test 'SimpleForm
|
|
77
|
+
test 'SimpleForm does not add object class to form if css_class is specified' do
|
|
78
78
|
with_concat_form_for(:user, html: { class: nil })
|
|
79
79
|
assert_no_select 'form.user'
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
-
test 'SimpleForm
|
|
82
|
+
test 'SimpleForm adds custom class to form if css_class is specified' do
|
|
83
83
|
with_concat_form_for(:user, html: { class: 'my_class' })
|
|
84
84
|
assert_select 'form.my_class'
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
test '
|
|
87
|
+
test 'passes options to SimpleForm' do
|
|
88
88
|
with_concat_form_for(:user, url: '/account', html: { id: 'my_form' })
|
|
89
89
|
assert_select 'form#my_form'
|
|
90
90
|
assert_select 'form[action=/account]'
|