techthumb-formtastic 1.rails3.sha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +584 -0
  3. data/Rakefile +127 -0
  4. data/generators/form/USAGE +16 -0
  5. data/generators/form/form_generator.rb +120 -0
  6. data/generators/form/templates/view__form.html.erb +5 -0
  7. data/generators/form/templates/view__form.html.haml +4 -0
  8. data/generators/formtastic/formtastic_generator.rb +24 -0
  9. data/generators/formtastic/templates/formtastic.css +131 -0
  10. data/generators/formtastic/templates/formtastic.rb +54 -0
  11. data/generators/formtastic/templates/formtastic_changes.css +14 -0
  12. data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
  13. data/init.rb +5 -0
  14. data/lib/formtastic/i18n.rb +35 -0
  15. data/lib/formtastic/layout_helper.rb +10 -0
  16. data/lib/formtastic/railtie.rb +12 -0
  17. data/lib/formtastic/util.rb +36 -0
  18. data/lib/formtastic.rb +1870 -0
  19. data/lib/generators/formtastic/form/form_generator.rb +86 -0
  20. data/lib/generators/formtastic/install/install_generator.rb +24 -0
  21. data/lib/locale/en.yml +8 -0
  22. data/rails/init.rb +2 -0
  23. data/spec/buttons_spec.rb +166 -0
  24. data/spec/commit_button_spec.rb +401 -0
  25. data/spec/custom_builder_spec.rb +98 -0
  26. data/spec/defaults_spec.rb +20 -0
  27. data/spec/error_proc_spec.rb +27 -0
  28. data/spec/errors_spec.rb +105 -0
  29. data/spec/form_helper_spec.rb +142 -0
  30. data/spec/helpers/layout_helper_spec.rb +21 -0
  31. data/spec/i18n_spec.rb +152 -0
  32. data/spec/include_blank_spec.rb +74 -0
  33. data/spec/input_spec.rb +786 -0
  34. data/spec/inputs/boolean_input_spec.rb +104 -0
  35. data/spec/inputs/check_boxes_input_spec.rb +426 -0
  36. data/spec/inputs/country_input_spec.rb +118 -0
  37. data/spec/inputs/date_input_spec.rb +168 -0
  38. data/spec/inputs/datetime_input_spec.rb +310 -0
  39. data/spec/inputs/file_input_spec.rb +34 -0
  40. data/spec/inputs/hidden_input_spec.rb +78 -0
  41. data/spec/inputs/numeric_input_spec.rb +44 -0
  42. data/spec/inputs/password_input_spec.rb +46 -0
  43. data/spec/inputs/radio_input_spec.rb +243 -0
  44. data/spec/inputs/select_input_spec.rb +546 -0
  45. data/spec/inputs/string_input_spec.rb +64 -0
  46. data/spec/inputs/text_input_spec.rb +34 -0
  47. data/spec/inputs/time_input_spec.rb +206 -0
  48. data/spec/inputs/time_zone_input_spec.rb +110 -0
  49. data/spec/inputs_spec.rb +476 -0
  50. data/spec/label_spec.rb +89 -0
  51. data/spec/semantic_errors_spec.rb +98 -0
  52. data/spec/semantic_fields_for_spec.rb +45 -0
  53. data/spec/spec.opts +2 -0
  54. data/spec/spec_helper.rb +289 -0
  55. data/spec/support/custom_macros.rb +494 -0
  56. data/spec/support/output_buffer.rb +4 -0
  57. data/spec/support/test_environment.rb +45 -0
  58. metadata +235 -0
@@ -0,0 +1,104 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'boolean input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+
12
+ @form = semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:allow_comments, :as => :boolean))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("boolean")
18
+ it_should_have_input_wrapper_with_id("post_allow_comments_input")
19
+ it_should_apply_error_logic_for_input_type(:boolean)
20
+
21
+ it 'should generate a label containing the input' do
22
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
23
+ output_buffer.should have_tag('form li label', :count => 1)
24
+ output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
25
+ output_buffer.should have_tag('form li label', /Allow comments/)
26
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
27
+ end
28
+
29
+ it 'should generate a checkbox input' do
30
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
31
+ output_buffer.should have_tag('form li label input')
32
+ output_buffer.should have_tag('form li label input#post_allow_comments')
33
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
34
+ output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
35
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
36
+ end
37
+
38
+ it 'should allow checked and unchecked values to be sent' do
39
+ form = semantic_form_for(@new_post) do |builder|
40
+ concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
41
+ end
42
+
43
+ output_buffer.concat(form) if Formtastic::Util.rails3?
44
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]:not([@unchecked_value][@checked_value])')
45
+ output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
46
+ end
47
+
48
+ it 'should generate a label and a checkbox even if no object is given' do
49
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
50
+ concat(builder.input(:allow_comments, :as => :boolean))
51
+ end
52
+
53
+ output_buffer.concat(form) if Formtastic::Util.rails3?
54
+
55
+ output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
56
+ output_buffer.should have_tag('form li label', /Allow comments/)
57
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
58
+
59
+ output_buffer.should have_tag('form li label input#project_allow_comments')
60
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
61
+ output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
62
+ end
63
+
64
+ describe 'when :selected is set' do
65
+ before do
66
+ @output_buffer = ''
67
+ end
68
+
69
+ describe "not selected" do
70
+ before do
71
+ @new_post.stub!(:allow_comments).and_return(true)
72
+
73
+ with_deprecation_silenced do
74
+ @form = semantic_form_for(@new_post) do |builder|
75
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => false))
76
+ end
77
+ end
78
+ end
79
+
80
+ it 'should not be selected' do
81
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
82
+ output_buffer.should_not have_tag("form li label input[@type='checkbox'][@checked='checked']")
83
+ end
84
+ end
85
+
86
+ describe "selected" do
87
+ before do
88
+ @new_post.stub!(:allow_comments).and_return(false)
89
+
90
+ with_deprecation_silenced do
91
+ @form = semantic_form_for(@new_post) do |builder|
92
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => true))
93
+ end
94
+ end
95
+ end
96
+
97
+ it 'should be selected' do
98
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
99
+ output_buffer.should have_tag("form li label input[@type='checkbox'][@checked='checked']")
100
+ end
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,426 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'check_boxes input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ describe 'for a has_many association' do
9
+ before do
10
+ @output_buffer = ''
11
+ mock_everything
12
+
13
+ @form = semantic_form_for(@fred) do |builder|
14
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
15
+ end
16
+ end
17
+
18
+ it_should_have_input_wrapper_with_class("check_boxes")
19
+ it_should_have_input_wrapper_with_id("author_posts_input")
20
+ it_should_have_a_nested_fieldset
21
+ it_should_apply_error_logic_for_input_type(:check_boxes)
22
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes)
23
+ it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
24
+
25
+ it 'should generate a legend containing a label with text for the input' do
26
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
27
+ output_buffer.should have_tag('form li fieldset legend.label label')
28
+ output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
29
+ end
30
+
31
+ it 'should not link the label within the legend to any input' do
32
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
33
+ output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
34
+ end
35
+
36
+
37
+ it 'should generate an ordered list with a list item for each choice' do
38
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
39
+ output_buffer.should have_tag('form li fieldset ol')
40
+ output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Post.find(:all).size)
41
+ end
42
+
43
+ it 'should have one option with a "checked" attribute' do
44
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
45
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
46
+ end
47
+
48
+ it 'should not generate hidden inputs with default value blank' do
49
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
50
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
51
+ end
52
+
53
+ describe "each choice" do
54
+
55
+ it 'should contain a label for the radio input with a nested input and label text' do
56
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
57
+ ::Post.find(:all).each do |post|
58
+ output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
59
+ output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
60
+ end
61
+ end
62
+
63
+ it 'should use values as li.class when value_as_class is true' do
64
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
65
+ ::Post.find(:all).each do |post|
66
+ output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
67
+ end
68
+ end
69
+
70
+ it 'should have a checkbox input but no hidden field for each post' do
71
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
72
+ ::Post.find(:all).each do |post|
73
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
74
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
75
+ end
76
+ end
77
+
78
+ it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
79
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
80
+ output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
81
+ end
82
+
83
+ it 'the hidden field with an empty array value should be followed by the ol' do
84
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
85
+ output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
86
+ end
87
+
88
+ it 'should not have a hidden field with an empty string value for the collection' do
89
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
90
+ output_buffer.should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
91
+ end
92
+
93
+ it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
94
+ output_buffer.replace ''
95
+
96
+ form = semantic_form_for(@fred) do |builder|
97
+ concat(builder.input(:posts, :as => :check_boxes, :hidden_fields => true, :value_as_class => true))
98
+ end
99
+ output_buffer.concat(form) if Formtastic::Util.rails3?
100
+
101
+ ::Post.find(:all).each do |post|
102
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
103
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
104
+ end
105
+
106
+ end
107
+
108
+ it "should mark input as checked if it's the the existing choice" do
109
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
110
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
111
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
112
+ end
113
+ end
114
+
115
+ describe 'and no object is given' do
116
+ before(:each) do
117
+ output_buffer.replace ''
118
+ @form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
119
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
120
+ end
121
+ end
122
+
123
+ it 'should generate a fieldset with legend' do
124
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
125
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
126
+ end
127
+
128
+ it 'shold generate an li tag for each item in the collection' do
129
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
130
+ output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.find(:all).size)
131
+ end
132
+
133
+ it 'should generate labels for each item' do
134
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
135
+ ::Author.find(:all).each do |author|
136
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
137
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
138
+ end
139
+ end
140
+
141
+ it 'should generate inputs for each item' do
142
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
143
+ ::Author.find(:all).each do |author|
144
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
145
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
146
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
147
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
148
+ end
149
+ end
150
+
151
+ it 'should html escape the label string' do
152
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
153
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
154
+ end
155
+ output_buffer.concat(form) if Formtastic::Util.rails3?
156
+ output_buffer.should have_tag('form li fieldset ol li label') do |label|
157
+ label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
158
+ end
159
+ end
160
+ end
161
+
162
+ describe 'when :hidden_fields is set to false' do
163
+ before do
164
+ @output_buffer = ''
165
+ mock_everything
166
+
167
+ form = semantic_form_for(@fred) do |builder|
168
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true, :hidden_fields => false))
169
+ end
170
+ output_buffer.concat(form) if Formtastic::Util.rails3?
171
+ end
172
+
173
+ it 'should have a checkbox input for each post' do
174
+ ::Post.find(:all).each do |post|
175
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
176
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.find(:all).length)
177
+ end
178
+ end
179
+
180
+ it "should mark input as checked if it's the the existing choice" do
181
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
182
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
183
+ end
184
+
185
+ it 'should not generate empty hidden inputs' do
186
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).length)
187
+ end
188
+ end
189
+
190
+ describe 'when :selected is set' do
191
+ before do
192
+ @output_buffer = ''
193
+ end
194
+
195
+ describe "no selected items" do
196
+ before do
197
+ @new_post.stub!(:author_ids).and_return(nil)
198
+
199
+ with_deprecation_silenced do
200
+ @form = semantic_form_for(@new_post) do |builder|
201
+ concat(builder.input(:authors, :as => :check_boxes, :selected => nil))
202
+ end
203
+ end
204
+ end
205
+
206
+ it 'should not have any selected item(s)' do
207
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
208
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']")
209
+ end
210
+ end
211
+
212
+ describe "single selected item" do
213
+ before do
214
+ @new_post.stub!(:author_ids).and_return(nil)
215
+
216
+ with_deprecation_silenced do
217
+ @form = semantic_form_for(@new_post) do |builder|
218
+ concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id))
219
+ end
220
+ end
221
+ end
222
+
223
+ it "should have one item selected; the specified one" do
224
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
225
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 1)
226
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
227
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']")
228
+ end
229
+ end
230
+
231
+ describe "multiple selected items" do
232
+ before do
233
+ @new_post.stub!(:author_ids).and_return(nil)
234
+
235
+ with_deprecation_silenced do
236
+ @form = semantic_form_for(@new_post) do |builder|
237
+ concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id]))
238
+ end
239
+ end
240
+ end
241
+
242
+ it "should have multiple items selected; the specified ones" do
243
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
244
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 2)
245
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
246
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@bob.id}']")
247
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
248
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']")
249
+ end
250
+ end
251
+
252
+ end
253
+
254
+ it 'should warn about :selected deprecation' do
255
+ with_deprecation_silenced do
256
+ ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
257
+ semantic_form_for(@new_post) do |builder|
258
+ concat(builder.input(:authors, :as => :check_boxes, :selected => @bob.id))
259
+ end
260
+ end
261
+ end
262
+
263
+
264
+ describe 'when :disabled is set' do
265
+ before do
266
+ @output_buffer = ''
267
+ end
268
+
269
+ describe "no disabled items" do
270
+ before do
271
+ @new_post.stub!(:author_ids).and_return(nil)
272
+
273
+ @form = semantic_form_for(@new_post) do |builder|
274
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => nil))
275
+ end
276
+ end
277
+
278
+ it 'should not have any disabled item(s)' do
279
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
280
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
281
+ end
282
+ end
283
+
284
+ describe "single disabled item" do
285
+ before do
286
+ @new_post.stub!(:author_ids).and_return(nil)
287
+
288
+ @form = semantic_form_for(@new_post) do |builder|
289
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id))
290
+ end
291
+ end
292
+
293
+ it "should have one item disabled; the specified one" do
294
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
295
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
296
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
297
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
298
+ end
299
+ end
300
+
301
+ describe "multiple disabled items" do
302
+ before do
303
+ @new_post.stub!(:author_ids).and_return(nil)
304
+
305
+ @form = semantic_form_for(@new_post) do |builder|
306
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id]))
307
+ end
308
+ end
309
+
310
+ it "should have multiple items disabled; the specified ones" do
311
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
312
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
313
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
314
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
315
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
316
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
317
+ end
318
+ end
319
+
320
+ end
321
+
322
+ describe "with i18n of the legend label" do
323
+
324
+ before do
325
+ ::I18n.backend.store_translations :en, :formtastic => { :labels => { :post => { :authors => "Translated!" }}}
326
+ Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
327
+
328
+ @new_post.stub!(:author_ids).and_return(nil)
329
+ @form = semantic_form_for(@new_post) do |builder|
330
+ concat(builder.input(:authors, :as => :check_boxes))
331
+ end
332
+ end
333
+
334
+ after do
335
+ ::I18n.backend.reload!
336
+ Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
337
+ end
338
+
339
+ it "should do foo" do
340
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
341
+ output_buffer.should have_tag("legend.label label", /Translated/)
342
+ end
343
+
344
+ end
345
+
346
+ describe "when :label option is set" do
347
+ before do
348
+ @new_post.stub!(:author_ids).and_return(nil)
349
+ @form = semantic_form_for(@new_post) do |builder|
350
+ concat(builder.input(:authors, :as => :check_boxes, :label => 'The authors'))
351
+ end
352
+ end
353
+
354
+ it "should output the correct label title" do
355
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
356
+ output_buffer.should have_tag("legend.label label", /The authors/)
357
+ end
358
+ end
359
+
360
+ describe "when :label option is false" do
361
+ before do
362
+ @output_buffer = ''
363
+ @new_post.stub!(:author_ids).and_return(nil)
364
+ @form = semantic_form_for(@new_post) do |builder|
365
+ concat(builder.input(:authors, :as => :check_boxes, :label => false))
366
+ end
367
+ end
368
+
369
+ it "should not output the legend" do
370
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
371
+ output_buffer.should_not have_tag("legend.label")
372
+ end
373
+ end
374
+ end
375
+
376
+ describe 'for a has_and_belongs_to_many association' do
377
+
378
+ before do
379
+ @output_buffer = ''
380
+ mock_everything
381
+
382
+ @form = semantic_form_for(@freds_post) do |builder|
383
+ concat(builder.input(:authors, :as => :check_boxes))
384
+ end
385
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
386
+ end
387
+
388
+ it 'should render checkboxes' do
389
+ # I'm aware these two lines test the same thing
390
+ output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
391
+ output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.find(:all).size)
392
+ end
393
+
394
+ it 'should only select checkboxes that are present in the association' do
395
+ # I'm aware these two lines test the same thing
396
+ output_buffer.should have_tag('input[checked="checked"]', :count => 1)
397
+ output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
398
+ end
399
+
400
+ end
401
+
402
+ describe 'for an association when a :collection is provided' do
403
+ describe 'it should use the specified :value_method option' do
404
+ before do
405
+ @output_buffer = ''
406
+ mock_everything
407
+ end
408
+
409
+ it 'to set the right input value' do
410
+ item = mock('item')
411
+ item.should_not_receive(:id)
412
+ item.stub!(:custom_value).and_return('custom_value')
413
+ item.should_receive(:custom_value).exactly(3).times
414
+ @new_post.author.should_receive(:custom_value)
415
+ @form = semantic_form_for(@new_post) do |builder|
416
+ concat(builder.input(:author, :as => :check_boxes, :value_method => :custom_value, :collection => [item, item, item]))
417
+ end
418
+
419
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
420
+ output_buffer.should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
421
+ end
422
+ end
423
+ end
424
+
425
+ end
426
+
@@ -0,0 +1,118 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'country input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe "when country_select is not available as a helper from a plugin" do
14
+
15
+ it "should raise an error, sugesting the author installs a plugin" do
16
+ lambda {
17
+ semantic_form_for(@new_post) do |builder|
18
+ concat(builder.input(:country, :as => :country))
19
+ end
20
+ }.should raise_error
21
+ end
22
+
23
+ end
24
+
25
+ describe "when country_select is available as a helper (from a plugin)" do
26
+
27
+ before do
28
+ @form = semantic_form_for(@new_post) do |builder|
29
+ builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
30
+ concat(builder.input(:country, :as => :country))
31
+ end
32
+ end
33
+
34
+ it_should_have_input_wrapper_with_class("country")
35
+ it_should_have_input_wrapper_with_id("post_country_input")
36
+
37
+ # TODO -- needs stubbing inside the builder block, tricky!
38
+ #it_should_apply_error_logic_for_input_type(:country)
39
+
40
+ it 'should generate a label for the input' do
41
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
42
+ output_buffer.should have_tag('form li label')
43
+ output_buffer.should have_tag('form li label[@for="post_country"]')
44
+ output_buffer.should have_tag('form li label', /Country/)
45
+ end
46
+
47
+ it "should generate a select" do
48
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
49
+ output_buffer.should have_tag("form li select")
50
+ end
51
+
52
+ end
53
+
54
+ describe ":priority_countries option" do
55
+
56
+ it "should be passed down to the country_select helper when provided" do
57
+ priority_countries = ["Foo", "Bah"]
58
+ semantic_form_for(@new_post) do |builder|
59
+ builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
60
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
61
+
62
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
63
+ end
64
+ end
65
+
66
+ it "should default to the @@priority_countries config when absent" do
67
+ priority_countries = ::Formtastic::SemanticFormBuilder.priority_countries
68
+ priority_countries.should_not be_empty
69
+ priority_countries.should_not be_nil
70
+
71
+ semantic_form_for(@new_post) do |builder|
72
+ builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
73
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
74
+
75
+ concat(builder.input(:country, :as => :country))
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ describe "matching" do
82
+
83
+ describe "when the attribute is 'country'" do
84
+
85
+ before do
86
+ @form = semantic_form_for(@new_post) do |builder|
87
+ builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
88
+ concat(builder.input(:country))
89
+ end
90
+ end
91
+
92
+ it "should render a country input" do
93
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
94
+ output_buffer.should have_tag "form li.country"
95
+ end
96
+ end
97
+
98
+ describe "whent the attribute is 'country_something'" do
99
+
100
+ before do
101
+ @form = semantic_form_for(@new_post) do |builder|
102
+ concat(builder.input(:country_subdivision))
103
+ concat(builder.input(:country_code))
104
+ end
105
+ end
106
+
107
+ it "should render a country input" do
108
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
109
+ output_buffer.should_not have_tag "form li.country"
110
+ output_buffer.should have_tag "form li.string", :count => 2
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+