simple_form 2.0.0 → 2.1.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.
Files changed (60) hide show
  1. data/CHANGELOG.md +34 -231
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +221 -159
  4. data/lib/generators/simple_form/install_generator.rb +14 -5
  5. data/lib/generators/simple_form/templates/config/initializers/{simple_form.rb.tt → simple_form.rb} +8 -42
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +45 -0
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +26 -0
  8. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -5
  9. data/lib/simple_form/action_view_extensions/builder.rb +46 -30
  10. data/lib/simple_form/action_view_extensions/builder.rb.orig +247 -0
  11. data/lib/simple_form/action_view_extensions/form_helper.rb +7 -9
  12. data/lib/simple_form/components/hints.rb +3 -2
  13. data/lib/simple_form/components/label_input.rb +1 -1
  14. data/lib/simple_form/components/labels.rb +1 -1
  15. data/lib/simple_form/components/maxlength.rb +1 -1
  16. data/lib/simple_form/components/min_max.rb +2 -1
  17. data/lib/simple_form/components/pattern.rb +1 -1
  18. data/lib/simple_form/components/readonly.rb +1 -1
  19. data/lib/simple_form/components.rb +12 -10
  20. data/lib/simple_form/error_notification.rb +1 -1
  21. data/lib/simple_form/form_builder.rb +18 -8
  22. data/lib/simple_form/form_builder.rb.orig +486 -0
  23. data/lib/simple_form/helpers/validators.rb +2 -2
  24. data/lib/simple_form/inputs/base.rb +33 -8
  25. data/lib/simple_form/inputs/boolean_input.rb +19 -5
  26. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +2 -6
  27. data/lib/simple_form/inputs/date_time_input.rb +0 -4
  28. data/lib/simple_form/inputs.rb +19 -17
  29. data/lib/simple_form/version.rb +1 -1
  30. data/lib/simple_form/version.rb.orig +7 -0
  31. data/lib/simple_form/wrappers/many.rb +5 -1
  32. data/lib/simple_form/wrappers/root.rb +4 -2
  33. data/lib/simple_form/wrappers/single.rb +6 -0
  34. data/lib/simple_form.rb +17 -8
  35. data/test/action_view_extensions/builder_test.rb +145 -66
  36. data/test/action_view_extensions/form_helper_test.rb +59 -20
  37. data/test/components/label_test.rb +34 -17
  38. data/test/form_builder/association_test.rb +19 -10
  39. data/test/form_builder/error_notification_test.rb +1 -1
  40. data/test/form_builder/general_test.rb +74 -12
  41. data/test/form_builder/hint_test.rb +15 -0
  42. data/test/form_builder/input_field_test.rb +45 -0
  43. data/test/form_builder/label_test.rb +6 -0
  44. data/test/form_builder/wrapper_test.rb +58 -4
  45. data/test/generators/simple_form_generator_test.rb +11 -1
  46. data/test/inputs/boolean_input_test.rb +39 -0
  47. data/test/inputs/datetime_input_test.rb +2 -2
  48. data/test/inputs/disabled_test.rb +51 -11
  49. data/test/inputs/discovery_test.rb +9 -1
  50. data/test/inputs/general_test.rb +58 -11
  51. data/test/inputs/grouped_collection_select_input_test.rb +15 -0
  52. data/test/inputs/numeric_input_test.rb +6 -0
  53. data/test/inputs/readonly_test.rb +51 -11
  54. data/test/inputs/string_input_test.rb +6 -0
  55. data/test/support/discovery_inputs.rb +6 -0
  56. data/test/support/misc_helpers.rb +41 -1
  57. data/test/support/models.rb +8 -4
  58. data/test/test_helper.rb +8 -6
  59. metadata +26 -13
  60. data/test/support/mock_response.rb +0 -14
@@ -11,6 +11,27 @@ class WrapperTest < ActionView::TestCase
11
11
  assert_no_select 'div.field_with_errors'
12
12
  end
