formtastic-rails3 0.9.7

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 (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +558 -0
  3. data/Rakefile +103 -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 +144 -0
  10. data/generators/formtastic/templates/formtastic.rb +54 -0
  11. data/generators/formtastic/templates/formtastic_changes.css +10 -0
  12. data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
  13. data/lib/formtastic.rb +1724 -0
  14. data/lib/formtastic/i18n.rb +32 -0
  15. data/lib/formtastic/layout_helper.rb +10 -0
  16. data/lib/generators/formtastic/form/form_generator.rb +85 -0
  17. data/lib/generators/formtastic/form/templates/_form.html.erb +5 -0
  18. data/lib/generators/formtastic/form/templates/_form.html.haml +4 -0
  19. data/lib/generators/formtastic/install/install_generator.rb +23 -0
  20. data/lib/generators/formtastic/install/templates/formtastic.css +144 -0
  21. data/lib/generators/formtastic/install/templates/formtastic.rb +58 -0
  22. data/lib/generators/formtastic/install/templates/formtastic_changes.css +10 -0
  23. data/lib/locale/en.yml +8 -0
  24. data/spec/buttons_spec.rb +149 -0
  25. data/spec/commit_button_spec.rb +377 -0
  26. data/spec/custom_builder_spec.rb +62 -0
  27. data/spec/custom_macros.rb +460 -0
  28. data/spec/defaults_spec.rb +20 -0
  29. data/spec/error_proc_spec.rb +27 -0
  30. data/spec/errors_spec.rb +85 -0
  31. data/spec/form_helper_spec.rb +110 -0
  32. data/spec/i18n_spec.rb +131 -0
  33. data/spec/include_blank_spec.rb +70 -0
  34. data/spec/input_spec.rb +632 -0
  35. data/spec/inputs/boolean_input_spec.rb +93 -0
  36. data/spec/inputs/check_boxes_input_spec.rb +167 -0
  37. data/spec/inputs/country_input_spec.rb +80 -0
  38. data/spec/inputs/currency_input_spec.rb +80 -0
  39. data/spec/inputs/date_input_spec.rb +143 -0
  40. data/spec/inputs/datetime_input_spec.rb +259 -0
  41. data/spec/inputs/file_input_spec.rb +33 -0
  42. data/spec/inputs/hidden_input_spec.rb +52 -0
  43. data/spec/inputs/numeric_input_spec.rb +44 -0
  44. data/spec/inputs/password_input_spec.rb +46 -0
  45. data/spec/inputs/radio_input_spec.rb +152 -0
  46. data/spec/inputs/select_input_spec.rb +470 -0
  47. data/spec/inputs/string_input_spec.rb +47 -0
  48. data/spec/inputs/text_input_spec.rb +33 -0
  49. data/spec/inputs/time_input_spec.rb +128 -0
  50. data/spec/inputs/time_zone_input_spec.rb +102 -0
  51. data/spec/inputs_spec.rb +395 -0
  52. data/spec/label_spec.rb +48 -0
  53. data/spec/layout_helper_spec.rb +42 -0
  54. data/spec/semantic_errors_spec.rb +98 -0
  55. data/spec/semantic_fields_for_spec.rb +44 -0
  56. data/spec/spec.opts +2 -0
  57. data/spec/spec_helper.rb +238 -0
  58. metadata +205 -0
@@ -0,0 +1,259 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'datetime input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+
12
+ @new_post.should_receive(:publish_at=).any_number_of_times
13
+ @new_post.should_receive(:created_at=).any_number_of_times
14
+ @bob.should_receive(:created_at=).any_number_of_times
15
+ @new_post.should_receive(:title=).any_number_of_times # Macro stuff forces this.
16
+
17
+ semantic_form_for(@new_post) do |builder|
18
+ concat(builder.input(:publish_at, :as => :datetime))
19
+ end
20
+ end
21
+
22
+ it_should_have_input_wrapper_with_class("datetime")
23
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
24
+ it_should_have_a_nested_fieldset
25
+ it_should_apply_error_logic_for_input_type(:datetime)
26
+
27
+ it 'should have a legend and label with the label text inside the fieldset' do
28
+ output_buffer.should have_tag('form li.datetime fieldset legend.label label', /Publish at/)
29
+ end
30
+
31
+ it 'should associate the legend label with the first select' do
32
+ output_buffer.should have_tag('form li.datetime fieldset legend.label label[@for="post_publish_at_1i"]')
33
+ end
34
+
35
+ it 'should have an ordered list of five items inside the fieldset' do
36
+ output_buffer.should have_tag('form li.datetime fieldset ol')
37
+ output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
38
+ end
39
+
40
+ it 'should have five labels for year, month, day, hour and minute' do
41
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
42
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
43
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
44
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
45
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
46
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
47
+ end
48
+
49
+ it 'should have five selects for year, month, day, hour and minute' do
50
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
51
+ end
52
+
53
+ it 'should generate a sanitized label and matching ids for attribute' do
54
+ semantic_form_for(@new_post) do |builder|
55
+ builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
56
+ concat(bob_builder.input(:created_at, :as => :datetime))
57
+ end
58
+ end
59
+
60
+ 1.upto(5) do |i|
61
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']")
62
+ output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i")
63
+ end
64
+ end
65
+
66
+ describe 'when :discard_input => true is set' do
67
+ it 'should use default attribute value when it is not nil' do
68
+ @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
69
+ semantic_form_for(@new_post) do |builder|
70
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
71
+ end
72
+
73
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
74
+ end
75
+ end
76
+
77
+ describe 'inputs order' do
78
+ it 'should have a default' do
79
+ semantic_form_for(@new_post) do |builder|
80
+ self.should_receive(:select_year).once.ordered.and_return('')
81
+ self.should_receive(:select_month).once.ordered.and_return('')
82
+ self.should_receive(:select_day).once.ordered.and_return('')
83
+ builder.input(:publish_at, :as => :datetime)
84
+ end
85
+ end
86
+
87
+ it 'should be specified with :order option' do
88
+ ::I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
89
+ semantic_form_for(@new_post) do |builder|
90
+ self.should_receive(:select_month).once.ordered.and_return('')
91
+ self.should_receive(:select_year).once.ordered.and_return('')
92
+ self.should_receive(:select_day).once.ordered.and_return('')
93
+ builder.input(:publish_at, :as => :datetime)
94
+ end
95
+ end
96
+
97
+ it 'should be changed through I18n' do
98
+ semantic_form_for(@new_post) do |builder|
99
+ self.should_receive(:select_day).once.ordered.and_return('')
100
+ self.should_receive(:select_month).once.ordered.and_return('')
101
+ self.should_receive(:select_year).once.ordered.and_return('')
102
+ builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
103
+ end
104
+ end
105
+ end
106
+
107
+ describe 'when the locale changes the label text' do
108
+ before do
109
+ ::I18n.backend.store_translations 'en', :datetime => {:prompts => {
110
+ :year => 'The Year', :month => 'The Month', :day => 'The Day',
111
+ :hour => 'The Hour', :minute => 'The Minute'
112
+ }}
113
+ semantic_form_for(@new_post) do |builder|
114
+ concat(builder.input(:publish_at, :as => :datetime))
115
+ end
116
+ end
117
+
118
+ after do
119
+ ::I18n.backend.store_translations 'en', :formtastic => {
120
+ :year => nil, :month => nil, :day => nil,
121
+ :hour => nil, :minute => nil
122
+ }
123
+ end
124
+
125
+ it 'should have translated labels for year, month, day, hour and minute' do
126
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
127
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
128
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
129
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
130
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
131
+ end
132
+ end
133
+
134
+ describe 'when no object is given' do
135
+ before(:each) do
136
+ output_buffer.replace ''
137
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
138
+ concat(builder.input(:publish_at, :as => :datetime))
139
+ end
140
+ end
141
+
142
+ it 'should have fieldset with legend - classified as a label' do
143
+ output_buffer.should have_tag('form li.datetime fieldset legend.label', /Publish at/)
144
+ end
145
+
146
+ it 'should have labels for each input' do
147
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
148
+ end
149
+
150
+ it 'should have selects for each inputs' do
151
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
152
+ end
153
+ end
154
+
155
+ describe ':default option' do
156
+
157
+ describe "when the object has a value" do
158
+ it "should select the object value (ignoring :default)" do
159
+ output_buffer.replace ''
160
+ @new_post.stub!(:created_at => Time.mktime(2012))
161
+ semantic_form_for(@new_post) do |builder|
162
+ concat(builder.input(:created_at, :as => :datetime, :default => Time.mktime(1999)))
163
+ end
164
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
165
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1)
166
+ end
167
+ end
168
+
169
+ describe 'when the object has no value' do
170
+ it "should select the :default if provided as a Date" do
171
+ output_buffer.replace ''
172
+ @new_post.stub!(:created_at => nil)
173
+ semantic_form_for(@new_post) do |builder|
174
+ concat(builder.input(:created_at, :as => :datetime, :default => Date.new(1999)))
175
+ end
176
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
177
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
178
+ end
179
+
180
+ it "should select the :default if provided as a Time" do
181
+ output_buffer.replace ''
182
+ @new_post.stub!(:created_at => nil)
183
+ semantic_form_for(@new_post) do |builder|
184
+ concat(builder.input(:created_at, :as => :datetime, :default => Time.mktime(1999)))
185
+ end
186
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
187
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
188
+ end
189
+
190
+ it "should not select an option if the :default is provided as nil" do
191
+ output_buffer.replace ''
192
+ @new_post.stub!(:created_at => nil)
193
+ semantic_form_for(@new_post) do |builder|
194
+ concat(builder.input(:created_at, :as => :datetime, :default => nil))
195
+ end
196
+ output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]")
197
+ end
198
+
199
+ it "should select Time.now if a :default is not provided" do
200
+ output_buffer.replace ''
201
+ @new_post.stub!(:created_at => nil)
202
+ semantic_form_for(@new_post) do |builder|
203
+ concat(builder.input(:created_at, :as => :datetime))
204
+ end
205
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
206
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='#{Time.now.year}'][@selected]", :count => 1)
207
+
208
+ end
209
+ end
210
+
211
+ it 'should warn about :selected deprecation' do
212
+ with_deprecation_silenced do
213
+ ::ActiveSupport::Deprecation.should_receive(:warn)
214
+ semantic_form_for(@new_post) do |builder|
215
+ concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
216
+ end
217
+ end
218
+ end
219
+
220
+ end
221
+
222
+ describe ':labels option' do
223
+ fields = [:year, :month, :day, :hour, :minute]
224
+ fields.each do |field|
225
+ it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
226
+ output_buffer.replace ''
227
+ semantic_form_for(@new_post) do |builder|
228
+ concat(builder.input(:created_at, :as => :datetime, :labels => { field => "another #{field} label" }))
229
+ end
230
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length)
231
+ fields.each do |f|
232
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
233
+ end
234
+ end
235
+
236
+ it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
237
+ output_buffer.replace ''
238
+ semantic_form_for(@new_post) do |builder|
239
+ concat(builder.input(:created_at, :as => :datetime, :labels => { field => "" }))
240
+ end
241
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length-1)
242
+ fields.each do |f|
243
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /#{f}/i) unless field == f
244
+ end
245
+ end
246
+ end
247
+ end
248
+
249
+ it 'should warn about :selected deprecation' do
250
+ with_deprecation_silenced do
251
+ ::ActiveSupport::Deprecation.should_receive(:warn)
252
+ semantic_form_for(@new_post) do |builder|
253
+ concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999)))
254
+ end
255
+ end
256
+ end
257
+
258
+ end
259
+
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'file input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:body, :as => :file))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("file")
18
+ it_should_have_input_wrapper_with_id("post_body_input")
19
+ it_should_have_label_with_text(/Body/)
20
+ it_should_have_label_for("post_body")
21
+ it_should_have_input_with_id("post_body")
22
+ it_should_have_input_with_name("post[body]")
23
+ it_should_apply_error_logic_for_input_type(:file)
24
+
25
+ it 'should use input_html to style inputs' do
26
+ semantic_form_for(@new_post) do |builder|
27
+ concat(builder.input(:title, :as => :file, :input_html => { :class => 'myclass' }))
28
+ end
29
+ output_buffer.should have_tag("form li input.myclass")
30
+ end
31
+
32
+ end
33
+
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'hidden input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:secret, :as => :hidden))
14
+ concat(builder.input(:author_id, :as => :hidden, :value => 99))
15
+ concat(builder.input(:published, :as => :hidden, :input_html => {:value => true}))
16
+ end
17
+ end
18
+
19
+ it_should_have_input_wrapper_with_class("hidden")
20
+ it_should_have_input_wrapper_with_id("post_secret_input")
21
+ it_should_not_have_a_label
22
+
23
+ it "should generate a input field" do
24
+ output_buffer.should have_tag("form li input#post_secret")
25
+ output_buffer.should have_tag("form li input#post_secret[@type=\"hidden\"]")
26
+ output_buffer.should have_tag("form li input#post_secret[@name=\"post[secret]\"]")
27
+ end
28
+
29
+ it "should pass any explicitly specified value - using :value" do
30
+ output_buffer.should have_tag("form li input#post_author_id[@type=\"hidden\"][@value=\"99\"]")
31
+ end
32
+
33
+ # Handle Formtastic :input_html options for consistency.
34
+ it "should pass any explicitly specified value - using :input_html options" do
35
+ output_buffer.should have_tag("form li input#post_published[@type=\"hidden\"][@value=\"true\"]")
36
+ end
37
+
38
+ it "should not render inline errors" do
39
+ @errors = mock('errors')
40
+ @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"])
41
+ @new_post.stub!(:errors).and_return(@errors)
42
+
43
+ semantic_form_for(@new_post) do |builder|
44
+ concat(builder.input(:secret, :as => :hidden))
45
+ end
46
+
47
+ output_buffer.should_not have_tag("form li p.inline-errors")
48
+ output_buffer.should_not have_tag("form li ul.errors")
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'numeric input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:title, :as => :numeric))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class(:numeric)
18
+ it_should_have_input_wrapper_with_id("post_title_input")
19
+ it_should_have_label_with_text(/Title/)
20
+ it_should_have_label_for("post_title")
21
+ it_should_have_input_with_id("post_title")
22
+ it_should_have_input_with_type(:text)
23
+ it_should_have_input_with_name("post[title]")
24
+ it_should_use_default_text_field_size_when_method_has_no_database_column(:string)
25
+ it_should_apply_custom_input_attributes_when_input_html_provided(:string)
26
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
27
+ it_should_apply_error_logic_for_input_type(:numeric)
28
+
29
+ describe "when no object is provided" do
30
+ before do
31
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
32
+ concat(builder.input(:title, :as => :numeric))
33
+ end
34
+ end
35
+
36
+ it_should_have_label_with_text(/Title/)
37
+ it_should_have_label_for("project_title")
38
+ it_should_have_input_with_id("project_title")
39
+ it_should_have_input_with_type(:text)
40
+ it_should_have_input_with_name("project[title]")
41
+ end
42
+
43
+ end
44
+
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'password input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:title, :as => :password))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class(:password)
18
+ it_should_have_input_wrapper_with_id("post_title_input")
19
+ it_should_have_label_with_text(/Title/)
20
+ it_should_have_label_for("post_title")
21
+ it_should_have_input_with_id("post_title")
22
+ it_should_have_input_with_type(:password)
23
+ it_should_have_input_with_name("post[title]")
24
+ it_should_have_maxlength_matching_column_limit
25
+ it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(:string)
26
+ it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string)
27
+ it_should_use_default_text_field_size_when_method_has_no_database_column(:string)
28
+ it_should_apply_custom_input_attributes_when_input_html_provided(:string)
29
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
30
+ it_should_apply_error_logic_for_input_type(:password)
31
+
32
+ describe "when no object is provided" do
33
+ before do
34
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
35
+ concat(builder.input(:title, :as => :password))
36
+ end
37
+ end
38
+
39
+ it_should_have_label_with_text(/Title/)
40
+ it_should_have_label_for("project_title")
41
+ it_should_have_input_with_id("project_title")
42
+ it_should_have_input_with_type(:password)
43
+ it_should_have_input_with_name("project[title]")
44
+ end
45
+
46
+ end
@@ -0,0 +1,152 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'radio input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+ end
12
+
13
+ describe 'for belongs_to association' do
14
+ before do
15
+ semantic_form_for(@new_post) do |builder|
16
+ concat(builder.input(:author, :as => :radio, :value_as_class => true))
17
+ end
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class("radio")
21
+ it_should_have_input_wrapper_with_id("post_author_input")
22
+ it_should_have_a_nested_fieldset
23
+ it_should_apply_error_logic_for_input_type(:radio)
24
+ it_should_use_the_collection_when_provided(:radio, 'input')
25
+
26
+ it 'should generate a legend containing a label with text for the input' do
27
+ output_buffer.should have_tag('form li fieldset legend.label label')
28
+ output_buffer.should have_tag('form li fieldset legend.label label', /Author/)
29
+ end
30
+
31
+ it 'should not link the label within the legend to any input' do
32
+ output_buffer.should_not have_tag('form li fieldset legend label[@for^="post_author_id_"]')
33
+ end
34
+
35
+ it 'should generate an ordered list with a list item for each choice' do
36
+ output_buffer.should have_tag('form li fieldset ol')
37
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
38
+ end
39
+
40
+ it 'should have one option with a "checked" attribute' do
41
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
42
+ end
43
+
44
+ describe "each choice" do
45
+
46
+ it 'should contain a label for the radio input with a nested input and label text' do
47
+ ::Author.find(:all).each do |author|
48
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
49
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
50
+ end
51
+ end
52
+
53
+ it 'should use values as li.class when value_as_class is true' do
54
+ ::Author.find(:all).each do |author|
55
+ output_buffer.should have_tag("form li fieldset ol li.author_#{author.id} label")
56
+ end
57
+ end
58
+
59
+ it "should have a radio input" do
60
+ ::Author.find(:all).each do |author|
61
+ output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
62
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
63
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
64
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
65
+ end
66
+ end
67
+
68
+ it "should mark input as checked if it's the the existing choice" do
69
+ @new_post.author_id.should == @bob.id
70
+ @new_post.author.id.should == @bob.id
71
+ @new_post.author.should == @bob
72
+
73
+ semantic_form_for(@new_post) do |builder|
74
+ concat(builder.input(:author, :as => :radio))
75
+ end
76
+
77
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
78
+ end
79
+ end
80
+
81
+ describe 'and no object is given' do
82
+ before(:each) do
83
+ output_buffer.replace ''
84
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
85
+ concat(builder.input(:author_id, :as => :radio, :collection => ::Author.find(:all)))
86
+ end
87
+ end
88
+
89
+ it 'should generate a fieldset with legend' do
90
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
91
+ end
92
+
93
+ it 'should generate an li tag for each item in the collection' do
94
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
95
+ end
96
+
97
+ it 'should generate labels for each item' do
98
+ ::Author.find(:all).each do |author|
99
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
100
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
101
+ end
102
+ end
103
+
104
+ it 'should generate inputs for each item' do
105
+ ::Author.find(:all).each do |author|
106
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
107
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
108
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
109
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']")
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ describe 'when :selected is set' do
116
+ before do
117
+ @output_buffer = ActiveSupport::SafeBuffer.new
118
+ end
119
+
120
+ describe "no selected items" do
121
+ before do
122
+ @new_post.stub!(:author_ids).and_return(nil)
123
+
124
+ semantic_form_for(@new_post) do |builder|
125
+ concat(builder.input(:authors, :as => :radio, :selected => nil))
126
+ end
127
+ end
128
+
129
+ it 'should not have any selected item(s)' do
130
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']")
131
+ end
132
+ end
133
+
134
+ describe "single selected item" do
135
+ before do
136
+ @new_post.stub!(:author_ids).and_return(nil)
137
+
138
+ semantic_form_for(@new_post) do |builder|
139
+ concat(builder.input(:authors, :as => :radio, :selected => @fred.id))
140
+ end
141
+ end
142
+
143
+ it "should have one item selected; the specified one" do
144
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio'][@checked='checked']", :count => 1)
145
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
146
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio'][@checked='checked'][@value='#{@fred.id}']")
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ end