ehoch_simple_form 2.0.2.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/CHANGELOG.md +257 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +797 -0
  4. data/lib/generators/simple_form/USAGE +3 -0
  5. data/lib/generators/simple_form/install_generator.rb +32 -0
  6. data/lib/generators/simple_form/templates/README +12 -0
  7. data/lib/generators/simple_form/templates/_form.html.erb +13 -0
  8. data/lib/generators/simple_form/templates/_form.html.haml +10 -0
  9. data/lib/generators/simple_form/templates/_form.html.slim +10 -0
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +181 -0
  11. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +26 -0
  12. data/lib/simple_form.rb +215 -0
  13. data/lib/simple_form/action_view_extensions/builder.rb +338 -0
  14. data/lib/simple_form/action_view_extensions/form_helper.rb +74 -0
  15. data/lib/simple_form/components.rb +20 -0
  16. data/lib/simple_form/components/errors.rb +35 -0
  17. data/lib/simple_form/components/hints.rb +18 -0
  18. data/lib/simple_form/components/html5.rb +26 -0
  19. data/lib/simple_form/components/label_input.rb +15 -0
  20. data/lib/simple_form/components/labels.rb +79 -0
  21. data/lib/simple_form/components/maxlength.rb +41 -0
  22. data/lib/simple_form/components/min_max.rb +50 -0
  23. data/lib/simple_form/components/pattern.rb +34 -0
  24. data/lib/simple_form/components/placeholders.rb +16 -0
  25. data/lib/simple_form/components/readonly.rb +22 -0
  26. data/lib/simple_form/core_ext/hash.rb +16 -0
  27. data/lib/simple_form/error_notification.rb +48 -0
  28. data/lib/simple_form/form_builder.rb +472 -0
  29. data/lib/simple_form/helpers.rb +12 -0
  30. data/lib/simple_form/helpers/autofocus.rb +11 -0
  31. data/lib/simple_form/helpers/disabled.rb +15 -0
  32. data/lib/simple_form/helpers/readonly.rb +15 -0
  33. data/lib/simple_form/helpers/required.rb +35 -0
  34. data/lib/simple_form/helpers/validators.rb +44 -0
  35. data/lib/simple_form/i18n_cache.rb +22 -0
  36. data/lib/simple_form/inputs.rb +21 -0
  37. data/lib/simple_form/inputs/base.rb +162 -0
  38. data/lib/simple_form/inputs/block_input.rb +14 -0
  39. data/lib/simple_form/inputs/boolean_input.rb +64 -0
  40. data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
  41. data/lib/simple_form/inputs/collection_input.rb +101 -0
  42. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +63 -0
  43. data/lib/simple_form/inputs/collection_select_input.rb +14 -0
  44. data/lib/simple_form/inputs/date_time_input.rb +28 -0
  45. data/lib/simple_form/inputs/file_input.rb +9 -0
  46. data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
  47. data/lib/simple_form/inputs/hidden_input.rb +17 -0
  48. data/lib/simple_form/inputs/numeric_input.rb +24 -0
  49. data/lib/simple_form/inputs/password_input.rb +12 -0
  50. data/lib/simple_form/inputs/priority_input.rb +24 -0
  51. data/lib/simple_form/inputs/range_input.rb +14 -0
  52. data/lib/simple_form/inputs/string_input.rb +23 -0
  53. data/lib/simple_form/inputs/text_input.rb +11 -0
  54. data/lib/simple_form/map_type.rb +16 -0
  55. data/lib/simple_form/version.rb +3 -0
  56. data/lib/simple_form/wrappers.rb +8 -0
  57. data/lib/simple_form/wrappers/builder.rb +103 -0
  58. data/lib/simple_form/wrappers/many.rb +69 -0
  59. data/lib/simple_form/wrappers/root.rb +34 -0
  60. data/lib/simple_form/wrappers/single.rb +18 -0
  61. data/test/action_view_extensions/builder_test.rb +577 -0
  62. data/test/action_view_extensions/form_helper_test.rb +104 -0
  63. data/test/components/label_test.rb +310 -0
  64. data/test/form_builder/association_test.rb +177 -0
  65. data/test/form_builder/button_test.rb +47 -0
  66. data/test/form_builder/error_notification_test.rb +79 -0
  67. data/test/form_builder/error_test.rb +121 -0
  68. data/test/form_builder/general_test.rb +356 -0
  69. data/test/form_builder/hint_test.rb +139 -0
  70. data/test/form_builder/input_field_test.rb +63 -0
  71. data/test/form_builder/label_test.rb +71 -0
  72. data/test/form_builder/wrapper_test.rb +149 -0
  73. data/test/generators/simple_form_generator_test.rb +32 -0
  74. data/test/inputs/boolean_input_test.rb +108 -0
  75. data/test/inputs/collection_check_boxes_input_test.rb +224 -0
  76. data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
  77. data/test/inputs/collection_select_input_test.rb +241 -0
  78. data/test/inputs/datetime_input_test.rb +99 -0
  79. data/test/inputs/disabled_test.rb +38 -0
  80. data/test/inputs/discovery_test.rb +61 -0
  81. data/test/inputs/file_input_test.rb +16 -0
  82. data/test/inputs/general_test.rb +69 -0
  83. data/test/inputs/grouped_collection_select_input_test.rb +118 -0
  84. data/test/inputs/hidden_input_test.rb +30 -0
  85. data/test/inputs/numeric_input_test.rb +173 -0
  86. data/test/inputs/priority_input_test.rb +43 -0
  87. data/test/inputs/readonly_test.rb +61 -0
  88. data/test/inputs/required_test.rb +113 -0
  89. data/test/inputs/string_input_test.rb +140 -0
  90. data/test/inputs/text_input_test.rb +24 -0
  91. data/test/simple_form_test.rb +9 -0
  92. data/test/support/discovery_inputs.rb +21 -0
  93. data/test/support/misc_helpers.rb +102 -0
  94. data/test/support/mock_controller.rb +24 -0
  95. data/test/support/models.rb +210 -0
  96. data/test/test_helper.rb +90 -0
  97. metadata +210 -0
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class FormHelperTest < ActionView::TestCase
4
+
5
+ test 'SimpleForm for yields an instance of FormBuilder' do
6
+ simple_form_for :user do |f|
7
+ assert f.instance_of?(SimpleForm::FormBuilder)
8
+ end
9
+ end
10
+
11
+ test 'SimpleForm should add default class to form' do
12
+ with_concat_form_for(:user)
13
+ assert_select 'form.simple_form'
14
+ end
15
+
16
+ test 'SimpleForm should use default browser validations by default' do
17
+ with_concat_form_for(:user)
18
+ assert_no_select 'form[novalidate]'
19
+ end
20
+
21
+ test 'SimpleForm should not use default browser validations if specified in the configuration options' do
22
+ swap SimpleForm, :browser_validations => false do
23
+ with_concat_form_for(:user)
24
+ assert_select 'form[novalidate="novalidate"]'
25
+ end
26
+ end
27
+
28
+ test 'a form specific disabled validation option should override the default enabled browser validation configuration option' do
29
+ with_concat_form_for(:user, :html => { :novalidate => true })
30
+ assert_select 'form[novalidate="novalidate"]'
31
+ end
32
+
33
+ test 'a form specific enabled validation option should override the disabled browser validation configuration option' do
34
+ swap SimpleForm, :browser_validations => false do
35
+ with_concat_form_for(:user, :html => { :novalidate => false })
36
+ assert_no_select 'form[novalidate]'
37
+ end
38
+ end
39
+
40
+ test 'SimpleForm should add object name as css class to form when object is not present' do
41
+ with_concat_form_for(:user, :html => { :novalidate => true })
42
+ assert_select 'form.simple_form.user'
43
+ end
44
+
45
+ test 'SimpleForm should add :as option as css class to form when object is not present' do
46
+ with_concat_form_for(:user, :as => 'superuser')
47
+ assert_select 'form.simple_form.superuser'
48
+ end
49
+
50
+ test 'SimpleForm should add object class name with new prefix as css class to form if record is not persisted' do
51
+ @user.new_record!
52
+ with_concat_form_for(@user)
53
+ assert_select 'form.simple_form.new_user'
54
+ end
55
+
56
+ test 'SimpleForm should add :as option with new prefix as css class to form if record is not persisted' do
57
+ @user.new_record!
58
+ with_concat_form_for(@user, :as => 'superuser')
59
+ assert_select 'form.simple_form.new_superuser'
60
+ end
61
+
62
+ test 'SimpleForm should add edit class prefix as css class to form if record is persisted' do
63
+ with_concat_form_for(@user)
64
+ assert_select 'form.simple_form.edit_user'
65
+ end
66
+
67
+ test 'SimpleForm should add :as options with edit prefix as css class to form if record is persisted' do
68
+ with_concat_form_for(@user, :as => 'superuser')
69
+ assert_select 'form.simple_form.edit_superuser'
70
+ end
71
+
72
+ test 'SimpleForm should not add object class to form if css_class is specified' do
73
+ with_concat_form_for(:user, :html => {:class => nil})
74
+ assert_no_select 'form.user'
75
+ end
76
+
77
+ test 'SimpleForm should add custom class to form if css_class is specified' do
78
+ with_concat_form_for(:user, :html => {:class => 'my_class'})
79
+ assert_select 'form.my_class'
80
+ end
81
+
82
+ test 'pass options to SimpleForm' do
83
+ with_concat_form_for(:user, :url => '/account', :html => { :id => 'my_form' })
84
+ assert_select 'form#my_form'
85
+ assert_select 'form[action=/account]'
86
+ end
87
+
88
+ test 'fields for yields an instance of FormBuilder' do
89
+ with_concat_form_for(:user) do |f|
90
+ assert f.instance_of?(SimpleForm::FormBuilder)
91
+ end
92
+ end
93
+
94
+ test 'fields for with a hash like model yeilds an instance of FormBuilder' do
95
+ @hash_backed_author = HashBackedAuthor.new
96
+
97
+ with_concat_fields_for(:author, @hash_backed_author) do |f|
98
+ assert f.instance_of?(SimpleForm::FormBuilder)
99
+ f.input :name
100
+ end
101
+
102
+ assert_select "input[name='author[name]'][value='hash backed author']"
103
+ end
104
+ end
@@ -0,0 +1,310 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ # Isolated tests for label without triggering f.label.
5
+ class IsolatedLabelTest < ActionView::TestCase
6
+ setup do
7
+ SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html
8
+ end
9
+
10
+ def with_label_for(object, attribute_name, type, options={})
11
+ with_concat_form_for(object) do |f|
12
+ options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
13
+ SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label
14
+ end
15
+ end
16
+
17
+ test 'label should generate a default humanized description' do
18
+ with_label_for @user, :name, :string
19
+ assert_select 'label[for=user_name]', /Name/
20
+ end
21
+
22
+ test 'label should allow a customized description' do
23
+ with_label_for @user, :name, :string, :label => 'My label!'
24
+ assert_select 'label[for=user_name]', /My label!/
25
+ end
26
+
27
+ test 'label should use human attribute name from object when available' do
28
+ with_label_for @user, :description, :text
29
+ assert_select 'label[for=user_description]', /User Description!/
30
+ end
31
+
32
+ test 'label should use human attribute name based on association name' do
33
+ with_label_for @user, :company_id, :string, :setup_association => true
34
+ assert_select 'label', /Company Human Name!/
35
+ end
36
+
37
+ test 'label should use i18n based on model, action, and attribute to lookup translation' do
38
+ @controller.action_name = "new"
39
+ store_translations(:en, :simple_form => { :labels => { :user => {
40
+ :new => { :description => 'Nova descrição' }
41
+ } } } ) do
42
+ with_label_for @user, :description, :text
43
+ assert_select 'label[for=user_description]', /Nova descrição/
44
+ end
45
+ end
46
+
47
+ test 'label should fallback to new when action is create' do
48
+ @controller.action_name = "create"
49
+ store_translations(:en, :simple_form => { :labels => { :user => {
50
+ :new => { :description => 'Nova descrição' }
51
+ } } } ) do
52
+ with_label_for @user, :description, :text
53
+ assert_select 'label[for=user_description]', /Nova descrição/
54
+ end
55
+ end
56
+
57
+ test 'label should not explode while looking for i18n translation when action is not set' do
58
+ def @controller.action_name; nil; end
59
+
60
+ assert_nothing_raised do
61
+ with_label_for @user, :description, :text
62
+ end
63
+ assert_select 'label[for=user_description]'
64
+ end
65
+
66
+ test 'label should use i18n based on model and attribute to lookup translation' do
67
+ store_translations(:en, :simple_form => { :labels => { :user => {
68
+ :description => 'Descrição'
69
+ } } } ) do
70
+ with_label_for @user, :description, :text
71
+ assert_select 'label[for=user_description]', /Descrição/
72
+ end
73
+ end
74
+
75
+ test 'input should use i18n under defaults to lookup translation' do
76
+ store_translations(:en, :simple_form => { :labels => { :defaults => {:age => 'Idade'} } } ) do
77
+ with_label_for @user, :age, :integer
78
+ assert_select 'label[for=user_age]', /Idade/
79
+ end
80
+ end
81
+
82
+ test 'input should not use i18n label if translate is false' do
83
+ swap SimpleForm, :translate_labels => false do
84
+ store_translations(:en, :simple_form => { :labels => { :defaults => {:age => 'Idade'} } } ) do
85
+ with_label_for @user, :age, :integer
86
+ assert_select 'label[for=user_age]', /Age/
87
+ end
88
+ end
89
+ end
90
+
91
+ test 'label should use i18n with lookup for association name' do
92
+ store_translations(:en, :simple_form => { :labels => {
93
+ :user => { :company => 'My company!' }
94
+ } } ) do
95
+ with_label_for @user, :company_id, :string, :setup_association => true
96
+ assert_select 'label[for=user_company_id]', /My company!/
97
+ end
98
+ end
99
+
100
+ test 'label should do correct i18n lookup for nested models with nested translation' do
101
+ @user.company = Company.new(1, 'Empresa')
102
+
103
+ store_translations(:en, :simple_form => { :labels => {
104
+ :user => { :name => 'Usuario', :company => { :name => 'Nome da empresa' } }
105
+ } } ) do
106
+ with_concat_form_for @user do |f|
107
+ concat f.input :name
108
+ concat(f.simple_fields_for(:company) do |company_form|
109
+ concat(company_form.input :name)
110
+ end)
111
+ end
112
+
113
+ assert_select 'label[for=user_name]', /Usuario/
114
+ assert_select 'label[for=user_company_attributes_name]', /Nome da empresa/
115
+ end
116
+ end
117
+
118
+ test 'label should do correct i18n lookup for nested models with no nested translation' do
119
+ @user.company = Company.new(1, 'Empresa')
120
+
121
+ store_translations(:en, :simple_form => { :labels => {
122
+ :user => { :name => 'Usuario' },
123
+ :company => { :name => 'Nome da empresa' }
124
+ } } ) do
125
+ with_concat_form_for @user do |f|
126
+ concat f.input :name
127
+ concat(f.simple_fields_for(:company) do |company_form|
128
+ concat(company_form.input :name)
129
+ end)
130
+ end
131
+
132
+ assert_select 'label[for=user_name]', /Usuario/
133
+ assert_select 'label[for=user_company_attributes_name]', /Nome da empresa/
134
+ end
135
+ end
136
+
137
+ test 'label should do correct i18n lookup for nested has_many models with no nested translation' do
138
+ @user.tags = [Tag.new(1, 'Empresa')]
139
+
140
+ store_translations(:en, :simple_form => { :labels => {
141
+ :user => { :name => 'Usuario' },
142
+ :tags => { :name => 'Nome da empresa' }
143
+ } } ) do
144
+ with_concat_form_for @user do |f|
145
+ concat f.input :name
146
+ concat(f.simple_fields_for(:tags, :child_index => "new_index") do |tags_form|
147
+ concat(tags_form.input :name)
148
+ end)
149
+ end
150
+
151
+ assert_select 'label[for=user_name]', /Usuario/
152
+ assert_select 'label[for=user_tags_attributes_new_index_name]', /Nome da empresa/
153
+ end
154
+ end
155
+
156
+ test 'label should have css class from type' do
157
+ with_label_for @user, :name, :string
158
+ assert_select 'label.string'
159
+ with_label_for @user, :description, :text
160
+ assert_select 'label.text'
161
+ with_label_for @user, :age, :integer
162
+ assert_select 'label.integer'
163
+ with_label_for @user, :born_at, :date
164
+ assert_select 'label.date'
165
+ with_label_for @user, :created_at, :datetime
166
+ assert_select 'label.datetime'
167
+ end
168
+
169
+ test 'label should not have css class from type when generate_additional_classes_for does not include :label' do
170
+ swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
171
+ with_label_for @user, :name, :string
172
+ assert_no_select 'label.string'
173
+ with_label_for @user, :description, :text
174
+ assert_no_select 'label.text'
175
+ with_label_for @user, :age, :integer
176
+ assert_no_select 'label.integer'
177
+ with_label_for @user, :born_at, :date
178
+ assert_no_select 'label.date'
179
+ with_label_for @user, :created_at, :datetime
180
+ assert_no_select 'label.datetime'
181
+ end
182
+ end
183
+
184
+ test 'label should obtain required from ActiveModel::Validations when it is included' do
185
+ with_label_for @validating_user, :name, :string
186
+ assert_select 'label.required'
187
+ with_label_for @validating_user, :status, :string
188
+ assert_select 'label.optional'
189
+ end
190
+
191
+ test 'label should not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
192
+ swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
193
+ with_label_for @validating_user, :name, :string
194
+ assert_no_select 'label.required'
195
+ with_label_for @validating_user, :status, :string
196
+ assert_no_select 'label.optional'
197
+ end
198
+ end
199
+
200
+ test 'label should allow overriding required when ActiveModel::Validations is included' do
201
+ with_label_for @validating_user, :name, :string, :required => false
202
+ assert_select 'label.optional'
203
+ with_label_for @validating_user, :status, :string, :required => true
204
+ assert_select 'label.required'
205
+ end
206
+
207
+ test 'label should be required by default when ActiveModel::Validations is not included' do
208
+ with_label_for @user, :name, :string
209
+ assert_select 'label.required'
210
+ end
211
+
212
+ test 'label should be able to disable required when ActiveModel::Validations is not included' do
213
+ with_label_for @user, :name, :string, :required => false
214
+ assert_no_select 'label.required'
215
+ end
216
+
217
+ test 'label should add required text when required' do
218
+ with_label_for @user, :name, :string
219
+ assert_select 'label.required abbr[title=required]', '*'
220
+ end
221
+
222
+ test 'label should not have required text in no required inputs' do
223
+ with_label_for @user, :name, :string, :required => false
224
+ assert_no_select 'form label abbr'
225
+ end
226
+
227
+ test 'label should use i18n to find required text' do
228
+ store_translations(:en, :simple_form => { :required => { :text => 'campo requerido' }}) do
229
+ with_label_for @user, :name, :string
230
+ assert_select 'form label abbr[title=campo requerido]', '*'
231
+ end
232
+ end
233
+
234
+ test 'label should use i18n to find required mark' do
235
+ store_translations(:en, :simple_form => { :required => { :mark => '*-*' }}) do
236
+ with_label_for @user, :name, :string
237
+ assert_select 'form label abbr', '*-*'
238
+ end
239
+ end
240
+
241
+ test 'label should use i18n to find required string tag' do
242
+ store_translations(:en, :simple_form => { :required => { :html => '<span class="required" title="requerido">*</span>' }}) do
243
+ with_label_for @user, :name, :string
244
+ assert_no_select 'form label abbr'
245
+ assert_select 'form label span.required[title=requerido]', '*'
246
+ end
247
+ end
248
+
249
+ test 'label should allow overwriting input id' do
250
+ with_label_for @user, :name, :string, :input_html => { :id => 'my_new_id' }
251
+ assert_select 'label[for=my_new_id]'
252
+ end
253
+
254
+ test 'label should allow overwriting of for attribute' do
255
+ with_label_for @user, :name, :string, :label_html => { :for => 'my_new_id' }
256
+ assert_select 'label[for=my_new_id]'
257
+ end
258
+
259
+ test 'label should allow overwriting of for attribute with input_html not containing id' do
260
+ with_label_for @user, :name, :string, :label_html => { :for => 'my_new_id' }, :input_html => {:class => 'foo'}
261
+ assert_select 'label[for=my_new_id]'
262
+ end
263
+
264
+ test 'label should use default input id when it was not overridden' do
265
+ with_label_for @user, :name, :string, :input_html => { :class => 'my_new_id' }
266
+ assert_select 'label[for=user_name]'
267
+ end
268
+
269
+ test 'label should be generated properly when object is not present' do
270
+ with_label_for :project, :name, :string
271
+ assert_select 'label[for=project_name]', /Name/
272
+ end
273
+
274
+ test 'label should include for attribute for select collection' do
275
+ with_label_for @user, :sex, :select, :collection => [:male, :female]
276
+ assert_select 'label[for=user_sex]'
277
+ end
278
+
279
+ test 'label should use i18n properly when object is not present' do
280
+ store_translations(:en, :simple_form => { :labels => {
281
+ :project => { :name => 'Nome' }
282
+ } } ) do
283
+ with_label_for :project, :name, :string
284
+ assert_select 'label[for=project_name]', /Nome/
285
+ end
286
+ end
287
+
288
+ test 'label should add required by default when object is not present' do
289
+ with_label_for :project, :name, :string
290
+ assert_select 'label.required[for=project_name]'
291
+ with_label_for :project, :description, :string, :required => false
292
+ assert_no_select 'label.required[for=project_description]'
293
+ end
294
+
295
+ test 'label should add chosen label class' do
296
+ swap SimpleForm, :label_class => :my_custom_class do
297
+ with_label_for @user, :name, :string
298
+ assert_select 'label.my_custom_class'
299
+ end
300
+ end
301
+
302
+ test 'label strips extra classes even when label_class is nil' do
303
+ swap SimpleForm, :label_class => nil do
304
+ with_label_for @user, :name, :string
305
+ assert_select "label[class='string required']"
306
+ assert_no_select "label[class='string required ']"
307
+ assert_no_select "label[class=' string required']"
308
+ end
309
+ end
310
+ end
@@ -0,0 +1,177 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ class AssociationTest < ActionView::TestCase
5
+ def with_association_for(object, *args)
6
+ with_concat_form_for(object) do |f|
7
+ f.association(*args)
8
+ end
9
+ end
10
+
11
+ test 'builder should not allow creating an association input when no object exists' do
12
+ assert_raise ArgumentError do
13
+ with_association_for :post, :author
14
+ end
15
+ end
16
+
17
+ test 'builder association with a block calls simple_fields_for' do
18
+ simple_form_for @user do |f|
19
+ f.association :posts do |posts_form|
20
+ assert posts_form.instance_of?(SimpleForm::FormBuilder)
21
+ end
22
+ end
23
+ end
24
+
25
+ test 'builder association forwards collection to simple_fields_for' do
26
+ calls = 0
27
+ simple_form_for @user do |f|
28
+ f.association :company, :collection => Company.all do |c|
29
+ calls += 1
30
+ end
31
+ end
32
+
33
+ assert_equal 3, calls
34
+ end
35
+
36
+ test 'builder association marks input as required based on both association and attribute' do
37
+ swap SimpleForm, :required_by_default => false do
38
+ with_association_for @validating_user, :company, :collection => []
39
+ assert_select 'label.required'
40
+ end
41
+ end
42
+
43
+ test 'builder preloads collection association' do
44
+ value = @user.tags
45
+ value.expects(:to_a).returns(value)
46
+ with_association_for @user, :tags
47
+ assert_select 'form select.select#user_tag_ids'
48
+ assert_select 'form select option[value=1]', 'Tag 1'
49
+ assert_select 'form select option[value=2]', 'Tag 2'
50
+ assert_select 'form select option[value=3]', 'Tag 3'
51
+ end
52
+
53
+ test 'builder does not preload collection association if preload is false' do
54
+ value = @user.company
55
+ value.expects(:to_a).never
56
+ with_association_for @user, :company, :preload => false
57
+ assert_select 'form select.select#user_company_id'
58
+ assert_select 'form select option[value=1]', 'Company 1'
59
+ assert_select 'form select option[value=2]', 'Company 2'
60
+ assert_select 'form select option[value=3]', 'Company 3'
61
+ end
62
+
63
+ test 'builder does not preload non-collection association' do
64
+ value = @user.company
65
+ value.expects(:to_a).never
66
+ with_association_for @user, :company, :preload => false
67
+ assert_select 'form select.select#user_company_id'
68
+ assert_select 'form select option[value=1]', 'Company 1'
69
+ assert_select 'form select option[value=2]', 'Company 2'
70
+ assert_select 'form select option[value=3]', 'Company 3'
71
+ end
72
+
73
+ # ASSOCIATIONS - BELONGS TO
74
+ test 'builder creates a select for belongs_to associations' do
75
+ with_association_for @user, :company
76
+ assert_select 'form select.select#user_company_id'
77
+ assert_select 'form select option[value=1]', 'Company 1'
78
+ assert_select 'form select option[value=2]', 'Company 2'
79
+ assert_select 'form select option[value=3]', 'Company 3'
80
+ end
81
+
82
+ test 'builder allows collection radio for belongs_to associations' do
83
+ with_association_for @user, :company, :as => :radio_buttons
84
+ assert_select 'form input.radio_buttons#user_company_id_1'
85
+ assert_select 'form input.radio_buttons#user_company_id_2'
86
+ assert_select 'form input.radio_buttons#user_company_id_3'
87
+ end
88
+
89
+ test 'builder marks the record which already belongs to the user' do
90
+ @user.company_id = 2
91
+ with_association_for @user, :company, :as => :radio_buttons
92
+ assert_no_select 'form input.radio_buttons#user_company_id_1[checked=checked]'
93
+ assert_select 'form input.radio_buttons#user_company_id_2[checked=checked]'
94
+ assert_no_select 'form input.radio_buttons#user_company_id_3[checked=checked]'
95
+ end
96
+
97
+ # ASSOCIATIONS - FINDERS
98
+ test 'builder should use reflection conditions to find collection' do
99
+ with_association_for @user, :special_company
100
+ assert_select 'form select.select#user_special_company_id'
101
+ assert_select 'form select option[value=1]'
102
+ assert_no_select 'form select option[value=2]'
103
+ assert_no_select 'form select option[value=3]'
104
+ end
105
+
106
+ test 'builder should allow overriding collection to association input' do
107
+ with_association_for @user, :company, :include_blank => false,
108
+ :collection => [Company.new(999, 'Teste')]
109
+ assert_select 'form select.select#user_company_id'
110
+ assert_no_select 'form select option[value=1]'
111
+ assert_select 'form select option[value=999]', 'Teste'
112
+ assert_select 'form select option', :count => 1
113
+ end
114
+
115
+ # ASSOCIATIONS - has_*
116
+ test 'builder does not allow has_one associations' do
117
+ assert_raise RuntimeError do
118
+ with_association_for @user, :first_company, :as => :radio_buttons
119
+ end
120
+ end
121
+
122
+ test 'builder creates a select with multiple options for collection associations' do
123
+ with_association_for @user, :tags
124
+ assert_select 'form select.select#user_tag_ids'
125
+ assert_select 'form select[multiple=multiple][size=5]'
126
+ assert_select 'form select option[value=1]', 'Tag 1'
127
+ assert_select 'form select option[value=2]', 'Tag 2'
128
+ assert_select 'form select option[value=3]', 'Tag 3'
129
+ end
130
+
131
+ test 'builder allows size to be overwritten for collection associations' do
132
+ with_association_for @user, :tags, :input_html => { :size => 10 }
133
+ assert_select 'form select[multiple=multiple][size=10]'
134
+ end
135
+
136
+ test 'builder marks all selected records which already belongs to user' do
137
+ @user.tag_ids = [1, 2]
138
+ with_association_for @user, :tags
139
+ assert_select 'form select option[value=1][selected=selected]'
140
+ assert_select 'form select option[value=2][selected=selected]'
141
+ assert_no_select 'form select option[value=3][selected=selected]'
142
+ end
143
+
144
+ test 'builder allows a collection of check boxes for collection associations' do
145
+ @user.tag_ids = [1, 2]
146
+ with_association_for @user, :tags, :as => :check_boxes
147
+ assert_select 'form input#user_tag_ids_1[type=checkbox]'
148
+ assert_select 'form input#user_tag_ids_2[type=checkbox]'
149
+ assert_select 'form input#user_tag_ids_3[type=checkbox]'
150
+ end
151
+
152
+ test 'builder marks all selected records for collection boxes' do
153
+ @user.tag_ids = [1, 2]
154
+ with_association_for @user, :tags, :as => :check_boxes
155
+ assert_select 'form input[type=checkbox][value=1][checked=checked]'
156
+ assert_select 'form input[type=checkbox][value=2][checked=checked]'
157
+ assert_no_select 'form input[type=checkbox][value=3][checked=checked]'
158
+ end
159
+
160
+ test 'builder with collection support giving collection and item wrapper tags' do
161
+ with_association_for @user, :tags, :as => :check_boxes,
162
+ :collection_wrapper_tag => :ul, :item_wrapper_tag => :li
163
+
164
+ assert_select 'form ul', :count => 1
165
+ assert_select 'form ul li', :count => 3
166
+ end
167
+
168
+ test 'builder with collection support should not change the options hash' do
169
+ options = { :as => :check_boxes, :collection_wrapper_tag => :ul, :item_wrapper_tag => :li}
170
+ with_association_for @user, :tags, options
171
+
172
+ assert_select 'form ul', :count => 1
173
+ assert_select 'form ul li', :count => 3
174
+ assert_equal({ :as => :check_boxes, :collection_wrapper_tag => :ul, :item_wrapper_tag => :li},
175
+ options)
176
+ end
177
+ end