13
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
+
14
35
  test 'wrapper should add error class for attribute with errors' do
15
36
  with_form_for @user, :name
16
37
  assert_select 'div.field_with_errors'
@@ -72,6 +93,16 @@ class WrapperTest < ActionView::TestCase
72
93
  assert_select 'form div.wrapper'
73
94
  assert_no_select 'div.required'
74
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
75
106
  end
76
107
  end
77
108
 
@@ -99,16 +130,16 @@ class WrapperTest < ActionView::TestCase
99
130
 
100
131
  test 'custom wrappers on a form basis' do
101
132
  swap_wrapper :another do
102
- concat simple_form_for(@user) { |f|
133
+ with_concat_form_for(@user) do |f|
103
134
  f.input :name
104
- }
135
+ end
105
136
 
106
137
  assert_no_select "section.custom_wrapper div.another_wrapper label"
107
138
  assert_no_select "section.custom_wrapper div.another_wrapper input.string"
108
139
 
109
- concat simple_form_for(@user, :wrapper => :another) { |f|
140
+ with_concat_form_for(@user, :wrapper => :another) do |f|
110
141
  f.input :name
111
- }
142
+ end
112
143
 
113
144
  assert_select "section.custom_wrapper div.another_wrapper label"
114
145
  assert_select "section.custom_wrapper div.another_wrapper input.string"
@@ -141,9 +172,32 @@ class WrapperTest < ActionView::TestCase
141
172
  end
142
173
  end
143
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
+
144
188
  test 'raise error when wrapper not found' do
145
189
  assert_raise SimpleForm::WrapperNotFound do
146
190
  with_form_for @user, :name, :wrapper => :not_found
147
191
  end
148
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
149
203
  end
@@ -19,10 +19,20 @@ class SimpleFormGeneratorTest < Rails::Generators::TestCase
19
19
 
20
20
  test 'generates the simple_form initializer with the bootstrap wrappers' do
21
21
  run_generator %w(--bootstrap)
22
- assert_file 'config/initializers/simple_form.rb', /config\.wrappers :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/,
23
25
  /config\.default_wrapper = :bootstrap/
24
26
  end
25
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
+
26
36
  %W(erb haml slim).each do |engine|
27
37
  test "generates the scaffold template when using #{engine}" do
28
38
  run_generator ['-e', engine]
@@ -14,6 +14,24 @@ class BooleanInputTest < ActionView::TestCase
14
14
  assert_no_select 'label'
15
15
  end
16
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
+
17
35
  test 'input uses inline boolean style by default' do
18
36
  with_input_for @user, :active, :boolean
19
37
  assert_select 'input.boolean + label.boolean.optional'
@@ -29,6 +47,20 @@ class BooleanInputTest < ActionView::TestCase
29
47
  end
30
48
  end
31
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
+
32
64
  test 'input boolean with nested generates a manual hidden field for checkbox outside the label, to recreate Rails functionality with valid html5' do
33
65
  swap SimpleForm, :boolean_style => :nested do
34
66
  with_input_for @user, :active, :boolean
@@ -70,6 +102,13 @@ class BooleanInputTest < ActionView::TestCase
70
102
  end
71
103
  end
72
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
+
73
112
  test 'input boolean works using :input only in wrapper config (no label_input)' do
74
113
  swap_wrapper do
75
114
  with_input_for @user, :active, :boolean
@@ -91,9 +91,9 @@ class DateTimeInputTest < ActionView::TestCase
91
91
  assert_select 'label[for=project_created_at_4i]'
92
92
  end
93
93
 
94
- test 'date time input should not generate invalid required html attribute' do
94
+ test 'date time input should generate required html attribute' do
95
95
  with_input_for @user, :delivery_time, :time, :required => true
96
96
  assert_select 'select.required'
97
- assert_no_select 'select[required]'
97
+ assert_select 'select[required]'
98
98
  end
99
99
  end
