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,47 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class ButtonTest < ActionView::TestCase
|
5
|
+
def with_button_for(object, *args)
|
6
|
+
with_concat_form_for(object) do |f|
|
7
|
+
f.button(*args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'builder should create buttons' do
|
12
|
+
with_button_for :post, :submit
|
13
|
+
assert_select 'form input.button[type=submit][value=Save Post]'
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'builder should create buttons with options' do
|
17
|
+
with_button_for :post, :submit, :class => 'my_button'
|
18
|
+
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'builder should not modify the options hash' do
|
22
|
+
options = { :class => 'my_button' }
|
23
|
+
with_button_for :post, :submit, options
|
24
|
+
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
|
25
|
+
assert_equal({ :class => 'my_button' }, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'builder should create buttons for records' do
|
29
|
+
@user.new_record!
|
30
|
+
with_button_for @user, :submit
|
31
|
+
assert_select 'form input.button[type=submit][value=Create User]'
|
32
|
+
end
|
33
|
+
|
34
|
+
test "builder should use the default class from the configuration" do
|
35
|
+
swap SimpleForm, :button_class => 'btn' do
|
36
|
+
with_button_for :post, :submit
|
37
|
+
assert_select 'form input.btn[type=submit][value=Save Post]'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
if ActionView::Helpers::FormBuilder.method_defined?(:button)
|
42
|
+
test "allows to use Rails button helper when available" do
|
43
|
+
with_button_for :post, :button, 'Save!'
|
44
|
+
assert_select 'form button.button[type=submit]', 'Save!'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
# Tests for f.error_notification
|
5
|
+
class ErrorNotificationTest < ActionView::TestCase
|
6
|
+
def with_error_notification_for(object, options={}, &block)
|
7
|
+
with_concat_form_for(object) do |f|
|
8
|
+
f.error_notification(options)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'error notification is not generated when the object has no error' do
|
13
|
+
assert @validating_user.valid?
|
14
|
+
|
15
|
+
with_error_notification_for @validating_user
|
16
|
+
assert_no_select 'p.error_notification'
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'error notification is not generated for forms without objects' do
|
20
|
+
with_error_notification_for :user
|
21
|
+
assert_no_select 'p.error_notification'
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'error notification is generated when the object has some error' do
|
25
|
+
with_error_notification_for @user
|
26
|
+
assert_select 'p.error_notification', 'Please review the problems below:'
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'error notification uses I18n based on model to generate the notification message' do
|
30
|
+
store_translations(:en, :simple_form => { :error_notification => { :user =>
|
31
|
+
'Alguns erros foram encontrados para o usuário:'
|
32
|
+
} }) do
|
33
|
+
with_error_notification_for @user
|
34
|
+
assert_select 'p.error_notification', 'Alguns erros foram encontrados para o usuário:'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'error notification uses I18n fallbacking to default message' do
|
39
|
+
store_translations(:en, :simple_form => { :error_notification => {
|
40
|
+
:default_message => 'Opa! Alguns erros foram encontrados, poderia verificar?'
|
41
|
+
} }) do
|
42
|
+
with_error_notification_for @user
|
43
|
+
assert_select 'p.error_notification', 'Opa! Alguns erros foram encontrados, poderia verificar?'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test 'error notification allows passing the notification message' do
|
48
|
+
with_error_notification_for @user, :message => 'Erro encontrado ao criar usuario'
|
49
|
+
assert_select 'p.error_notification', 'Erro encontrado ao criar usuario'
|
50
|
+
end
|
51
|
+
|
52
|
+
test 'error notification accepts other html options' do
|
53
|
+
with_error_notification_for @user, :id => 'user_error_message', :class => 'form_error'
|
54
|
+
assert_select 'p#user_error_message.form_error.error_notification'
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'error notification allows configuring the wrapper element' do
|
58
|
+
swap SimpleForm, :error_notification_tag => :div do
|
59
|
+
with_error_notification_for @user
|
60
|
+
assert_select 'div.error_notification'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'error notification can contain HTML tags' do
|
65
|
+
with_error_notification_for @user, :message => 'Erro encontrado ao criar <b>usuário</b>'
|
66
|
+
assert_select 'p.error_notification', 'Erro encontrado ao criar usuário'
|
67
|
+
assert_select 'p.error_notification b', 'usuário'
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'error notification uses I18n based on model to generate the notification message and accepts HTML' do
|
71
|
+
store_translations(:en, :simple_form => { :error_notification => { :user =>
|
72
|
+
'Alguns erros foram encontrados para o <b>usuário</b>:'
|
73
|
+
} }) do
|
74
|
+
with_error_notification_for @user
|
75
|
+
assert_select 'p.error_notification', 'Alguns erros foram encontrados para o usuário:'
|
76
|
+
assert_select 'p.error_notification b', 'usuário'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# Tests for f.error and f.full_error
|
4
|
+
class ErrorTest < ActionView::TestCase
|
5
|
+
def with_error_for(object, *args)
|
6
|
+
with_concat_form_for(object) do |f|
|
7
|
+
f.error(*args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def with_full_error_for(object, *args)
|
12
|
+
with_concat_form_for(object) do |f|
|
13
|
+
f.full_error(*args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'error should not generate content for attribute without errors' do
|
18
|
+
with_error_for @user, :active
|
19
|
+
assert_no_select 'span.error'
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'error should not generate messages when object is not present' do
|
23
|
+
with_error_for :project, :name
|
24
|
+
assert_no_select 'span.error'
|
25
|
+
end
|
26
|
+
|
27
|
+
test "error should not generate messages when object doesn't respond to errors method" do
|
28
|
+
@user.instance_eval { undef errors }
|
29
|
+
with_error_for @user, :name
|
30
|
+
assert_no_select 'span.error'
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'error should generate messages for attribute with single error' do
|
34
|
+
with_error_for @user, :name
|
35
|
+
assert_select 'span.error', "can't be blank"
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'error should generate messages for attribute with one error when using first' do
|
39
|
+
swap SimpleForm, :error_method => :first do
|
40
|
+
with_error_for @user, :age
|
41
|
+
assert_select 'span.error', 'is not a number'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'error should generate messages for attribute with several errors when using to_sentence' do
|
46
|
+
swap SimpleForm, :error_method => :to_sentence do
|
47
|
+
with_error_for @user, :age
|
48
|
+
assert_select 'span.error', 'is not a number and must be greater than 18'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
test 'error should be able to pass html options' do
|
53
|
+
with_error_for @user, :name, :id => 'error', :class => 'yay'
|
54
|
+
assert_select 'span#error.error.yay'
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'error should not modify the options hash' do
|
58
|
+
options = { :id => 'error', :class => 'yay' }
|
59
|
+
with_error_for @user, :name, options
|
60
|
+
assert_select 'span#error.error.yay'
|
61
|
+
assert_equal({ :id => 'error', :class => 'yay' }, options)
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'error should find errors on attribute and association' do
|
65
|
+
with_error_for @user, :company_id, :as => :select,
|
66
|
+
:error_method => :to_sentence, :reflection => Association.new(Company, :company, {})
|
67
|
+
assert_select 'span.error', 'must be valid and company must be present'
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'error should generate an error tag with a clean HTML' do
|
71
|
+
with_error_for @user, :name
|
72
|
+
assert_no_select 'span.error[error_html]'
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'error should generate an error tag with a clean HTML when errors options are present' do
|
76
|
+
with_error_for @user, :name, :error_tag => :p, :error_prefix => 'Name', :error_method => :first
|
77
|
+
assert_no_select 'p.error[error_html]'
|
78
|
+
assert_no_select 'p.error[error_tag]'
|
79
|
+
assert_no_select 'p.error[error_prefix]'
|
80
|
+
assert_no_select 'p.error[error_method]'
|
81
|
+
end
|
82
|
+
|
83
|
+
test 'error should generate an error message with raw HTML tags' do
|
84
|
+
with_error_for @user, :name, :error_prefix => '<b>Name</b>'
|
85
|
+
assert_select 'span.error', "Name can't be blank"
|
86
|
+
assert_select 'span.error b', "Name"
|
87
|
+
end
|
88
|
+
|
89
|
+
# FULL ERRORS
|
90
|
+
|
91
|
+
test 'full error should generate an full error tag for the attribute' do
|
92
|
+
with_full_error_for @user, :name
|
93
|
+
assert_select 'span.error', "Super User Name! can't be blank"
|
94
|
+
end
|
95
|
+
|
96
|
+
test 'full error should generate an full error tag with a clean HTML' do
|
97
|
+
with_full_error_for @user, :name
|
98
|
+
assert_no_select 'span.error[error_html]'
|
99
|
+
end
|
100
|
+
|
101
|
+
test 'full error should allow passing options to full error tag' do
|
102
|
+
with_full_error_for @user, :name, :id => 'name_error', :error_prefix => "Your name"
|
103
|
+
assert_select 'span.error#name_error', "Your name can't be blank"
|
104
|
+
end
|
105
|
+
|
106
|
+
test 'full error should not modify the options hash' do
|
107
|
+
options = { :id => 'name_error' }
|
108
|
+
with_full_error_for @user, :name, options
|
109
|
+
assert_select 'span.error#name_error', "Super User Name! can't be blank"
|
110
|
+
assert_equal({ :id => 'name_error' }, options)
|
111
|
+
end
|
112
|
+
|
113
|
+
# CUSTOM WRAPPERS
|
114
|
+
|
115
|
+
test 'error with custom wrappers works' do
|
116
|
+
swap_wrapper do
|
117
|
+
with_error_for @user, :name
|
118
|
+
assert_select 'span.omg_error', "can't be blank"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,402 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class FormBuilderTest < ActionView::TestCase
|
5
|
+
def with_custom_form_for(object, *args, &block)
|
6
|
+
with_concat_custom_form_for(object) do |f|
|
7
|
+
f.input(*args, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'nested simple fields should yield an instance of FormBuilder' do
|
12
|
+
simple_form_for :user do |f|
|
13
|
+
f.simple_fields_for :posts do |posts_form|
|
14
|
+
assert posts_form.instance_of?(SimpleForm::FormBuilder)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'builder input is html safe' do
|
20
|
+
simple_form_for @user do |f|
|
21
|
+
assert f.input(:name).html_safe?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'builder input should allow a block to configure input' do
|
26
|
+
with_form_for @user, :name do
|
27
|
+
text_field_tag :foo, :bar, :id => :cool
|
28
|
+
end
|
29
|
+
assert_no_select 'input.string'
|
30
|
+
assert_select 'input#cool'
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'builder should allow adding custom input mappings for default input types' do
|
34
|
+
swap SimpleForm, :input_mappings => { /count$/ => :integer } do
|
35
|
+
with_form_for @user, :post_count
|
36
|
+
assert_no_select 'form input#user_post_count.string'
|
37
|
+
assert_select 'form input#user_post_count.numeric.integer'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'builder should allow to skip input_type class' do
|
42
|
+
swap SimpleForm, :generate_additional_classes_for => [:label, :wrapper] do
|
43
|
+
with_form_for @user, :post_count
|
44
|
+
assert_no_select "form input#user_post_count.integer"
|
45
|
+
assert_select "form input#user_post_count"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'builder should allow to add additional classes only for wrapper' do
|
50
|
+
swap SimpleForm, :generate_additional_classes_for => [:wrapper] do
|
51
|
+
with_form_for @user, :post_count
|
52
|
+
assert_no_select "form input#user_post_count.string"
|
53
|
+
assert_no_select "form label#user_post_count.string"
|
54
|
+
assert_select "form div.input.string"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
test 'builder should allow adding custom input mappings for integer input types' do
|
59
|
+
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
|
60
|
+
with_form_for @user, :lock_version
|
61
|
+
assert_no_select 'form input#user_lock_version.integer'
|
62
|
+
assert_select 'form input#user_lock_version.hidden'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'builder uses the first matching custom input map when more than one matches' do
|
67
|
+
swap SimpleForm, :input_mappings => { /count$/ => :integer, /^post_/ => :password } do
|
68
|
+
with_form_for @user, :post_count
|
69
|
+
assert_no_select 'form input#user_post_count.password'
|
70
|
+
assert_select 'form input#user_post_count.numeric.integer'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'builder uses the custom map only for matched attributes' do
|
75
|
+
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
|
76
|
+
with_form_for @user, :post_count
|
77
|
+
assert_no_select 'form input#user_post_count.hidden'
|
78
|
+
assert_select 'form input#user_post_count.string'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# INPUT TYPES
|
83
|
+
test 'builder should generate text fields for string columns' do
|
84
|
+
with_form_for @user, :name
|
85
|
+
assert_select 'form input#user_name.string'
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'builder should generate text areas for text columns' do
|
89
|
+
with_form_for @user, :description
|
90
|
+
assert_select 'form textarea#user_description.text'
|
91
|
+
end
|
92
|
+
|
93
|
+
test 'builder should generate a checkbox for boolean columns' do
|
94
|
+
with_form_for @user, :active
|
95
|
+
assert_select 'form input[type=checkbox]#user_active.boolean'
|
96
|
+
end
|
97
|
+
|
98
|
+
test 'builder should use integer text field for integer columns' do
|
99
|
+
with_form_for @user, :age
|
100
|
+
assert_select 'form input#user_age.numeric.integer'
|
101
|
+
end
|
102
|
+
|
103
|
+
test 'builder should generate decimal text field for decimal columns' do
|
104
|
+
with_form_for @user, :credit_limit
|
105
|
+
assert_select 'form input#user_credit_limit.numeric.decimal'
|
106
|
+
end
|
107
|
+
|
108
|
+
test 'builder should generate password fields for columns that matches password' do
|
109
|
+
with_form_for @user, :password
|
110
|
+
assert_select 'form input#user_password.password'
|
111
|
+
end
|
112
|
+
|
113
|
+
test 'builder should generate country fields for columns that matches country' do
|
114
|
+
with_form_for @user, :residence_country
|
115
|
+
assert_select 'form select#user_residence_country.country'
|
116
|
+
end
|
117
|
+
|
118
|
+
test 'builder should generate time_zone fields for columns that matches time_zone' do
|
119
|
+
with_form_for @user, :time_zone
|
120
|
+
assert_select 'form select#user_time_zone.time_zone'
|
121
|
+
end
|
122
|
+
|
123
|
+
test 'builder should generate email fields for columns that matches email' do
|
124
|
+
with_form_for @user, :email
|
125
|
+
assert_select 'form input#user_email.string.email'
|
126
|
+
end
|
127
|
+
|
128
|
+
test 'builder should generate tel fields for columns that matches phone' do
|
129
|
+
with_form_for @user, :phone_number
|
130
|
+
assert_select 'form input#user_phone_number.string.tel'
|
131
|
+
end
|
132
|
+
|
133
|
+
test 'builder should generate url fields for columns that matches url' do
|
134
|
+
with_form_for @user, :url
|
135
|
+
assert_select 'form input#user_url.string.url'
|
136
|
+
end
|
137
|
+
|
138
|
+
test 'builder should generate date select for date columns' do
|
139
|
+
with_form_for @user, :born_at
|
140
|
+
assert_select 'form select#user_born_at_1i.date'
|
141
|
+
end
|
142
|
+
|
143
|
+
test 'builder should generate time select for time columns' do
|
144
|
+
with_form_for @user, :delivery_time
|
145
|
+
assert_select 'form select#user_delivery_time_4i.time'
|
146
|
+
end
|
147
|
+
|
148
|
+
test 'builder should generate datetime select for datetime columns' do
|
149
|
+
with_form_for @user, :created_at
|
150
|
+
assert_select 'form select#user_created_at_1i.datetime'
|
151
|
+
end
|
152
|
+
|
153
|
+
test 'builder should generate datetime select for timestamp columns' do
|
154
|
+
with_form_for @user, :updated_at
|
155
|
+
assert_select 'form select#user_updated_at_1i.datetime'
|
156
|
+
end
|
157
|
+
|
158
|
+
test 'builder should generate file for file columns' do
|
159
|
+
@user.avatar = mock("file")
|
160
|
+
@user.avatar.expects(:respond_to?).with(:mounted_as).returns(false)
|
161
|
+
@user.avatar.expects(:respond_to?).with(:file?).returns(false)
|
162
|
+
@user.avatar.expects(:respond_to?).with(:public_filename).returns(true)
|
163
|
+
|
164
|
+
with_form_for @user, :avatar
|
165
|
+
assert_select 'form input#user_avatar.file'
|
166
|
+
end
|
167
|
+
|
168
|
+
test 'builder should generate file for attributes that are real db columns but have file methods' do
|
169
|
+
@user.home_picture = mock("file")
|
170
|
+
@user.home_picture.expects(:respond_to?).with(:mounted_as).returns(true)
|
171
|
+
|
172
|
+
with_form_for @user, :home_picture
|
173
|
+
assert_select 'form input#user_home_picture.file'
|
174
|
+
end
|
175
|
+
|
176
|
+
test 'build should generate select if a collection is given' do
|
177
|
+
with_form_for @user, :age, :collection => 1..60
|
178
|
+
assert_select 'form select#user_age.select'
|
179
|
+
end
|
180
|
+
|
181
|
+
test 'builder should allow overriding default input type for text' do
|
182
|
+
with_form_for @user, :name, :as => :text
|
183
|
+
assert_no_select 'form input#user_name'
|
184
|
+
assert_select 'form textarea#user_name.text'
|
185
|
+
|
186
|
+
with_form_for @user, :active, :as => :radio_buttons
|
187
|
+
assert_no_select 'form input[type=checkbox]'
|
188
|
+
assert_select 'form input.radio_buttons[type=radio]', :count => 2
|
189
|
+
|
190
|
+
with_form_for @user, :born_at, :as => :string
|
191
|
+
assert_no_select 'form select'
|
192
|
+
assert_select 'form input#user_born_at.string'
|
193
|
+
end
|
194
|
+
|
195
|
+
# COMMON OPTIONS
|
196
|
+
test 'builder should add chosen form class' do
|
197
|
+
swap SimpleForm, :form_class => :my_custom_class do
|
198
|
+
with_form_for @user, :name
|
199
|
+
assert_select 'form.my_custom_class'
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
test 'builder should allow passing options to input' do
|
204
|
+
with_form_for @user, :name, :input_html => { :class => 'my_input', :id => 'my_input' }
|
205
|
+
assert_select 'form input#my_input.my_input.string'
|
206
|
+
end
|
207
|
+
|
208
|
+
test 'builder should not propagate input options to wrapper' do
|
209
|
+
with_form_for @user, :name, :input_html => { :class => 'my_input', :id => 'my_input' }
|
210
|
+
assert_no_select 'form div.input.my_input.string'
|
211
|
+
assert_select 'form input#my_input.my_input.string'
|
212
|
+
end
|
213
|
+
|
214
|
+
test 'builder should not propagate input options to wrapper with custom wrapper' do
|
215
|
+
swap_wrapper :default, self.custom_wrapper_with_wrapped_input do
|
216
|
+
with_form_for @user, :name, :input_html => { :class => 'my_input' }
|
217
|
+
assert_no_select 'form div.input.my_input'
|
218
|
+
assert_select 'form input.my_input.string'
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
test 'builder should not propagate label options to wrapper with custom wrapper' do
|
223
|
+
swap_wrapper :default, self.custom_wrapper_with_wrapped_label do
|
224
|
+
with_form_for @user, :name, :label_html => { :class => 'my_label' }
|
225
|
+
assert_no_select 'form div.label.my_label'
|
226
|
+
assert_select 'form label.my_label.string'
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
test 'builder should generate a input with label' do
|
231
|
+
with_form_for @user, :name
|
232
|
+
assert_select 'form label.string[for=user_name]', /Name/
|
233
|
+
end
|
234
|
+
|
235
|
+
test 'builder should be able to disable the label for a input' do
|
236
|
+
with_form_for @user, :name, :label => false
|
237
|
+
assert_no_select 'form label'
|
238
|
+
end
|
239
|
+
|
240
|
+
test 'builder should be able to disable the label for an input and return a html safe string' do
|
241
|
+
with_form_for @user, :name, :label => false, :wrapper => custom_wrapper_with_wrapped_label_input
|
242
|
+
assert_select 'form input#user_name'
|
243
|
+
end
|
244
|
+
|
245
|
+
test 'builder should use custom label' do
|
246
|
+
with_form_for @user, :name, :label => 'Yay!'
|
247
|
+
assert_select 'form label', /Yay!/
|
248
|
+
end
|
249
|
+
|
250
|
+
test 'builder should pass options to label' do
|
251
|
+
with_form_for @user, :name, :label_html => { :id => "cool" }
|
252
|
+
assert_select 'form label#cool', /Name/
|
253
|
+
end
|
254
|
+
|
255
|
+
test 'builder should not generate hints for a input' do
|
256
|
+
with_form_for @user, :name
|
257
|
+
assert_no_select 'span.hint'
|
258
|
+
end
|
259
|
+
|
260
|
+
test 'builder should be able to add a hint for a input' do
|
261
|
+
with_form_for @user, :name, :hint => 'test'
|
262
|
+
assert_select 'span.hint', 'test'
|
263
|
+
end
|
264
|
+
|
265
|
+
test 'builder should be able to disable a hint even if it exists in i18n' do
|
266
|
+
store_translations(:en, :simple_form => { :hints => { :name => 'Hint test' } }) do
|
267
|
+
SimpleForm::Inputs::Base.any_instance.expects(:hint).never
|
268
|
+
|
269
|
+
with_form_for @user, :name, :hint => false
|
270
|
+
assert_no_select 'span.hint'
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
test 'builder should pass options to hint' do
|
275
|
+
with_form_for @user, :name, :hint => 'test', :hint_html => { :id => "cool" }
|
276
|
+
assert_select 'span.hint#cool', 'test'
|
277
|
+
end
|
278
|
+
|
279
|
+
test 'builder should generate errors for attribute without errors' do
|
280
|
+
with_form_for @user, :credit_limit
|
281
|
+
assert_no_select 'span.errors'
|
282
|
+
end
|
283
|
+
|
284
|
+
test 'builder should generate errors for attribute with errors' do
|
285
|
+
with_form_for @user, :name
|
286
|
+
assert_select 'span.error', "can't be blank"
|
287
|
+
end
|
288
|
+
|
289
|
+
test 'builder should be able to disable showing errors for a input' do
|
290
|
+
with_form_for @user, :name, :error => false
|
291
|
+
assert_no_select 'span.error'
|
292
|
+
end
|
293
|
+
|
294
|
+
test 'builder should pass options to errors' do
|
295
|
+
with_form_for @user, :name, :error_html => { :id => "cool" }
|
296
|
+
assert_select 'span.error#cool', "can't be blank"
|
297
|
+
end
|
298
|
+
|
299
|
+
test 'placeholder should not be generated when set to false' do
|
300
|
+
store_translations(:en, :simple_form => { :placeholders => { :user => {
|
301
|
+
:name => 'Name goes here'
|
302
|
+
} } }) do
|
303
|
+
with_form_for @user, :name, :placeholder => false
|
304
|
+
assert_no_select 'input[placeholder]'
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# DEFAULT OPTIONS
|
309
|
+
test 'builder should receive a default argument and pass it to the inputs' do
|
310
|
+
with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f|
|
311
|
+
f.input :name
|
312
|
+
end
|
313
|
+
assert_select 'input.default_class'
|
314
|
+
end
|
315
|
+
|
316
|
+
test 'builder should receive a default argument and pass it to the inputs, respecting the specific options' do
|
317
|
+
with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f|
|
318
|
+
f.input :name, :input_html => { :id => 'specific_id' }
|
319
|
+
end
|
320
|
+
assert_select 'input.default_class#specific_id'
|
321
|
+
end
|
322
|
+
|
323
|
+
test 'builder should receive a default argument and pass it to the inputs, overwriting the defaults with specific options' do
|
324
|
+
with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f|
|
325
|
+
f.input :name, :input_html => { :id => 'specific_id' }
|
326
|
+
end
|
327
|
+
assert_select 'input.default_class#specific_id'
|
328
|
+
end
|
329
|
+
|
330
|
+
test 'builder should receive a default argument and pass it to the inputs without changing the defaults' do
|
331
|
+
with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f|
|
332
|
+
concat(f.input :name)
|
333
|
+
concat(f.input :credit_limit)
|
334
|
+
end
|
335
|
+
|
336
|
+
assert_select "input.string.default_class[name='user[name]']"
|
337
|
+
assert_no_select "input.string[name='user[credit_limit]']"
|
338
|
+
end
|
339
|
+
|
340
|
+
test 'builder should receive a default argument and pass it to the inputs and nested form' do
|
341
|
+
@user.company = Company.new(1, 'Empresa')
|
342
|
+
|
343
|
+
with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f|
|
344
|
+
concat(f.input :name)
|
345
|
+
concat(f.simple_fields_for(:company) do |company_form|
|
346
|
+
concat(company_form.input :name)
|
347
|
+
end)
|
348
|
+
end
|
349
|
+
|
350
|
+
assert_select "input.string.default_class[name='user[name]']"
|
351
|
+
assert_select "input.string.default_class[name='user[company_attributes][name]']"
|
352
|
+
end
|
353
|
+
|
354
|
+
# WITHOUT OBJECT
|
355
|
+
test 'builder should generate properly when object is not present' do
|
356
|
+
with_form_for :project, :name
|
357
|
+
assert_select 'form input.string#project_name'
|
358
|
+
end
|
359
|
+
|
360
|
+
test 'builder should generate password fields based on attribute name when object is not present' do
|
361
|
+
with_form_for :project, :password_confirmation
|
362
|
+
assert_select 'form input[type=password].password#project_password_confirmation'
|
363
|
+
end
|
364
|
+
|
365
|
+
test 'builder should generate text fields by default for all attributes when object is not present' do
|
366
|
+
with_form_for :project, :created_at
|
367
|
+
assert_select 'form input.string#project_created_at'
|
368
|
+
with_form_for :project, :budget
|
369
|
+
assert_select 'form input.string#project_budget'
|
370
|
+
end
|
371
|
+
|
372
|
+
test 'builder should allow overriding input type when object is not present' do
|
373
|
+
with_form_for :project, :created_at, :as => :datetime
|
374
|
+
assert_select 'form select.datetime#project_created_at_1i'
|
375
|
+
with_form_for :project, :budget, :as => :decimal
|
376
|
+
assert_select 'form input.decimal#project_budget'
|
377
|
+
end
|
378
|
+
|
379
|
+
# CUSTOM FORM BUILDER
|
380
|
+
test 'custom builder should inherit mappings' do
|
381
|
+
with_custom_form_for @user, :email
|
382
|
+
assert_select 'form input[type=email]#user_email.custom'
|
383
|
+
end
|
384
|
+
|
385
|
+
test 'form with CustomMapTypeFormBuilder should use custom map type builder' do
|
386
|
+
with_concat_custom_mapping_form_for(:user) do |user|
|
387
|
+
assert user.instance_of?(CustomMapTypeFormBuilder)
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
test 'form with CustomMapTypeFormBuilder should use custom mapping' do
|
392
|
+
with_concat_custom_mapping_form_for(:user) do |user|
|
393
|
+
assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
test 'form without CustomMapTypeFormBuilder should not use custom mapping' do
|
398
|
+
with_concat_form_for(:user) do |user|
|
399
|
+
assert_nil user.class.mappings[:custom_type]
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|