simple_form_awesome 2.2.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.
- data/CHANGELOG.md +327 -0
- data/MIT-LICENSE +20 -0
- data/README.md +25 -0
- data/lib/generators/simple_form/USAGE +3 -0
- data/lib/generators/simple_form/install_generator.rb +48 -0
- data/lib/generators/simple_form/templates/AUI_README +19 -0
- data/lib/generators/simple_form/templates/README +12 -0
- data/lib/generators/simple_form/templates/_form.html.erb +13 -0
- data/lib/generators/simple_form/templates/_form.html.haml +10 -0
- data/lib/generators/simple_form/templates/_form.html.slim +10 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +142 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form_aui.rb +21 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +45 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +26 -0
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +26 -0
- data/lib/simple_form/action_view_extensions/builder.rb +340 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +72 -0
- data/lib/simple_form/components/errors.rb +35 -0
- data/lib/simple_form/components/hints.rb +18 -0
- data/lib/simple_form/components/html5.rb +26 -0
- data/lib/simple_form/components/label_input.rb +15 -0
- data/lib/simple_form/components/labels.rb +79 -0
- data/lib/simple_form/components/maxlength.rb +41 -0
- data/lib/simple_form/components/min_max.rb +50 -0
- data/lib/simple_form/components/pattern.rb +34 -0
- data/lib/simple_form/components/placeholders.rb +16 -0
- data/lib/simple_form/components/readonly.rb +22 -0
- data/lib/simple_form/components.rb +20 -0
- data/lib/simple_form/core_ext/hash.rb +16 -0
- data/lib/simple_form/error_notification.rb +48 -0
- data/lib/simple_form/form_builder.rb +482 -0
- 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 +35 -0
- data/lib/simple_form/helpers/validators.rb +44 -0
- data/lib/simple_form/helpers.rb +12 -0
- data/lib/simple_form/i18n_cache.rb +22 -0
- data/lib/simple_form/inputs/aui_string_input.rb +10 -0
- data/lib/simple_form/inputs/base.rb +184 -0
- data/lib/simple_form/inputs/block_input.rb +14 -0
- data/lib/simple_form/inputs/boolean_input.rb +78 -0
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
- data/lib/simple_form/inputs/collection_input.rb +101 -0
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +63 -0
- data/lib/simple_form/inputs/collection_select_input.rb +14 -0
- data/lib/simple_form/inputs/date_time_input.rb +28 -0
- data/lib/simple_form/inputs/file_input.rb +9 -0
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
- data/lib/simple_form/inputs/hidden_input.rb +17 -0
- data/lib/simple_form/inputs/numeric_input.rb +24 -0
- data/lib/simple_form/inputs/password_input.rb +12 -0
- data/lib/simple_form/inputs/priority_input.rb +24 -0
- data/lib/simple_form/inputs/range_input.rb +14 -0
- data/lib/simple_form/inputs/string_input.rb +23 -0
- data/lib/simple_form/inputs/text_area_input.rb +12 -0
- data/lib/simple_form/inputs/text_input.rb +11 -0
- data/lib/simple_form/inputs.rb +23 -0
- data/lib/simple_form/map_type.rb +16 -0
- data/lib/simple_form/version.rb +3 -0
- data/lib/simple_form/wrappers/builder.rb +103 -0
- data/lib/simple_form/wrappers/many.rb +73 -0
- data/lib/simple_form/wrappers/root.rb +36 -0
- data/lib/simple_form/wrappers/single.rb +24 -0
- data/lib/simple_form/wrappers.rb +8 -0
- data/lib/simple_form.rb +221 -0
- data/test/action_view_extensions/builder_test.rb +583 -0
- data/test/action_view_extensions/form_helper_test.rb +143 -0
- data/test/components/label_test.rb +327 -0
- data/test/form_builder/association_test.rb +186 -0
- data/test/form_builder/button_test.rb +47 -0
- data/test/form_builder/error_notification_test.rb +79 -0
- data/test/form_builder/error_test.rb +121 -0
- data/test/form_builder/general_test.rb +402 -0
- data/test/form_builder/hint_test.rb +138 -0
- data/test/form_builder/input_field_test.rb +63 -0
- data/test/form_builder/label_test.rb +71 -0
- data/test/form_builder/wrapper_test.rb +203 -0
- data/test/generators/simple_form_generator_test.rb +42 -0
- data/test/inputs/boolean_input_test.rb +140 -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 +99 -0
- data/test/inputs/disabled_test.rb +78 -0
- data/test/inputs/discovery_test.rb +69 -0
- data/test/inputs/file_input_test.rb +16 -0
- data/test/inputs/general_test.rb +116 -0
- data/test/inputs/grouped_collection_select_input_test.rb +118 -0
- data/test/inputs/hidden_input_test.rb +30 -0
- data/test/inputs/numeric_input_test.rb +173 -0
- data/test/inputs/priority_input_test.rb +43 -0
- data/test/inputs/readonly_test.rb +101 -0
- data/test/inputs/required_test.rb +113 -0
- data/test/inputs/string_input_test.rb +146 -0
- data/test/inputs/text_input_test.rb +24 -0
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +27 -0
- data/test/support/misc_helpers.rb +138 -0
- data/test/support/mock_controller.rb +24 -0
- data/test/support/models.rb +216 -0
- data/test/test_helper.rb +95 -0
- metadata +217 -0
@@ -0,0 +1,138 @@
|
|
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 not modify the options hash' do
|
22
|
+
options = { :hint => 'Use with care...' }
|
23
|
+
with_hint_for @user, :name, options
|
24
|
+
assert_select 'span.hint', 'Use with care...'
|
25
|
+
assert_equal({ :hint => 'Use with care...' }, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'hint should be generated cleanly with optional text' do
|
29
|
+
with_hint_for @user, :name, :hint => 'Use with care...', :hint_tag => :span
|
30
|
+
assert_no_select 'span.hint[hint]'
|
31
|
+
assert_no_select 'span.hint[hint_tag]'
|
32
|
+
assert_no_select 'span.hint[hint_html]'
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'hint uses the current component tag set' do
|
36
|
+
with_hint_for @user, :name, :hint => 'Use with care...', :hint_tag => :p
|
37
|
+
assert_select 'p.hint', 'Use with care...'
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'hint should be able to pass html options' do
|
41
|
+
with_hint_for @user, :name, :hint => 'Yay!', :id => 'hint', :class => 'yay'
|
42
|
+
assert_select 'span#hint.hint.yay'
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'hint should be output as html_safe' do
|
46
|
+
with_hint_for @user, :name, :hint => '<b>Bold</b> and not...'
|
47
|
+
assert_select 'span.hint', 'Bold and not...'
|
48
|
+
end
|
49
|
+
|
50
|
+
# Without attribute name
|
51
|
+
|
52
|
+
test 'hint without attribute name' do
|
53
|
+
with_hint_for @validating_user, 'Hello World!'
|
54
|
+
assert_select 'span.hint', 'Hello World!'
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'hint without attribute name should generate component tag with a clean HTML' do
|
58
|
+
with_hint_for @validating_user, 'Hello World!'
|
59
|
+
assert_no_select 'span.hint[hint]'
|
60
|
+
assert_no_select 'span.hint[hint_html]'
|
61
|
+
end
|
62
|
+
|
63
|
+
test 'hint without attribute name uses the current component tag set' do
|
64
|
+
with_hint_for @user, 'Hello World!', :hint_tag => :p
|
65
|
+
assert_no_select 'p.hint[hint]'
|
66
|
+
assert_no_select 'p.hint[hint_html]'
|
67
|
+
assert_no_select 'p.hint[hint_tag]'
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'hint without attribute name should be able to pass html options' do
|
71
|
+
with_hint_for @user, 'Yay', :id => 'hint', :class => 'yay'
|
72
|
+
assert_select 'span#hint.hint.yay', 'Yay'
|
73
|
+
end
|
74
|
+
|
75
|
+
# I18n
|
76
|
+
|
77
|
+
test 'hint should use i18n based on model, action, and attribute to lookup translation' do
|
78
|
+
store_translations(:en, :simple_form => { :hints => { :user => {
|
79
|
+
:edit => { :name => 'Content of this input will be truncated...' }
|
80
|
+
} } }) do
|
81
|
+
with_hint_for @user, :name
|
82
|
+
assert_select 'span.hint', 'Content of this input will be truncated...'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
test 'hint should use i18n with model and attribute to lookup translation' do
|
87
|
+
store_translations(:en, :simple_form => { :hints => { :user => {
|
88
|
+
:name => 'Content of this input will be capitalized...'
|
89
|
+
} } }) do
|
90
|
+
with_hint_for @user, :name
|
91
|
+
assert_select 'span.hint', 'Content of this input will be capitalized...'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
test 'hint should use i18n under defaults namespace to lookup translation' do
|
96
|
+
store_translations(:en, :simple_form => {
|
97
|
+
:hints => {:defaults => {:name => 'Content of this input will be downcased...' } }
|
98
|
+
}) do
|
99
|
+
with_hint_for @user, :name
|
100
|
+
assert_select 'span.hint', 'Content of this input will be downcased...'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
test 'hint should use i18n with lookup for association name' do
|
105
|
+
store_translations(:en, :simple_form => { :hints => {
|
106
|
+
:user => { :company => 'My company!' }
|
107
|
+
} } ) do
|
108
|
+
with_hint_for @user, :company_id, :as => :string, :reflection => Association.new(Company, :company, {})
|
109
|
+
assert_select 'span.hint', /My company!/
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
test 'hint should output translations as html_safe' do
|
114
|
+
store_translations(:en, :simple_form => { :hints => { :user => {
|
115
|
+
:edit => { :name => '<b>This is bold</b> and this is not...' }
|
116
|
+
} } }) do
|
117
|
+
with_hint_for @user, :name
|
118
|
+
assert_select 'span.hint', 'This is bold and this is not...'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
# No object
|
124
|
+
|
125
|
+
test 'hint should generate properly when object is not present' do
|
126
|
+
with_hint_for :project, :name, :hint => 'Test without object'
|
127
|
+
assert_select 'span.hint', 'Test without object'
|
128
|
+
end
|
129
|
+
|
130
|
+
# Custom wrappers
|
131
|
+
|
132
|
+
test 'hint with custom wrappers works' do
|
133
|
+
swap_wrapper do
|
134
|
+
with_hint_for @user, :name, :hint => "can't be blank"
|
135
|
+
assert_select 'div.omg_hint', "can't be blank"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,63 @@
|
|
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 not modify the options hash' do
|
33
|
+
options = { :id => 'name_input', :class => 'name' }
|
34
|
+
|
35
|
+
with_concat_form_for(@user) do |f|
|
36
|
+
f.input_field :name, options
|
37
|
+
end
|
38
|
+
|
39
|
+
assert_select 'input.string.name#name_input'
|
40
|
+
assert_equal({ :id => 'name_input', :class => 'name' }, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
test 'builder input_field should generate an input tag with a clean HTML' do
|
45
|
+
with_concat_form_for(@user) do |f|
|
46
|
+
f.input_field :name, :as => :integer, :class => 'name'
|
47
|
+
end
|
48
|
+
|
49
|
+
assert_no_select 'input.integer[input_html]'
|
50
|
+
assert_no_select 'input.integer[as]'
|
51
|
+
end
|
52
|
+
|
53
|
+
test 'builder collection input_field should generate input tag with a clean HTML' do
|
54
|
+
with_concat_form_for(@user) do |f|
|
55
|
+
f.input_field :status, :collection => ['Open', 'Closed'], :class => 'status', :label_method => :to_s, :value_method => :to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
assert_no_select 'select.status[input_html]'
|
59
|
+
assert_no_select 'select.status[collection]'
|
60
|
+
assert_no_select 'select.status[label_method]'
|
61
|
+
assert_no_select 'select.status[value_method]'
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,71 @@
|
|
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 for the boolean attrbiute' do
|
17
|
+
with_label_for @user, :name, :as => :boolean
|
18
|
+
assert_select 'label.boolean[for=user_name]', /Name/
|
19
|
+
assert_no_select 'label[as=boolean]'
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'builder should generate a label componet tag with a clean HTML' do
|
23
|
+
with_label_for @user, :name
|
24
|
+
assert_no_select 'label.string[label_html]'
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'builder should add a required class to label if the attribute is required' do
|
28
|
+
with_label_for @validating_user, :name
|
29
|
+
assert_select 'label.string.required[for=validating_user_name]', /Name/
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'builder should allow passing options to label tag' do
|
33
|
+
with_label_for @user, :name, :label => 'My label', :id => 'name_label'
|
34
|
+
assert_select 'label.string#name_label', /My label/
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'builder label should generate label tag with clean HTML' do
|
38
|
+
with_label_for @user, :name, :label => 'My label', :required => true, :id => 'name_label'
|
39
|
+
assert_select 'label.string#name_label', /My label/
|
40
|
+
assert_no_select 'label[label]'
|
41
|
+
assert_no_select 'label[required]'
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'builder should not modify the options hash' do
|
45
|
+
options = { :label => 'My label', :id => 'name_label' }
|
46
|
+
with_label_for @user, :name, options
|
47
|
+
assert_select 'label.string#name_label', /My label/
|
48
|
+
assert_equal({ :label => 'My label', :id => 'name_label' }, options)
|
49
|
+
end
|
50
|
+
|
51
|
+
test 'builder should fallback to default label when string is given' do
|
52
|
+
with_label_for @user, :name, 'Nome do usuário'
|
53
|
+
assert_select 'label', 'Nome do usuário'
|
54
|
+
assert_no_select 'label.string'
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'builder should fallback to default label when block is given' do
|
58
|
+
with_label_for @user, :name do
|
59
|
+
'Nome do usuário'
|
60
|
+
end
|
61
|
+
assert_select 'label', 'Nome do usuário'
|
62
|
+
assert_no_select 'label.string'
|
63
|
+
end
|
64
|
+
|
65
|
+
test 'builder allows label order to be changed' do
|
66
|
+
swap SimpleForm, :label_text => lambda { |l, r| "#{l}:" } do
|
67
|
+
with_label_for @user, :age
|
68
|
+
assert_select 'label.integer[for=user_age]', "Age:"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,203 @@
|
|
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 the attribute name class' do
|
15
|
+
with_form_for @user, :name
|
16
|
+
assert_select 'div.user_name'
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'wrapper should add the attribute name class for nested forms' do
|
20
|
+
@user.company = Company.new(1, 'Empresa')
|
21
|
+
with_concat_form_for @user do |f|
|
22
|
+
concat(f.simple_fields_for(:company) do |company_form|
|
23
|
+
concat(company_form.input :name)
|
24
|
+
end)
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_select 'div.user_company_name'
|
28
|
+
end
|
29
|
+
|
30
|
+
test 'wrapper should add the association name class' do
|
31
|
+
with_form_for @user, :company
|
32
|
+
assert_select 'div.user_company'
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'wrapper should add error class for attribute with errors' do
|
36
|
+
with_form_for @user, :name
|
37
|
+
assert_select 'div.field_with_errors'
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'wrapper should add hint class for attribute with a hint' do
|
41
|
+
with_form_for @user, :name, :hint => 'hint'
|
42
|
+
assert_select 'div.field_with_hint'
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'wrapper should not have disabled class by default' do
|
46
|
+
with_form_for @user, :active
|
47
|
+
assert_no_select 'div.disabled'
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'wrapper should have disabled class when input is disabled' do
|
51
|
+
with_form_for @user, :active, :disabled => true
|
52
|
+
assert_select 'div.disabled'
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'wrapper should support no wrapping when wrapper is false' do
|
56
|
+
with_form_for @user, :name, :wrapper => false
|
57
|
+
assert_select 'form > label[for=user_name]'
|
58
|
+
assert_select 'form > input#user_name.string'
|
59
|
+
end
|
60
|
+
|
61
|
+
test 'wrapper should support no wrapping when wrapper tag is false' do
|
62
|
+
with_form_for @user, :name, :wrapper => custom_wrapper_without_top_level
|
63
|
+
assert_select 'form > label[for=user_name]'
|
64
|
+
assert_select 'form > input#user_name.string'
|
65
|
+
end
|
66
|
+
|
67
|
+
test 'wrapper should wrapping tag adds required/optional css classes' do
|
68
|
+
with_form_for @user, :name
|
69
|
+
assert_select 'form div.input.required.string'
|
70
|
+
|
71
|
+
with_form_for @user, :age, :required => false
|
72
|
+
assert_select 'form div.input.optional.integer'
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'wrapper should allow custom options to be given' do
|
76
|
+
with_form_for @user, :name, :wrapper_html => { :id => "super_cool", :class => 'yay' }
|
77
|
+
assert_select 'form #super_cool.required.string.yay'
|
78
|
+
end
|
79
|
+
|
80
|
+
test 'wrapper should allow tag to be given on demand' do
|
81
|
+
with_form_for @user, :name, :wrapper_tag => :b
|
82
|
+
assert_select 'form b.required.string'
|
83
|
+
end
|
84
|
+
|
85
|
+
test 'wrapper should allow wrapper class to be given on demand' do
|
86
|
+
with_form_for @user, :name, :wrapper_class => :wrapper
|
87
|
+
assert_select 'form div.wrapper.required.string'
|
88
|
+
end
|
89
|
+
|
90
|
+
test 'wrapper should skip additional classes when configured' do
|
91
|
+
swap SimpleForm, :generate_additional_classes_for => [:input, :label] do
|
92
|
+
with_form_for @user, :name, :wrapper_class => :wrapper
|
93
|
+
assert_select 'form div.wrapper'
|
94
|
+
assert_no_select 'div.required'
|
95
|
+
assert_no_select 'div.string'
|
96
|
+
assert_no_select 'div.user_name'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
test 'wrapper should not generate empty css class' do
|
101
|
+
swap SimpleForm, :generate_additional_classes_for => [:input, :label] do
|
102
|
+
swap_wrapper :default, custom_wrapper_without_class do
|
103
|
+
with_form_for @user, :name
|
104
|
+
assert_no_select 'div#custom_wrapper_without_class[class]'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Custom wrapper test
|
110
|
+
|
111
|
+
test 'custom wrappers works' do
|
112
|
+
swap_wrapper do
|
113
|
+
with_form_for @user, :name, :hint => "cool"
|
114
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
115
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
116
|
+
assert_no_select "section.custom_wrapper div.another_wrapper span.omg_error"
|
117
|
+
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
118
|
+
assert_select "section.custom_wrapper > div.omg_hint", "cool"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
test 'custom wrappers can be turned off' do
|
123
|
+
swap_wrapper do
|
124
|
+
with_form_for @user, :name, :another => false
|
125
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
126
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
127
|
+
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'custom wrappers on a form basis' do
|
132
|
+
swap_wrapper :another do
|
133
|
+
with_concat_form_for(@user) do |f|
|
134
|
+
f.input :name
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
138
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
139
|
+
|
140
|
+
with_concat_form_for(@user, :wrapper => :another) do |f|
|
141
|
+
f.input :name
|
142
|
+
end
|
143
|
+
|
144
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
145
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
test 'custom wrappers on input basis' do
|
150
|
+
swap_wrapper :another do
|
151
|
+
with_form_for @user, :name
|
152
|
+
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
153
|
+
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
154
|
+
output_buffer.replace ""
|
155
|
+
|
156
|
+
with_form_for @user, :name, :wrapper => :another
|
157
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
158
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
159
|
+
output_buffer.replace ""
|
160
|
+
end
|
161
|
+
|
162
|
+
with_form_for @user, :name, :wrapper => custom_wrapper
|
163
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
164
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
165
|
+
end
|
166
|
+
|
167
|
+
test 'access wrappers with indifferent access' do
|
168
|
+
swap_wrapper :another do
|
169
|
+
with_form_for @user, :name, :wrapper => "another"
|
170
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
171
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
test 'do not duplicate label classes for different inputs' do
|
176
|
+
swap_wrapper :default, self.custom_wrapper_with_label_html_option do
|
177
|
+
with_concat_form_for(@user) do |f|
|
178
|
+
concat f.input :name, :required => false
|
179
|
+
concat f.input :email, :as => :email, :required => true
|
180
|
+
end
|
181
|
+
|
182
|
+
assert_select "label.string.optional.extra-label-class[for='user_name']"
|
183
|
+
assert_select "label.email.required.extra-label-class[for='user_email']"
|
184
|
+
assert_no_select "label.string.optional.extra-label-class[for='user_email']"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
test 'raise error when wrapper not found' do
|
189
|
+
assert_raise SimpleForm::WrapperNotFound do
|
190
|
+
with_form_for @user, :name, :wrapper => :not_found
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
test 'use wrapper for specified in config mapping' do
|
195
|
+
swap_wrapper :another do
|
196
|
+
swap SimpleForm, :wrapper_mappings => { :string => :another } do
|
197
|
+
with_form_for @user, :name
|
198
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
199
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,42 @@
|
|
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',
|
23
|
+
/config\.default_wrapper = :default/, /config\.boolean_style = :nested/
|
24
|
+
assert_file 'config/initializers/simple_form_bootstrap.rb', /config\.wrappers :bootstrap/,
|
25
|
+
/config\.default_wrapper = :bootstrap/
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'generates the simple_form initializer with the foundation wrappers' do
|
29
|
+
run_generator %w(--foundation)
|
30
|
+
assert_file 'config/initializers/simple_form.rb',
|
31
|
+
/config\.default_wrapper = :default/, /config\.boolean_style = :nested/
|
32
|
+
assert_file 'config/initializers/simple_form_foundation.rb', /config\.wrappers :foundation/,
|
33
|
+
/config\.default_wrapper = :foundation/
|
34
|
+
end
|
35
|
+
|
36
|
+
%W(erb haml slim).each do |engine|
|
37
|
+
test "generates the scaffold template when using #{engine}" do
|
38
|
+
run_generator ['-e', engine]
|
39
|
+
assert_file "lib/templates/#{engine}/scaffold/_form.html.#{engine}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,140 @@
|
|
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 custom checked value' do
|
18
|
+
@user.action = 'on'
|
19
|
+
with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
|
20
|
+
assert_select 'input[type=checkbox][value=on][checked=checked]'
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'input uses custom unchecked value' do
|
24
|
+
@user.action = 'off'
|
25
|
+
with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
|
26
|
+
assert_select 'input[type=checkbox][value=on]'
|
27
|
+
assert_no_select 'input[checked=checked][value=on]'
|
28
|
+
end
|
29
|
+
|
30
|
+
test 'input generates hidden input with custom unchecked value' do
|
31
|
+
with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
|
32
|
+
assert_select 'input[type=hidden][value=off]'
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'input uses inline boolean style by default' do
|
36
|
+
with_input_for @user, :active, :boolean
|
37
|
+
assert_select 'input.boolean + label.boolean.optional'
|
38
|
+
assert_no_select 'label > input'
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'input allows changing default boolean style config to nested, generating a default label and a manual hidden field for checkbox' do
|
42
|
+
swap SimpleForm, :boolean_style => :nested do
|
43
|
+
with_input_for @user, :active, :boolean
|
44
|
+
assert_select 'label[for=user_active]', 'Active'
|
45
|
+
assert_select 'label.boolean > input.boolean'
|
46
|
+
assert_no_select 'input[type=checkbox] + label'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'input boolean with nested allows :inline_label' do
|
51
|
+
swap SimpleForm, :boolean_style => :nested do
|
52
|
+
with_input_for @user, :active, :boolean, :label => false, :inline_label => 'I am so inline.'
|
53
|
+
assert_select 'label.checkbox', :text => 'I am so inline.'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'input boolean with nested style creates an inline label using the default label text when inline_label option set to true' do
|
58
|
+
swap SimpleForm, :boolean_style => :nested do
|
59
|
+
with_input_for @user, :active, :boolean, :label => false, :inline_label => true
|
60
|
+
assert_select 'label.checkbox', :text => 'Active'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'input boolean with nested generates a manual hidden field for checkbox outside the label, to recreate Rails functionality with valid html5' do
|
65
|
+
swap SimpleForm, :boolean_style => :nested do
|
66
|
+
with_input_for @user, :active, :boolean
|
67
|
+
|
68
|
+
assert_select "input[type=hidden][name='user[active]'] + label.boolean > input.boolean"
|
69
|
+
assert_no_select 'input[type=checkbox] + label'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
test 'input boolean with nested generates a disabled hidden field for checkbox outside the label, if the field is disabled' do
|
74
|
+
swap SimpleForm, :boolean_style => :nested do
|
75
|
+
with_input_for @user, :active, :boolean, :disabled => true
|
76
|
+
|
77
|
+
assert_select "input[type=hidden][name='user[active]'][disabled] + label.boolean > input.boolean[disabled]"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'input accepts changing boolean style to nested through given options' do
|
82
|
+
with_input_for @user, :active, :boolean, :boolean_style => :nested
|
83
|
+
assert_select 'label[for=user_active]', 'Active'
|
84
|
+
assert_select 'label.boolean > input.boolean'
|
85
|
+
assert_no_select 'input[type=checkbox] + label'
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'input accepts changing boolean style to inline through given options, when default is nested' do
|
89
|
+
swap SimpleForm, :boolean_style => :nested do
|
90
|
+
with_input_for @user, :active, :boolean, :boolean_style => :inline
|
91
|
+
assert_select 'label[for=user_active]', 'Active'
|
92
|
+
assert_select 'input.boolean + label.boolean'
|
93
|
+
assert_no_select 'label > input'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
test 'input with nested style allows disabling label' do
|
98
|
+
swap SimpleForm, :boolean_style => :nested do
|
99
|
+
with_input_for @user, :active, :boolean, :label => false
|
100
|
+
assert_select 'input.boolean'
|
101
|
+
assert_no_select 'label.boolean'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
test 'input with nested style allows customizing input_html' do
|
106
|
+
swap SimpleForm, :boolean_style => :nested do
|
107
|
+
with_input_for @user, :active, :boolean, :input_html => { :name => 'active_user' }
|
108
|
+
assert_select "input[type=hidden][name=active_user] + label.boolean > input.boolean[name=active_user]"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
test 'input boolean works using :input only in wrapper config (no label_input)' do
|
113
|
+
swap_wrapper do
|
114
|
+
with_input_for @user, :active, :boolean
|
115
|
+
|
116
|
+
assert_select 'label.boolean + input[type=hidden] + input.boolean'
|
117
|
+
assert_no_select 'label.checkbox'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
test 'input boolean with nested style works using :input only in wrapper config (no label_input), adding the extra "checkbox" label wrapper' do
|
122
|
+
swap_wrapper do
|
123
|
+
swap SimpleForm, :boolean_style => :nested do
|
124
|
+
with_input_for @user, :active, :boolean
|
125
|
+
|
126
|
+
assert_select 'label.boolean + input[type=hidden] + label.checkbox > input.boolean'
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'input boolean with nested style works using :label_input in wrapper config, adding "checkbox" class to label' do
|
132
|
+
swap_wrapper :default, self.custom_wrapper_without_top_level do
|
133
|
+
swap SimpleForm, :boolean_style => :nested do
|
134
|
+
with_input_for @user, :active, :boolean
|
135
|
+
|
136
|
+
assert_select 'input[type=hidden] + label.boolean.checkbox > input.boolean'
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|