@@ -1,38 +1,78 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class DisabledTest < ActionView::TestCase
4
- test 'input should generate disabled elements based on the disabled option' do
4
+ test 'string input should be disabled when disabled option is true' do
5
5
  with_input_for @user, :name, :string, :disabled => true
6
6
  assert_select 'input.string.disabled[disabled]'
7
+ end
8
+
9
+ test 'text input should be disabled when disabled option is true' do
7
10
  with_input_for @user, :description, :text, :disabled => true
8
11
  assert_select 'textarea.text.disabled[disabled]'
12
+ end
13
+
14
+ test 'numeric input should be disabled when disabled option is true' do
9
15
  with_input_for @user, :age, :integer, :disabled => true
10
16
  assert_select 'input.integer.disabled[disabled]'
17
+ end
18
+
19
+ test 'date input should be disabled when disabled option is true' do
11
20
  with_input_for @user, :born_at, :date, :disabled => true
12
21
  assert_select 'select.date.disabled[disabled]'
22
+ end
23
+
24
+ test 'datetime input should be disabled when disabled option is true' do
13
25
  with_input_for @user, :created_at, :datetime, :disabled => true
14
26
  assert_select 'select.datetime.disabled[disabled]'
27
+ end
15
28
 
29
+ test 'string input should not be disabled when disabled option is false' do
16
30
  with_input_for @user, :name, :string, :disabled => false
17
- assert_select 'input.string:not(.disabled[disabled])'
31
+ assert_no_select 'input.string.disabled[disabled]'
32
+ end
33
+
34
+ test 'text input should not be disabled when disabled option is false' do
18
35
  with_input_for @user, :description, :text, :disabled => false
19
- assert_select 'textarea.text:not(.disabled[disabled])'
36
+ assert_no_select 'textarea.text.disabled[disabled]'
37
+ end
38
+
39
+ test 'numeric input should not be disabled when disabled option is false' do
20
40
  with_input_for @user, :age, :integer, :disabled => false
21
- assert_select 'input.integer:not(.disabled[disabled])'
41
+ assert_no_select 'input.integer.disabled[disabled]'
42
+ end
43
+
44
+ test 'date input should not be disabled when disabled option is false' do
22
45
  with_input_for @user, :born_at, :date, :disabled => false
23
- assert_select 'select.date:not(.disabled[disabled])'
46
+ assert_no_select 'select.date.disabled[disabled]'
47
+ end
48
+
49
+ test 'datetime input should not be disabled when disabled option is false' do
24
50
  with_input_for @user, :created_at, :datetime, :disabled => false
25
- assert_select 'select.datetime:not(.disabled[disabled])'
51
+ assert_no_select 'select.datetime.disabled[disabled]'
52
+ end
26
53
 
54
+ test 'string input should not be disabled when disabled option is not present' do
27
55
  with_input_for @user, :name, :string
28
- assert_select 'input.string:not(.disabled[disabled])'
56
+ assert_no_select 'input.string.disabled[disabled]'
57
+ end
58
+
59
+ test 'text input should not be disabled when disabled option is not present' do
29
60
  with_input_for @user, :description, :text
30
- assert_select 'textarea.text:not(.disabled[disabled])'
61
+ assert_no_select 'textarea.text.disabled[disabled]'
62
+ end
63
+
64
+ test 'numeric input should not be disabled when disabled option is not present' do
31
65
  with_input_for @user, :age, :integer
32
- assert_select 'input.integer:not(.disabled[disabled])'
66
+ assert_no_select 'input.integer.disabled[disabled]'
67
+ end
68
+
69
+ test 'date input should not be disabled when disabled option is not present' do
33
70
  with_input_for @user, :born_at, :date
34
- assert_select 'select.date:not(.disabled[disabled])'
71
+ assert_no_select 'select.date.disabled[disabled]'
72
+ end
73
+
74
+ test 'datetime input should not be disabled when disabled option is not present' do
35
75
  with_input_for @user, :created_at, :datetime
