simple_form 1.5.2 → 2.0.0.rc
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.
- data/CHANGELOG.md +224 -0
- data/MIT-LICENSE +1 -1
- data/README.md +817 -0
- data/lib/generators/simple_form/install_generator.rb +15 -1
- data/lib/generators/simple_form/templates/README +12 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +173 -0
- data/lib/simple_form.rb +109 -43
- data/lib/simple_form/action_view_extensions/builder.rb +158 -53
- data/lib/simple_form/action_view_extensions/form_helper.rb +29 -22
- data/lib/simple_form/components.rb +11 -1
- data/lib/simple_form/components/errors.rb +6 -24
- data/lib/simple_form/components/hints.rb +7 -21
- data/lib/simple_form/components/html5.rb +26 -0
- data/lib/simple_form/components/labels.rb +15 -13
- data/lib/simple_form/components/maxlength.rb +41 -0
- data/lib/simple_form/components/min_max.rb +49 -0
- data/lib/simple_form/components/pattern.rb +34 -0
- data/lib/simple_form/components/placeholders.rb +5 -17
- data/lib/simple_form/components/readonly.rb +22 -0
- data/lib/simple_form/core_ext/hash.rb +16 -0
- data/lib/simple_form/error_notification.rb +8 -1
- data/lib/simple_form/form_builder.rb +86 -22
- data/lib/simple_form/helpers.rb +7 -4
- data/lib/simple_form/helpers/autofocus.rb +11 -0
- data/lib/simple_form/helpers/disabled.rb +15 -0
- data/lib/simple_form/helpers/readonly.rb +15 -0
- data/lib/simple_form/helpers/required.rb +7 -10
- data/lib/simple_form/helpers/validators.rb +4 -4
- data/lib/simple_form/inputs.rb +17 -13
- data/lib/simple_form/inputs/base.rb +50 -81
- data/lib/simple_form/inputs/boolean_input.rb +43 -4
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
- data/lib/simple_form/inputs/collection_input.rb +27 -13
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +69 -0
- data/lib/simple_form/inputs/collection_select_input.rb +14 -0
- data/lib/simple_form/inputs/date_time_input.rb +2 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
- data/lib/simple_form/inputs/hidden_input.rb +3 -6
- data/lib/simple_form/inputs/numeric_input.rb +3 -51
- data/lib/simple_form/inputs/password_input.rb +1 -2
- data/lib/simple_form/inputs/priority_input.rb +2 -2
- data/lib/simple_form/inputs/range_input.rb +1 -3
- data/lib/simple_form/inputs/string_input.rb +6 -8
- data/lib/simple_form/inputs/text_input.rb +1 -2
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers.rb +8 -0
- data/lib/simple_form/wrappers/builder.rb +75 -0
- data/lib/simple_form/wrappers/many.rb +68 -0
- data/lib/simple_form/wrappers/root.rb +34 -0
- data/lib/simple_form/wrappers/single.rb +18 -0
- data/test/action_view_extensions/builder_test.rb +195 -100
- data/test/action_view_extensions/form_helper_test.rb +24 -2
- data/test/components/label_test.rb +20 -5
- data/test/form_builder/association_test.rb +167 -0
- data/test/form_builder/button_test.rb +28 -0
- data/test/{error_notification_test.rb → form_builder/error_notification_test.rb} +2 -1
- data/test/form_builder/error_test.rb +101 -0
- data/test/form_builder/general_test.rb +348 -0
- data/test/form_builder/hint_test.rb +115 -0
- data/test/form_builder/input_field_test.rb +51 -0
- data/test/form_builder/label_test.rb +51 -0
- data/test/form_builder/wrapper_test.rb +140 -0
- data/test/generators/simple_form_generator_test.rb +32 -0
- data/test/inputs/boolean_input_test.rb +91 -0
- data/test/inputs/collection_check_boxes_input_test.rb +224 -0
- data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
- data/test/inputs/collection_select_input_test.rb +241 -0
- data/test/inputs/datetime_input_test.rb +85 -0
- data/test/inputs/disabled_test.rb +38 -0
- data/test/inputs/discovery_test.rb +61 -0
- data/test/inputs/file_input_test.rb +16 -0
- data/test/inputs/general_test.rb +69 -0
- data/test/inputs/grouped_collection_select_input_test.rb +109 -0
- data/test/inputs/hidden_input_test.rb +30 -0
- data/test/inputs/numeric_input_test.rb +167 -0
- data/test/inputs/priority_input_test.rb +43 -0
- data/test/inputs/readonly_test.rb +61 -0
- data/test/inputs/required_test.rb +113 -0
- data/test/inputs/string_input_test.rb +140 -0
- data/test/inputs/text_input_test.rb +24 -0
- data/test/{discovery_inputs.rb → support/discovery_inputs.rb} +0 -0
- data/test/support/misc_helpers.rb +48 -6
- data/test/support/mock_controller.rb +2 -2
- data/test/support/models.rb +20 -5
- data/test/test_helper.rb +5 -8
- metadata +123 -98
- data/.gitignore +0 -3
- data/.gitmodules +0 -3
- data/.travis.yml +0 -15
- data/CHANGELOG.rdoc +0 -159
- data/Gemfile +0 -9
- data/README.rdoc +0 -466
- data/Rakefile +0 -27
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +0 -93
- data/lib/simple_form/components/wrapper.rb +0 -38
- data/lib/simple_form/helpers/has_errors.rb +0 -15
- data/lib/simple_form/helpers/maxlength.rb +0 -24
- data/lib/simple_form/helpers/pattern.rb +0 -28
- data/simple_form.gemspec +0 -25
- data/test/components/error_test.rb +0 -56
- data/test/components/hint_test.rb +0 -74
- data/test/components/wrapper_test.rb +0 -63
- data/test/custom_components.rb +0 -7
- data/test/form_builder_test.rb +0 -930
- data/test/inputs_test.rb +0 -995
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
# Tests for f.hint
|
|
4
|
+
class HintTest < ActionView::TestCase
|
|
5
|
+
def with_hint_for(object, *args)
|
|
6
|
+
with_concat_form_for(object) do |f|
|
|
7
|
+
f.hint(*args)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
test 'hint should not be generated by default' do
|
|
12
|
+
with_hint_for @user, :name
|
|
13
|
+
assert_no_select 'span.hint'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test 'hint should be generated with optional text' do
|
|
17
|
+
with_hint_for @user, :name, :hint => 'Use with care...'
|
|
18
|
+
assert_select 'span.hint', 'Use with care...'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'hint should be generated cleanly with optional text' do
|
|
22
|
+
with_hint_for @user, :name, :hint => 'Use with care...'
|
|
23
|
+
assert_no_select 'span.hint[hint]'
|
|
24
|
+
assert_no_select 'span.hint[hint_html]'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'hint uses the current component tag set' do
|
|
28
|
+
with_hint_for @user, :name, :hint => 'Use with care...', :hint_tag => :p
|
|
29
|
+
assert_select 'p.hint', 'Use with care...'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'hint should be able to pass html options' do
|
|
33
|
+
with_hint_for @user, :name, :hint => 'Yay!', :id => 'hint', :class => 'yay'
|
|
34
|
+
assert_select 'span#hint.hint.yay'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Without attribute name
|
|
38
|
+
|
|
39
|
+
test 'hint without attribute name' do
|
|
40
|
+
with_hint_for @validating_user, 'Hello World!'
|
|
41
|
+
assert_select 'span.hint', 'Hello World!'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'hint without attribute name should generate component tag with a clean HTML' do
|
|
45
|
+
with_hint_for @validating_user, 'Hello World!'
|
|
46
|
+
assert_no_select 'span.hint[hint]'
|
|
47
|
+
assert_no_select 'span.hint[hint_html]'
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test 'hint without attribute name uses the current component tag set' do
|
|
51
|
+
with_hint_for @user, 'Hello World!', :hint_tag => :p
|
|
52
|
+
assert_no_select 'p.hint[hint]'
|
|
53
|
+
assert_no_select 'p.hint[hint_html]'
|
|
54
|
+
assert_no_select 'p.hint[hint_tag]'
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'hint without attribute name should be able to pass html options' do
|
|
58
|
+
with_hint_for @user, 'Yay', :id => 'hint', :class => 'yay'
|
|
59
|
+
assert_select 'span#hint.hint.yay', 'Yay'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# I18n
|
|
63
|
+
|
|
64
|
+
test 'hint should use i18n based on model, action, and attribute to lookup translation' do
|
|
65
|
+
store_translations(:en, :simple_form => { :hints => { :user => {
|
|
66
|
+
:edit => { :name => 'Content of this input will be truncated...' }
|
|
67
|
+
} } }) do
|
|
68
|
+
with_hint_for @user, :name
|
|
69
|
+
assert_select 'span.hint', 'Content of this input will be truncated...'
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
test 'hint should use i18n with model and attribute to lookup translation' do
|
|
74
|
+
store_translations(:en, :simple_form => { :hints => { :user => {
|
|
75
|
+
:name => 'Content of this input will be capitalized...'
|
|
76
|
+
} } }) do
|
|
77
|
+
with_hint_for @user, :name
|
|
78
|
+
assert_select 'span.hint', 'Content of this input will be capitalized...'
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'hint should use i18n under defaults namespace to lookup translation' do
|
|
83
|
+
store_translations(:en, :simple_form => {
|
|
84
|
+
:hints => {:defaults => {:name => 'Content of this input will be downcased...' } }
|
|
85
|
+
}) do
|
|
86
|
+
with_hint_for @user, :name
|
|
87
|
+
assert_select 'span.hint', 'Content of this input will be downcased...'
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test 'hint should use i18n with lookup for association name' do
|
|
92
|
+
store_translations(:en, :simple_form => { :hints => {
|
|
93
|
+
:user => { :company => 'My company!' }
|
|
94
|
+
} } ) do
|
|
95
|
+
with_hint_for @user, :company_id, :as => :string, :reflection => Association.new(Company, :company, {})
|
|
96
|
+
assert_select 'span.hint', /My company!/
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# No object
|
|
101
|
+
|
|
102
|
+
test 'hint should generate properly when object is not present' do
|
|
103
|
+
with_hint_for :project, :name, :hint => 'Test without object'
|
|
104
|
+
assert_select 'span.hint', 'Test without object'
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Custom wrappers
|
|
108
|
+
|
|
109
|
+
test 'hint with custom wrappers works' do
|
|
110
|
+
swap_wrapper do
|
|
111
|
+
with_hint_for @user, :name, :hint => "can't be blank"
|
|
112
|
+
assert_select 'span.omg_hint', "can't be blank"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
# Tests for f.input_field
|
|
4
|
+
class InputFieldTest < ActionView::TestCase
|
|
5
|
+
test "builder input_field should only render the input tag, nothing else" do
|
|
6
|
+
with_concat_form_for(@user) do |f|
|
|
7
|
+
f.input_field :name
|
|
8
|
+
end
|
|
9
|
+
assert_select 'form > input.required.string'
|
|
10
|
+
assert_no_select 'div.string'
|
|
11
|
+
assert_no_select 'label'
|
|
12
|
+
assert_no_select '.hint'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
test 'builder input_field should allow overriding default input type' do
|
|
16
|
+
with_concat_form_for(@user) do |f|
|
|
17
|
+
f.input_field :name, :as => :text
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
assert_no_select 'input#user_name'
|
|
21
|
+
assert_select 'textarea#user_name.text'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
test 'builder input_field should allow passing options to input tag' do
|
|
25
|
+
with_concat_form_for(@user) do |f|
|
|
26
|
+
f.input_field :name, :id => 'name_input', :class => 'name'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
assert_select 'input.string.name#name_input'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'builder input_field should generate an input tag with a clean HTML' do
|
|
33
|
+
with_concat_form_for(@user) do |f|
|
|
34
|
+
f.input_field :name, :as => :integer, :class => 'name'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
assert_no_select 'input.integer[input_html]'
|
|
38
|
+
assert_no_select 'input.integer[as]'
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'builder collection input_field should generate input tag with a clean HTML' do
|
|
42
|
+
with_concat_form_for(@user) do |f|
|
|
43
|
+
f.input_field :status, :collection => ['Open', 'Closed'], :class => 'status', :label_method => :to_s, :value_method => :to_s
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
assert_no_select 'select.status[input_html]'
|
|
47
|
+
assert_no_select 'select.status[collection]'
|
|
48
|
+
assert_no_select 'select.status[label_method]'
|
|
49
|
+
assert_no_select 'select.status[value_method]'
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require 'test_helper'
|
|
3
|
+
|
|
4
|
+
class LabelTest < ActionView::TestCase
|
|
5
|
+
def with_label_for(object, *args, &block)
|
|
6
|
+
with_concat_form_for(object) do |f|
|
|
7
|
+
f.label(*args, &block)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
test 'builder should generate a label for the attribute' do
|
|
12
|
+
with_label_for @user, :name
|
|
13
|
+
assert_select 'label.string[for=user_name]', /Name/
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test 'builder should generate a label componet tag with a clean HTML' do
|
|
17
|
+
with_label_for @user, :name
|
|
18
|
+
assert_no_select 'label.string[label_html]'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'builder should add a required class to label if the attribute is required' do
|
|
22
|
+
with_label_for @validating_user, :name
|
|
23
|
+
assert_select 'label.string.required[for=validating_user_name]', /Name/
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
test 'builder should allow passing options to label tag' do
|
|
27
|
+
with_label_for @user, :name, :label => 'My label', :id => 'name_label'
|
|
28
|
+
assert_select 'label.string#name_label', /My label/
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test 'builder should fallback to default label when string is given' do
|
|
32
|
+
with_label_for @user, :name, 'Nome do usuário'
|
|
33
|
+
assert_select 'label', 'Nome do usuário'
|
|
34
|
+
assert_no_select 'label.string'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'builder should fallback to default label when block is given' do
|
|
38
|
+
with_label_for @user, :name do
|
|
39
|
+
'Nome do usuário'
|
|
40
|
+
end
|
|
41
|
+
assert_select 'label', 'Nome do usuário'
|
|
42
|
+
assert_no_select 'label.string'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
test 'builder allows label order to be changed' do
|
|
46
|
+
swap SimpleForm, :label_text => lambda { |l, r| "#{l}:" } do
|
|
47
|
+
with_label_for @user, :age
|
|
48
|
+
assert_select 'label.integer[for=user_age]', "Age:"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class WrapperTest < ActionView::TestCase
|
|
4
|
+
test 'wrapper should not have error class for attribute without errors' do
|
|
5
|
+
with_form_for @user, :active
|
|
6
|
+
assert_no_select 'div.field_with_errors'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
test 'wrapper should not have error class when object is not present' do
|
|
10
|
+
with_form_for :project, :name
|
|
11
|
+
assert_no_select 'div.field_with_errors'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'wrapper should add error class for attribute with errors' do
|
|
15
|
+
with_form_for @user, :name
|
|
16
|
+
assert_select 'div.field_with_errors'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'wrapper should add hint class for attribute with a hint' do
|
|
20
|
+
with_form_for @user, :name, :hint => 'hint'
|
|
21
|
+
assert_select 'div.field_with_hint'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
test 'wrapper should not have disabled class by default' do
|
|
25
|
+
with_form_for @user, :active
|
|
26
|
+
assert_no_select 'div.disabled'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'wrapper should have disabled class when input is disabled' do
|
|
30
|
+
with_form_for @user, :active, :disabled => true
|
|
31
|
+
assert_select 'div.disabled'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test 'wrapper should support no wrapping when wrapper is false' do
|
|
35
|
+
with_form_for @user, :name, :wrapper => false
|
|
36
|
+
assert_select 'form > label[for=user_name]'
|
|
37
|
+
assert_select 'form > input#user_name.string'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'wrapper should support no wrapping when wrapper tag is false' do
|
|
41
|
+
with_form_for @user, :name, :wrapper => custom_wrapper_without_top_level
|
|
42
|
+
assert_select 'form > label[for=user_name]'
|
|
43
|
+
assert_select 'form > input#user_name.string'
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test 'wrapper should wrapping tag adds required/optional css classes' do
|
|
47
|
+
with_form_for @user, :name
|
|
48
|
+
assert_select 'form div.input.required.string'
|
|
49
|
+
|
|
50
|
+
with_form_for @user, :age, :required => false
|
|
51
|
+
assert_select 'form div.input.optional.integer'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test 'wrapper should allow custom options to be given' do
|
|
55
|
+
with_form_for @user, :name, :wrapper_html => { :id => "super_cool", :class => 'yay' }
|
|
56
|
+
assert_select 'form #super_cool.required.string.yay'
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
test 'wrapper should allow tag to be given on demand' do
|
|
60
|
+
with_form_for @user, :name, :wrapper_tag => :b
|
|
61
|
+
assert_select 'form b.required.string'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
test 'wrapper should allow wrapper class to be given on demand' do
|
|
65
|
+
with_form_for @user, :name, :wrapper_class => :wrapper
|
|
66
|
+
assert_select 'form div.wrapper.required.string'
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Custom wrapper test
|
|
70
|
+
|
|
71
|
+
test 'custom wrappers works' do
|
|
72
|
+
swap_wrapper do
|
|
73
|
+
with_form_for @user, :name, :hint => "cool"
|
|
74
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
75
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
76
|
+
assert_no_select "section.custom_wrapper div.another_wrapper span.omg_error"
|
|
77
|
+
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
|
78
|
+
assert_select "section.custom_wrapper > span.omg_hint", "cool"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'custom wrappers can be turned off' do
|
|
83
|
+
swap_wrapper do
|
|
84
|
+
with_form_for @user, :name, :another => false
|
|
85
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
86
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
|
87
|
+
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test 'custom wrappers on a form basis' do
|
|
92
|
+
swap_wrapper :another do
|
|
93
|
+
concat simple_form_for(@user) { |f|
|
|
94
|
+
f.input :name
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
98
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
|
99
|
+
|
|
100
|
+
concat simple_form_for(@user, :wrapper => :another) { |f|
|
|
101
|
+
f.input :name
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
105
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
test 'custom wrappers on input basis' do
|
|
110
|
+
swap_wrapper :another do
|
|
111
|
+
with_form_for @user, :name
|
|
112
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
113
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
|
114
|
+
output_buffer.replace ""
|
|
115
|
+
|
|
116
|
+
with_form_for @user, :name, :wrapper => :another
|
|
117
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
118
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
119
|
+
output_buffer.replace ""
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
with_form_for @user, :name, :wrapper => custom_wrapper
|
|
123
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
124
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
test 'access wrappers with indifferent access' do
|
|
128
|
+
swap_wrapper :another do
|
|
129
|
+
with_form_for @user, :name, :wrapper => "another"
|
|
130
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
131
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
test 'raise error when wrapper not found' do
|
|
136
|
+
assert_raise SimpleForm::WrapperNotFound do
|
|
137
|
+
with_form_for @user, :name, :wrapper => :not_found
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class SimpleFormGeneratorTest < Rails::Generators::TestCase
|
|
4
|
+
tests SimpleForm::Generators::InstallGenerator
|
|
5
|
+
destination File.expand_path('../../tmp', __FILE__)
|
|
6
|
+
setup :prepare_destination
|
|
7
|
+
teardown { rm_rf(destination_root) }
|
|
8
|
+
|
|
9
|
+
test 'generates example locale file' do
|
|
10
|
+
run_generator
|
|
11
|
+
assert_file 'config/locales/simple_form.en.yml'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'generates the simple_form initializer' do
|
|
15
|
+
run_generator
|
|
16
|
+
assert_file 'config/initializers/simple_form.rb',
|
|
17
|
+
/config\.default_wrapper = :default/, /config\.boolean_style = :nested/
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
test 'generates the simple_form initializer with the bootstrap wrappers' do
|
|
21
|
+
run_generator %w(--bootstrap)
|
|
22
|
+
assert_file 'config/initializers/simple_form.rb', /config\.wrappers :bootstrap/,
|
|
23
|
+
/config\.default_wrapper = :bootstrap/
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
%W(erb haml slim).each do |engine|
|
|
27
|
+
test "generates the scaffold template when using #{engine}" do
|
|
28
|
+
run_generator ['-e', engine]
|
|
29
|
+
assert_file "lib/templates/#{engine}/scaffold/_form.html.#{engine}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require 'test_helper'
|
|
3
|
+
|
|
4
|
+
class BooleanInputTest < ActionView::TestCase
|
|
5
|
+
test 'input should generate a checkbox by default for boolean attributes' do
|
|
6
|
+
with_input_for @user, :active, :boolean
|
|
7
|
+
assert_select 'input[type=checkbox].boolean#user_active'
|
|
8
|
+
assert_select 'label.boolean.optional', 'Active'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
test 'input does not generate the label with the checkbox when label option is false' do
|
|
12
|
+
with_input_for @user, :active, :boolean, :label => false
|
|
13
|
+
assert_select 'input[type=checkbox].boolean#user_active'
|
|
14
|
+
assert_no_select 'label'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test 'input uses inline boolean style by default' do
|
|
18
|
+
with_input_for @user, :active, :boolean
|
|
19
|
+
assert_select 'input.boolean + label.boolean.optional'
|
|
20
|
+
assert_no_select 'label > input'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'input allows changing default boolean style config to nested, generating a default label and a manual hidden field for checkbox' do
|
|
24
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
25
|
+
with_input_for @user, :active, :boolean
|
|
26
|
+
assert_select 'label[for=user_active]', 'Active'
|
|
27
|
+
assert_select 'label.boolean > input.boolean'
|
|
28
|
+
assert_no_select 'input[type=checkbox] + label'
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'input boolean with nested generates a manual hidden field for checkbox outside the label, to recreate Rails functionality with valid html5' do
|
|
33
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
34
|
+
with_input_for @user, :active, :boolean
|
|
35
|
+
|
|
36
|
+
assert_select "input[type=hidden][name='user[active]'] + label.boolean > input.boolean"
|
|
37
|
+
assert_no_select 'input[type=checkbox] + label'
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'input boolean with nested generates a disabled hidden field for checkbox outside the label, if the field is disabled' do
|
|
42
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
43
|
+
with_input_for @user, :active, :boolean, :disabled => true
|
|
44
|
+
|
|
45
|
+
assert_select "input[type=hidden][name='user[active]'][disabled] + label.boolean > input.boolean[disabled]"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
test 'input accepts changing boolean style to nested through given options' do
|
|
50
|
+
with_input_for @user, :active, :boolean, :boolean_style => :nested
|
|
51
|
+
assert_select 'label[for=user_active]', 'Active'
|
|
52
|
+
assert_select 'label.boolean > input.boolean'
|
|
53
|
+
assert_no_select 'input[type=checkbox] + label'
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test 'input accepts changing boolean style to inline through given options, when default is nested' do
|
|
57
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
58
|
+
with_input_for @user, :active, :boolean, :boolean_style => :inline
|
|
59
|
+
assert_select 'label[for=user_active]', 'Active'
|
|
60
|
+
assert_select 'input.boolean + label.boolean'
|
|
61
|
+
assert_no_select 'label > input'
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'input with nested style allows disabling label' do
|
|
66
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
67
|
+
with_input_for @user, :active, :boolean, :label => false
|
|
68
|
+
assert_select 'input.boolean'
|
|
69
|
+
assert_no_select 'label.boolean'
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
test 'input boolean works using :input only in wrapper config (no label_input)' do
|
|
74
|
+
swap_wrapper do
|
|
75
|
+
with_input_for @user, :active, :boolean
|
|
76
|
+
|
|
77
|
+
assert_select 'label.boolean + input[type=hidden] + input.boolean'
|
|
78
|
+
assert_no_select 'label.checkbox'
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'input boolean with nested style works using :input only in wrapper config (no label_input), adding the extra "checkbox" label wrapper' do
|
|
83
|
+
swap_wrapper do
|
|
84
|
+
swap SimpleForm, :boolean_style => :nested do
|
|
85
|
+
with_input_for @user, :active, :boolean
|
|
86
|
+
|
|
87
|
+
assert_select 'label.boolean + input[type=hidden] + label.checkbox > input.boolean'
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|