36
- assert_select 'select.datetime:not(.disabled[disabled])'
76
+ assert_no_select 'select.datetime.disabled[disabled]'
37
77
  end
38
78
  end
@@ -12,6 +12,7 @@ class DiscoveryTest < ActionView::TestCase
12
12
  Object.send :remove_const, :StringInput
13
13
  Object.send :remove_const, :NumericInput
14
14
  Object.send :remove_const, :CustomizedInput
15
+ Object.send :remove_const, :CollectionSelectInput
15
16
  end
16
17
  end
17
18
  end
@@ -58,4 +59,11 @@ class DiscoveryTest < ActionView::TestCase
58
59
  assert_select 'form section input#user_age.numeric.integer'
59
60
  end
60
61
  end
61
- end
62
+
63
+ test 'new inputs can override the input_html_options' do
64
+ discovery do
65
+ with_form_for @user, :active, :as => :select
66
+ assert_select 'form select#user_active.select.chosen'
67
+ end
68
+ end
69
+ end
@@ -15,39 +15,79 @@ class InputTest < ActionView::TestCase
15
15
  assert_select 'select.datetime'
16
16
  end
17
17
 
18
- test 'input should generate autofocus attribute based on the autofocus option' do
18
+ test 'string input should generate autofocus attribute when autofocus option is true' do
19
19
  with_input_for @user, :name, :string, :autofocus => true
20
20
  assert_select 'input.string[autofocus]'
21
+ end
22
+
23
+ test 'text input should generate autofocus attribute when autofocus option is true' do
21
24
  with_input_for @user, :description, :text, :autofocus => true
22
25
  assert_select 'textarea.text[autofocus]'
26
+ end
27
+
28
+ test 'numeric input should generate autofocus attribute when autofocus option is true' do
23
29
  with_input_for @user, :age, :integer, :autofocus => true
24
30
  assert_select 'input.integer[autofocus]'
31
+ end
32
+
33
+ test 'date input should generate autofocus attribute when autofocus option is true' do
25
34
  with_input_for @user, :born_at, :date, :autofocus => true
26
35
  assert_select 'select.date[autofocus]'
36
+ end
37
+
38
+ test 'datetime input should generate autofocus attribute when autofocus option is true' do
27
39
  with_input_for @user, :created_at, :datetime, :autofocus => true
28
40
  assert_select 'select.datetime[autofocus]'
41
+ end
29
42
 
43
+ test 'string input should generate autofocus attribute when autofocus option is false' do
30
44
  with_input_for @user, :name, :string, :autofocus => false
31
- assert_select 'input.string:not([autofocus])'
45
+ assert_no_select 'input.string[autofocus]'
46
+ end
47
+
48
+ test 'text input should generate autofocus attribute when autofocus option is false' do
32
49
  with_input_for @user, :description, :text, :autofocus => false
33
- assert_select 'textarea.text:not([autofocus])'
50
+ assert_no_select 'textarea.text[autofocus]'
51
+ end
52
+
53
+ test 'numeric input should generate autofocus attribute when autofocus option is false' do
34
54
  with_input_for @user, :age, :integer, :autofocus => false
35
- assert_select 'input.integer:not([autofocus])'
55
+ assert_no_select 'input.integer[autofocus]'
56
+ end
57
+
58
+ test 'date input should generate autofocus attribute when autofocus option is false' do
36
59
  with_input_for @user, :born_at, :date, :autofocus => false
37
- assert_select 'select.date:not([autofocus])'
60
+ assert_no_select 'select.date[autofocus]'
61
+ end
62
+
63
+ test 'datetime input should generate autofocus attribute when autofocus option is false' do
38
64
  with_input_for @user, :created_at, :datetime, :autofocus => false
39
- assert_select 'select.datetime:not([autofocus])'
65
+ assert_no_select 'select.datetime[autofocus]'
66
+ end
40
67
 
68
+ test 'string input should generate autofocus attribute when autofocus option is not present' do
41
69
  with_input_for @user, :name, :string
42
- assert_select 'input.string:not([autofocus])'
70
+ assert_no_select 'input.string[autofocus]'
71
+ end
72
+
73
+ test 'text input should generate autofocus attribute when autofocus option is not present' do
43
74
  with_input_for @user, :description, :text
44
- assert_select 'textarea.text:not([autofocus])'
75
+ assert_no_select 'textarea.text[autofocus]'
76
+ end
77
+
78
+ test 'numeric input should generate autofocus attribute when autofocus option is not present' do
45
79
  with_input_for @user, :age, :integer
46
- assert_select 'input.integer:not([autofocus])'
80
+ assert_no_select 'input.integer[autofocus]'
81
+ end
82
+
83
+ test 'date input should generate autofocus attribute when autofocus option is not present' do
47
84
  with_input_for @user, :born_at, :date
48
- assert_select 'select.date:not([autofocus])'
85
+ assert_no_select 'select.date[autofocus]'
86
+ end
87
+
88
+ test 'datetime input should generate autofocus attribute when autofocus option is not present' do
49
89
  with_input_for @user, :created_at, :datetime
50
- assert_select 'select.datetime:not([autofocus])'
90
+ assert_no_select 'select.datetime[autofocus]'
51
91
  end
52
92
 
53
93
  # With no object
@@ -66,4 +106,11 @@ class InputTest < ActionView::TestCase
66
106
  with_input_for :project, :name, :select, :collection => ['Jose', 'Carlos']
67
107
  assert_select 'select.select#project_name'
68
108
  end
109
+
110
+ test 'input should not generate empty css class' do
111
+ swap SimpleForm, :generate_additional_classes_for => [:wrapper, :label] do
112
+ with_input_for :project, :name, :string
113
+ assert_no_select 'input#project_name[class]'
114
+ end
115
+ end
69
116
  end
@@ -94,6 +94,21 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
94
94
  end
95
95
  end
96
96
 
97
+ test 'grouped collection should allow overriding label and value methods using a lambda' do
98
+ with_input_for @user, :tag_ids, :grouped_select,
99
+ :collection => { 'Authors' => ['Jose', 'Carlos'] },
100
+ :group_method => :last,
101
+ :label_method => lambda { |i| i.upcase },
102
+ :value_method => lambda { |i| i.downcase }
103
+
104
+ assert_select 'select.grouped_select#user_tag_ids' do
105
+ assert_select 'optgroup[label=Authors]' do
106
+ assert_select 'option[value=jose]', 'JOSE'
107
+ assert_select 'option[value=carlos]', 'CARLOS'
108
+ end
109
+ end
110
+ end
111
+
97
112
  test 'grouped collection with associations' do
98
113
  tag_groups = [
99
114
  TagGroup.new(1, "Group of Tags", [Tag.new(1, "Tag 1"), Tag.new(2, "Tag 2")]),
@@ -164,4 +164,10 @@ class NumericInputTest < ActionView::TestCase
164
164
  assert_select 'input[max=99]'
165
165
  end
166
166
  end
167
+
168
+ test 'min_max should not emit max value as bare string' do
169
+ with_input_for @other_validating_user, :age, :integer
170
+ assert_select 'input[max]'
171
+ assert_no_select 'div', %r{^99}
172
+ end
167
173
  end
@@ -1,39 +1,79 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ReadonlyTest < ActionView::TestCase
4
- test 'input should generate readonly elements based on the readonly option' do
4
+ test 'string input should generate readonly elements when readonly option is true' do
5
5
  with_input_for @user, :name, :string, :readonly => true
6
6
  assert_select 'input.string.readonly[readonly]'
7
+ end
8
+
9
+ test 'text input should generate readonly elements when readonly option is true' do
7
10
  with_input_for @user, :description, :text, :readonly => true
8
11
  assert_select 'textarea.text.readonly[readonly]'
12
+ end
13
+
14
+ test 'numeric input should generate readonly elements when readonly option is true' do
9
15
  with_input_for @user, :age, :integer, :readonly => true
10
16
  assert_select 'input.integer.readonly[readonly]'
17
+ end
18
+
19
+ test 'date input should generate readonly elements when readonly option is true' do
11
20
  with_input_for @user, :born_at, :date, :readonly => true
12
21
  assert_select 'select.date.readonly[readonly]'
22
+ end
23
+
24
+ test 'datetime input should generate readonly elements when readonly option is true' do
13
25
  with_input_for @user, :created_at, :datetime, :readonly => true
14
26
  assert_select 'select.datetime.readonly[readonly]'
27
+ end
15
28
 
29
+ test 'string input should generate readonly elements when readonly option is false' do
16
30
  with_input_for @user, :name, :string, :readonly => false
17
- assert_select 'input.string:not(.readonly[readonly])'
31
+ assert_no_select 'input.string.readonly[readonly]'
32
+ end
33
+
34
+ test 'text input should generate readonly elements when readonly option is false' do
18
35
  with_input_for @user, :description, :text, :readonly => false
19
- assert_select 'textarea.text:not(.readonly[readonly])'
36
+ assert_no_select 'textarea.text.readonly[readonly]'
37
+ end
38
+
39
+ test 'numeric input should generate readonly elements when readonly option is false' do
20
40
  with_input_for @user, :age, :integer, :readonly => false
21
- assert_select 'input.integer:not(.readonly[readonly])'
41
+ assert_no_select 'input.integer.readonly[readonly]'
42
+ end
43
+
44
+ test 'date input should generate readonly elements when readonly option is false' do
22
45
  with_input_for @user, :born_at, :date, :readonly => false
23
- assert_select 'select.date:not(.readonly[readonly])'
46
+ assert_no_select 'select.date.readonly[readonly]'
47
+ end
48
+
49
+ test 'datetime input should generate readonly elements when readonly option is false' do
24
50
  with_input_for @user, :created_at, :datetime, :readonly => false
25
- assert_select 'select.datetime:not(.readonly[readonly])'
51
+ assert_no_select 'select.datetime.readonly[readonly]'
52
+ end
26
53
 
54
+ test 'string input should generate readonly elements when readonly option is not present' do
27
55
  with_input_for @user, :name, :string
28
- assert_select 'input.string:not(.readonly[readonly])'
56
+ assert_no_select 'input.string.readonly[readonly]'
57
+ end
58
+
59
+ test 'text input should generate readonly elements when readonly option is not present' do
29
60
  with_input_for @user, :description, :text
30
- assert_select 'textarea.text:not(.readonly[readonly])'
61
+ assert_no_select 'textarea.text.readonly[readonly]'
62
+ end
63
+
64
+ test 'numeric input should generate readonly elements when readonly option is not present' do
31
65
  with_input_for @user, :age, :integer
32
- assert_select 'input.integer:not(.readonly[readonly])'
66
+ assert_no_select 'input.integer.readonly[readonly]'
67
+ end
68
+
69
+ test 'date input should generate readonly elements when readonly option is not present' do
33
70
  with_input_for @user, :born_at, :date
34
- assert_select 'select.date:not(.readonly[readonly])'
71
+ assert_no_select 'select.date.readonly[readonly]'
72
+ end
73
+
74
+ test 'datetime input should generate readonly elements when readonly option is not present' do
35
75
  with_input_for @user, :created_at, :datetime
36
- assert_select 'select.datetime:not(.readonly[readonly])'
76
+ assert_no_select 'select.datetime.readonly[readonly]'
37
77
  end
38
78
 
39
79
  test 'input should generate readonly attribute when the field is readonly and the object is persisted' do
@@ -57,6 +57,12 @@ class StringInputTest < ActionView::TestCase
57
57
  assert_select 'input.string[maxlength=12]'
58
58
  end
59
59
 
60
+ test 'input size and maxlength should be the column limit plus one to make room for decimal point' do
61
+ with_input_for @user, :credit_limit, :string
62
+
63
+ assert_select "input.string[maxlength=16][size=16]"
64
+ end
65
+
60
66
  test 'input should not generate placeholder by default' do
61
67
  with_input_for @user, :name, :string
62
68
  assert_no_select 'input[placeholder]'
@@ -19,3 +19,9 @@ class CustomizedInput < SimpleForm::Inputs::StringInput
19
19
  :text_field
20
20
  end
21
21
  end
22
+
23
+ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
24
+ def input_html_classes
25
+ super.push('chosen')
26
+ end
27
+ end
@@ -46,6 +46,24 @@ module MiscHelpers
46
46
  end
47
47
  end
48
48
 
49
+ def custom_wrapper_with_wrapped_input
50
+ SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
51
+ b.wrapper :tag => :div, :class => 'elem' do |component|
52
+ component.use :label
53
+ component.use :input, :wrap_with => { :tag => :div, :class => 'input' }
54
+ end
55
+ end
56
+ end
57
+
58
+ def custom_wrapper_with_wrapped_label
59
+ SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
60
+ b.wrapper :tag => :div, :class => 'elem' do |component|
61
+ component.use :label, :wrap_with => { :tag => :div, :class => 'label' }
62
+ component.use :input
63
+ end
64
+ end
65
+ end
66
+
49
67
  def custom_wrapper_without_top_level
50
68
  SimpleForm.build :tag => false, :class => 'custom_wrapper_without_top_level' do |b|
51
69
  b.use :label_input
@@ -54,6 +72,24 @@ module MiscHelpers
54
72
  end
55
73
  end
56
74
 
75
+ def custom_wrapper_without_class
76
+ SimpleForm.build :tag => :div, :wrapper_html => { :id => 'custom_wrapper_without_class' } do |b|
77
+ b.use :label_input
78
+ end
79
+ end
80
+
81
+ def custom_wrapper_with_label_html_option
82
+ SimpleForm.build :tag => :div, :class => "custom_wrapper", :label_html => { :class => 'extra-label-class' } do |b|
83
+ b.use :label_input
84
+ end
85
+ end
86
+
87
+ def custom_wrapper_with_wrapped_label_input
88
+ SimpleForm.build :tag => :section, :class => "custom_wrapper", :pattern => false do |b|
89
+ b.use :label_input, :wrap_with => { :tag => :div, :class => :field }
90
+ end
91
+ end
92
+
57
93
  def custom_form_for(object, *args, &block)
58
94
  simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
59
95
  end
@@ -63,7 +99,11 @@ module MiscHelpers
63
99
  end
64
100
 
65
101
  def with_concat_form_for(*args, &block)
66
- concat simple_form_for(*args, &block)
102
+ concat simple_form_for(*args, &(block || proc {}))
103
+ end
104
+
105
+ def with_concat_fields_for(*args, &block)
106
+ concat simple_fields_for(*args, &block)
67
107
  end
68
108
 
69
109
  def with_concat_custom_form_for(*args, &block)
@@ -1,8 +1,12 @@
1
- require 'ostruct'
2
-
3
- Column = Struct.new(:name, :type, :limit)
4
1
  Association = Struct.new(:klass, :name, :macro, :options)
5
2
 
3
+ class Column < Struct.new(:name, :type, :limit)
4
+ # Returns +true+ if the column is either of type integer, float or decimal.
5
+ def number?
6
+ type == :integer || type == :float || type == :decimal
7
+ end
8
+ end
9
+
6
10
  class Company < Struct.new(:id, :name)
7
11
  extend ActiveModel::Naming
8
12
  include ActiveModel::Conversion
@@ -126,7 +130,7 @@ class User
126
130
  end
127
131
 
128
132
  def self.readonly_attributes
129
- [:credit_card]
133
+ ["credit_card"]
130
134
  end
131
135
  end
132
136