formtastic 1.2.5 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/.gitignore +13 -0
  2. data/.rspec +2 -0
  3. data/.yardopts +1 -0
  4. data/CHANGELOG +279 -0
  5. data/Gemfile +3 -0
  6. data/README.textile +155 -172
  7. data/RELEASE_PROCESS +7 -0
  8. data/Rakefile +52 -0
  9. data/app/assets/stylesheets/formtastic.css +275 -0
  10. data/app/assets/stylesheets/formtastic_ie6.css +27 -0
  11. data/app/assets/stylesheets/formtastic_ie7.css +17 -0
  12. data/formtastic.gemspec +51 -0
  13. data/lib/formtastic.rb +21 -1960
  14. data/lib/formtastic/engine.rb +7 -0
  15. data/lib/formtastic/form_builder.rb +83 -0
  16. data/lib/formtastic/helpers.rb +16 -0
  17. data/lib/formtastic/helpers/buttons_helper.rb +277 -0
  18. data/lib/formtastic/helpers/errors_helper.rb +113 -0
  19. data/lib/formtastic/helpers/fieldset_wrapper.rb +75 -0
  20. data/lib/formtastic/helpers/file_column_detection.rb +16 -0
  21. data/lib/formtastic/helpers/form_helper.rb +198 -0
  22. data/lib/formtastic/helpers/input_helper.rb +366 -0
  23. data/lib/formtastic/helpers/inputs_helper.rb +392 -0
  24. data/lib/formtastic/helpers/reflection.rb +33 -0
  25. data/lib/formtastic/helpers/semantic_form_helper.rb +11 -0
  26. data/lib/formtastic/html_attributes.rb +21 -0
  27. data/lib/formtastic/i18n.rb +1 -0
  28. data/lib/formtastic/inputs.rb +31 -0
  29. data/lib/formtastic/inputs/base.rb +61 -0
  30. data/lib/formtastic/inputs/base/associations.rb +31 -0
  31. data/lib/formtastic/inputs/base/choices.rb +103 -0
  32. data/lib/formtastic/inputs/base/collections.rb +94 -0
  33. data/lib/formtastic/inputs/base/database.rb +17 -0
  34. data/lib/formtastic/inputs/base/errors.rb +58 -0
  35. data/lib/formtastic/inputs/base/fileish.rb +23 -0
  36. data/lib/formtastic/inputs/base/grouped_collections.rb +77 -0
  37. data/lib/formtastic/inputs/base/hints.rb +31 -0
  38. data/lib/formtastic/inputs/base/html.rb +52 -0
  39. data/lib/formtastic/inputs/base/labelling.rb +55 -0
  40. data/lib/formtastic/inputs/base/naming.rb +42 -0
  41. data/lib/formtastic/inputs/base/options.rb +18 -0
  42. data/lib/formtastic/inputs/base/stringish.rb +35 -0
  43. data/lib/formtastic/inputs/base/timeish.rb +128 -0
  44. data/lib/formtastic/inputs/base/validations.rb +166 -0
  45. data/lib/formtastic/inputs/base/wrapping.rb +40 -0
  46. data/lib/formtastic/inputs/boolean_input.rb +96 -0
  47. data/lib/formtastic/inputs/check_boxes_input.rb +179 -0
  48. data/lib/formtastic/inputs/country_input.rb +66 -0
  49. data/lib/formtastic/inputs/date_input.rb +14 -0
  50. data/lib/formtastic/inputs/datetime_input.rb +9 -0
  51. data/lib/formtastic/inputs/email_input.rb +40 -0
  52. data/lib/formtastic/inputs/file_input.rb +42 -0
  53. data/lib/formtastic/inputs/hidden_input.rb +66 -0
  54. data/lib/formtastic/inputs/number_input.rb +118 -0
  55. data/lib/formtastic/inputs/numeric_input.rb +21 -0
  56. data/lib/formtastic/inputs/password_input.rb +40 -0
  57. data/lib/formtastic/inputs/phone_input.rb +41 -0
  58. data/lib/formtastic/inputs/radio_input.rb +157 -0
  59. data/lib/formtastic/inputs/range_input.rb +119 -0
  60. data/lib/formtastic/inputs/search_input.rb +40 -0
  61. data/lib/formtastic/inputs/select_input.rb +210 -0
  62. data/lib/formtastic/inputs/string_input.rb +34 -0
  63. data/lib/formtastic/inputs/text_input.rb +47 -0
  64. data/lib/formtastic/inputs/time_input.rb +14 -0
  65. data/lib/formtastic/inputs/time_zone_input.rb +48 -0
  66. data/lib/formtastic/inputs/url_input.rb +40 -0
  67. data/lib/formtastic/localized_string.rb +105 -0
  68. data/lib/formtastic/railtie.rb +5 -7
  69. data/lib/formtastic/semantic_form_builder.rb +11 -0
  70. data/lib/formtastic/util.rb +6 -19
  71. data/lib/formtastic/version.rb +3 -0
  72. data/lib/generators/formtastic/install/install_generator.rb +28 -6
  73. data/lib/generators/templates/_form.html.erb +10 -4
  74. data/lib/generators/templates/_form.html.haml +8 -4
  75. data/lib/generators/templates/formtastic.rb +25 -32
  76. data/lib/locale/en.yml +1 -0
  77. data/lib/tasks/verify_rcov.rb +44 -0
  78. data/sample/basic_inputs.html +182 -0
  79. data/sample/config.ru +69 -0
  80. data/sample/index.html +14 -0
  81. data/spec/builder/custom_builder_spec.rb +109 -0
  82. data/spec/builder/error_proc_spec.rb +27 -0
  83. data/spec/builder/errors_spec.rb +193 -0
  84. data/spec/builder/semantic_fields_for_spec.rb +88 -0
  85. data/spec/helpers/buttons_helper_spec.rb +150 -0
  86. data/spec/helpers/commit_button_helper_spec.rb +470 -0
  87. data/spec/helpers/form_helper_spec.rb +135 -0
  88. data/spec/helpers/input_helper_spec.rb +837 -0
  89. data/spec/helpers/inputs_helper_spec.rb +562 -0
  90. data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
  91. data/spec/i18n_spec.rb +199 -0
  92. data/spec/inputs/boolean_input_spec.rb +184 -0
  93. data/spec/inputs/check_boxes_input_spec.rb +375 -0
  94. data/spec/inputs/country_input_spec.rb +133 -0
  95. data/spec/inputs/custom_input_spec.rb +52 -0
  96. data/spec/inputs/date_input_spec.rb +110 -0
  97. data/spec/inputs/datetime_input_spec.rb +115 -0
  98. data/spec/inputs/email_input_spec.rb +55 -0
  99. data/spec/inputs/file_input_spec.rb +59 -0
  100. data/spec/inputs/hidden_input_spec.rb +120 -0
  101. data/spec/inputs/include_blank_spec.rb +70 -0
  102. data/spec/inputs/label_spec.rb +104 -0
  103. data/spec/inputs/number_input_spec.rb +487 -0
  104. data/spec/inputs/numeric_input_spec.rb +41 -0
  105. data/spec/inputs/password_input_spec.rb +69 -0
  106. data/spec/inputs/phone_input_spec.rb +55 -0
  107. data/spec/inputs/placeholder_spec.rb +71 -0
  108. data/spec/inputs/radio_input_spec.rb +234 -0
  109. data/spec/inputs/range_input_spec.rb +477 -0
  110. data/spec/inputs/search_input_spec.rb +55 -0
  111. data/spec/inputs/select_input_spec.rb +545 -0
  112. data/spec/inputs/string_input_spec.rb +163 -0
  113. data/spec/inputs/text_input_spec.rb +158 -0
  114. data/spec/inputs/time_input_spec.rb +155 -0
  115. data/spec/inputs/time_zone_input_spec.rb +87 -0
  116. data/spec/inputs/url_input_spec.rb +55 -0
  117. data/spec/spec.opts +2 -0
  118. data/spec/spec_helper.rb +361 -0
  119. data/spec/support/custom_macros.rb +656 -0
  120. data/spec/support/deferred_garbage_collection.rb +21 -0
  121. data/spec/support/deprecation.rb +6 -0
  122. data/spec/support/test_environment.rb +30 -0
  123. metadata +306 -88
  124. data/generators/form/USAGE +0 -16
  125. data/generators/form/form_generator.rb +0 -111
  126. data/generators/formtastic/formtastic_generator.rb +0 -26
  127. data/init.rb +0 -5
  128. data/lib/formtastic/layout_helper.rb +0 -12
  129. data/lib/generators/formtastic/form/form_generator.rb +0 -84
  130. data/lib/generators/templates/formtastic.css +0 -145
  131. data/lib/generators/templates/formtastic_changes.css +0 -14
  132. data/lib/generators/templates/rails2/_form.html.erb +0 -5
  133. data/lib/generators/templates/rails2/_form.html.haml +0 -4
  134. data/rails/init.rb +0 -2
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'search input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe "when object is provided" do
14
+ before do
15
+ concat(semantic_form_for(@new_post) do |builder|
16
+ concat(builder.input(:search))
17
+ end)
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class(:search)
21
+ it_should_have_input_wrapper_with_class(:input)
22
+ it_should_have_input_wrapper_with_class(:stringish)
23
+ it_should_have_input_wrapper_with_id("post_search_input")
24
+ it_should_have_label_with_text(/Search/)
25
+ it_should_have_label_for("post_search")
26
+ it_should_have_input_with_id("post_search")
27
+ it_should_have_input_with_type(:search)
28
+ it_should_have_input_with_name("post[search]")
29
+
30
+ end
31
+
32
+ describe "when namespace is provided" do
33
+
34
+ before do
35
+ concat(semantic_form_for(@new_post, :namespace => "context2") do |builder|
36
+ concat(builder.input(:search))
37
+ end)
38
+ end
39
+
40
+ it_should_have_input_wrapper_with_id("context2_post_search_input")
41
+ it_should_have_label_and_input_with_id("context2_post_search")
42
+
43
+ end
44
+
45
+ describe "when required" do
46
+ it "should add the required attribute to the input's html options" do
47
+ concat(semantic_form_for(@new_post) do |builder|
48
+ concat(builder.input(:title, :as => :search, :required => true))
49
+ end)
50
+ output_buffer.should have_tag("input[@required]")
51
+ end
52
+ end
53
+
54
+ end
55
+
@@ -0,0 +1,545 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'select input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe 'explicit values' do
14
+ describe 'using an array of values' do
15
+ before do
16
+ @array_with_values = ["Title A", "Title B", "Title C"]
17
+ @array_with_keys_and_values = [["Title D", 1], ["Title E", 2], ["Title F", 3]]
18
+ concat(semantic_form_for(@new_post) do |builder|
19
+ concat(builder.input(:title, :as => :select, :collection => @array_with_values))
20
+ concat(builder.input(:title, :as => :select, :collection => @array_with_keys_and_values))
21
+ end)
22
+ end
23
+
24
+ it 'should have a option for each key and/or value' do
25
+ @array_with_values.each do |v|
26
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
27
+ end
28
+ @array_with_keys_and_values.each do |v|
29
+ output_buffer.should have_tag("form li select option[@value='#{v.second}']", /^#{v.first}$/)
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "using a related model without reflection's options (Mongoid Document)" do
35
+ before do
36
+ @new_post.stub!(:mongoid_reviewer)
37
+ concat(semantic_form_for(@new_post) do |builder|
38
+ concat(builder.input(:mongoid_reviewer, :as => :select))
39
+ end)
40
+ end
41
+
42
+ it 'should draw select options' do
43
+ output_buffer.should have_tag('form li select')
44
+ output_buffer.should have_tag('form li select#post_reviewer_id')
45
+ output_buffer.should_not have_tag('form li select#post_mongoid_reviewer_id')
46
+ end
47
+ end
48
+
49
+ describe 'using a range' do
50
+ before do
51
+ @range_with_values = 1..5
52
+ concat(semantic_form_for(@new_post) do |builder|
53
+ concat(builder.input(:title, :as => :select, :collection => @range_with_values))
54
+ end)
55
+ end
56
+
57
+ it 'should have an option for each value' do
58
+ @range_with_values.each do |v|
59
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe 'using a string' do
65
+ before do
66
+ @string ="<option value='0'>0</option><option value='1'>1</option>"
67
+ concat(semantic_form_for(@new_post) do |builder|
68
+ concat(builder.input(:title, :as => :select, :collection => @string))
69
+ end)
70
+ end
71
+
72
+ it 'should render select options using provided HTML string' do
73
+ 2.times do |v|
74
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ describe 'for boolean columns' do
81
+ describe 'default formtastic locale' do
82
+ before do
83
+ # Note: Works, but something like Formtastic.root.join(...) would probably be "safer".
84
+ ::I18n.load_path = [File.join(File.dirname(__FILE__), *%w[.. .. lib locale en.yml])]
85
+ ::I18n.backend.send(:init_translations)
86
+
87
+ concat(semantic_form_for(@new_post) do |builder|
88
+ concat(builder.input(:published, :as => :select))
89
+ end)
90
+ end
91
+
92
+ after do
93
+ ::I18n.backend.store_translations :en, {}
94
+ end
95
+
96
+ it 'should render a select with at least options: true/false' do
97
+ output_buffer.should have_tag("form li select option[@value='true']", /^Yes$/)
98
+ output_buffer.should have_tag("form li select option[@value='false']", /^No$/)
99
+ end
100
+ end
101
+
102
+ describe 'custom locale' do
103
+ before do
104
+ @boolean_select_labels = {:yes => 'Yep', :no => 'Nope'}
105
+ ::I18n.backend.store_translations :en, :formtastic => @boolean_select_labels
106
+
107
+ concat(semantic_form_for(@new_post) do |builder|
108
+ concat(builder.input(:published, :as => :select))
109
+ end)
110
+ end
111
+
112
+ after do
113
+ ::I18n.backend.store_translations :en, {}
114
+ end
115
+
116
+ it 'should render a select with at least options: true/false' do
117
+ output_buffer.should have_tag("form li select option[@value='true']", /#{@boolean_select_labels[:yes]}/)
118
+ output_buffer.should have_tag("form li select option[@value='false']", /#{@boolean_select_labels[:no]}/)
119
+ end
120
+ end
121
+ end
122
+
123
+ describe 'for a belongs_to association' do
124
+ before do
125
+ concat(semantic_form_for(@new_post) do |builder|
126
+ concat(builder.input(:author, :as => :select))
127
+ concat(builder.input(:reviewer, :as => :select))
128
+ end)
129
+ end
130
+
131
+ it_should_have_input_wrapper_with_class("select")
132
+ it_should_have_input_wrapper_with_class(:input)
133
+ it_should_have_input_wrapper_with_id("post_author_input")
134
+ it_should_have_label_with_text(/Author/)
135
+ it_should_have_label_for('post_author_id')
136
+ it_should_apply_error_logic_for_input_type(:select)
137
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
138
+ it_should_use_the_collection_when_provided(:select, 'option')
139
+
140
+ it 'should have a select inside the wrapper' do
141
+ output_buffer.should have_tag('form li select')
142
+ output_buffer.should have_tag('form li select#post_author_id')
143
+ output_buffer.should have_tag('form li select#post_reviewer_id')
144
+ end
145
+
146
+ it 'should have a valid name' do
147
+ output_buffer.should have_tag("form li select[@name='post[author_id]']")
148
+ output_buffer.should_not have_tag("form li select[@name='post[author_id][]']")
149
+ output_buffer.should_not have_tag("form li select[@name='post[reviewer_id][]']")
150
+ end
151
+
152
+ it 'should not create a multi-select' do
153
+ output_buffer.should_not have_tag('form li select[@multiple]')
154
+ end
155
+
156
+ it 'should create a select without size' do
157
+ output_buffer.should_not have_tag('form li select[@size]')
158
+ end
159
+
160
+ it 'should have a blank option' do
161
+ output_buffer.should have_tag("form li select option[@value='']")
162
+ end
163
+
164
+ it 'should have a select option for each Author' do
165
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option", :count => ::Author.all.size + 1)
166
+ ::Author.all.each do |author|
167
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
168
+ end
169
+ end
170
+
171
+ it 'should have one option with a "selected" attribute (bob)' do
172
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option[@selected]", :count => 1)
173
+ end
174
+
175
+ it 'should not singularize the association name' do
176
+ @new_post.stub!(:author_status).and_return(@bob)
177
+ @new_post.stub!(:author_status_id).and_return(@bob.id)
178
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
179
+
180
+ concat(semantic_form_for(@new_post) do |builder|
181
+ concat(builder.input(:author_status, :as => :select))
182
+ end)
183
+
184
+ output_buffer.should have_tag('form li select#post_author_status_id')
185
+ end
186
+ end
187
+
188
+ describe "for a belongs_to association with :group_by => :author" do
189
+ it "should call author.posts" do
190
+ ::Author.stub!(:reflect_on_all_associations).and_return { |macro| macro == :has_many ? [mock('reflection', :klass => Post, :name => :posts)] : []}
191
+
192
+ [@freds_post].each { |post| post.stub!(:to_label).and_return("Post - #{post.id}") }
193
+ @fred.should_receive(:posts)
194
+
195
+ concat(semantic_form_for(@new_post) do |builder|
196
+ concat(builder.input(:main_post, :as => :select, :group_by => :author ) )
197
+ end)
198
+ end
199
+ end
200
+
201
+ describe "for a belongs_to association with :conditions" do
202
+ before do
203
+ ::Post.stub!(:reflect_on_association).with(:author).and_return do
204
+ mock = mock('reflection', :options => {:conditions => {:active => true}}, :klass => ::Author, :macro => :belongs_to)
205
+ mock.stub!(:[]).with(:class_name).and_return("Author")
206
+ mock
207
+ end
208
+ end
209
+
210
+ it "should call author.find with association conditions" do
211
+ ::Author.should_receive(:where).with(:active => true)
212
+
213
+ semantic_form_for(@new_post) do |builder|
214
+ concat(builder.input(:author, :as => :select))
215
+ end
216
+ end
217
+
218
+ it "should call author.find with association conditions and find_options conditions" do
219
+ ::Author.should_receive(:where).with({:active => true, :publisher => true})
220
+
221
+ semantic_form_for(@new_post) do |builder|
222
+ concat(builder.input(:author, :as => :select, :find_options => {:conditions => {:publisher => true}}))
223
+ end
224
+ end
225
+ end
226
+
227
+ describe 'for a belongs_to association with :group_by => :continent' do
228
+ before do
229
+ @authors = [@bob, @fred, @fred, @fred]
230
+ ::Author.stub!(:find).and_return(@authors)
231
+ @continent_names = %w(Europe Africa)
232
+ @continents = (0..1).map { |i| c = ::Continent.new; c.stub!(:id).and_return(100 - i);c }
233
+ @authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) }
234
+
235
+ ::Continent.stub!(:reflect_on_all_associations).and_return { |macro| macro == :has_many ? [mock('reflection', :klass => Author, :name => :authors)] : [] }
236
+ ::Continent.stub!(:reflect_on_association).and_return {|column_name| mock('reflection', :klass => Author) if column_name == :authors}
237
+ ::Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Continent, :macro => :belongs_to) if column_name == :continent }
238
+
239
+
240
+ @continents.each_with_index do |continent, i|
241
+ continent.stub!(:to_label).and_return(@continent_names[i])
242
+ continent.stub!(:authors).and_return([@authors[i]])
243
+ end
244
+
245
+ concat(semantic_form_for(@new_post) do |builder|
246
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
247
+ concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label => :id ) )
248
+ concat(builder.input(:author, :as => :select, :group_by => :continent, :member_label => :login ) )
249
+ concat(builder.input(:author, :as => :select, :group_by => :continent, :member_label => :login, :group_label => :id ) )
250
+ end)
251
+ end
252
+
253
+ it_should_have_input_wrapper_with_class("select")
254
+ it_should_have_input_wrapper_with_id("post_author_input")
255
+ it_should_have_label_with_text(/Author/)
256
+ it_should_have_label_for('post_author_id')
257
+
258
+ # TODO, need to find a way to repeat some of the specs and logic from the belongs_to specs without grouping
259
+
260
+ 0.upto(1) do |i|
261
+ it 'should have all option groups and the right values' do
262
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label)
263
+ end
264
+
265
+ it 'should have custom group labels' do
266
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label)
267
+ end
268
+
269
+ it 'should have custom author labels' do
270
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].login)
271
+ end
272
+
273
+ it 'should have custom author and group labels' do
274
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].login)
275
+ end
276
+ end
277
+
278
+ it 'should have no duplicate groups' do
279
+ output_buffer.should have_tag('form li select optgroup', :count => 8)
280
+ end
281
+
282
+ it 'should sort the groups on the label method' do
283
+ output_buffer.should have_tag("form li select optgroup[@label='Africa']")
284
+ output_buffer.should have_tag("form li select optgroup[@label='99']")
285
+ end
286
+
287
+ it 'should call find with :include for more optimized queries' do
288
+ Author.should_receive(:where).with(:include => :continent)
289
+
290
+ semantic_form_for(@new_post) do |builder|
291
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
292
+ end
293
+ end
294
+ end
295
+
296
+ describe 'for a has_many association' do
297
+ before do
298
+ concat(semantic_form_for(@fred) do |builder|
299
+ concat(builder.input(:posts, :as => :select))
300
+ end)
301
+ end
302
+
303
+ it_should_have_input_wrapper_with_class("select")
304
+ it_should_have_input_wrapper_with_id("author_posts_input")
305
+ it_should_have_label_with_text(/Post/)
306
+ it_should_have_label_for('author_post_ids')
307
+ it_should_apply_error_logic_for_input_type(:select)
308
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
309
+ it_should_use_the_collection_when_provided(:select, 'option')
310
+
311
+ it 'should have a select inside the wrapper' do
312
+ output_buffer.should have_tag('form li select')
313
+ output_buffer.should have_tag('form li select#author_post_ids')
314
+ end
315
+
316
+ it 'should have a multi-select select' do
317
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
318
+ end
319
+
320
+ it 'should append [] to the name attribute for multiple select' do
321
+ output_buffer.should have_tag('form li select[@multiple="multiple"][@name="author[post_ids][]"]')
322
+ end
323
+
324
+ it 'should have a select option for each Post' do
325
+ output_buffer.should have_tag('form li select option', :count => ::Post.all.size)
326
+ ::Post.all.each do |post|
327
+ output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
328
+ end
329
+ end
330
+
331
+ it 'should not have a blank option by default' do
332
+ output_buffer.should_not have_tag("form li select option[@value='']")
333
+ end
334
+
335
+ it 'should respect the :include_blank option for single selects' do
336
+ concat(semantic_form_for(@fred) do |builder|
337
+ concat(builder.input(:posts, :as => :select, :multiple => false, :include_blank => true))
338
+ end)
339
+
340
+ output_buffer.should have_tag("form li select option[@value='']")
341
+ end
342
+
343
+ it 'should respect the :include_blank option for multiple selects' do
344
+ concat(semantic_form_for(@fred) do |builder|
345
+ concat(builder.input(:posts, :as => :select, :multiple => true, :include_blank => true))
346
+ end)
347
+
348
+ output_buffer.should have_tag("form li select option[@value='']")
349
+ end
350
+
351
+ it 'should have one option with a "selected" attribute' do
352
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
353
+ end
354
+ end
355
+
356
+ describe 'for a has_and_belongs_to_many association' do
357
+ before do
358
+ concat(semantic_form_for(@freds_post) do |builder|
359
+ concat(builder.input(:authors, :as => :select))
360
+ end)
361
+ end
362
+
363
+ it_should_have_input_wrapper_with_class("select")
364
+ it_should_have_input_wrapper_with_id("post_authors_input")
365
+ it_should_have_label_with_text(/Author/)
366
+ it_should_have_label_for('post_author_ids')
367
+ it_should_apply_error_logic_for_input_type(:select)
368
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
369
+ it_should_use_the_collection_when_provided(:select, 'option')
370
+
371
+ it 'should have a select inside the wrapper' do
372
+ output_buffer.should have_tag('form li select')
373
+ output_buffer.should have_tag('form li select#post_author_ids')
374
+ end
375
+
376
+ it 'should have a multi-select select' do
377
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
378
+ end
379
+
380
+ it 'should have a select option for each Author' do
381
+ output_buffer.should have_tag('form li select option', :count => ::Author.all.size)
382
+ ::Author.all.each do |author|
383
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
384
+ end
385
+ end
386
+
387
+ it 'should not have a blank option by default' do
388
+ output_buffer.should_not have_tag("form li select option[@value='']")
389
+ end
390
+
391
+ it 'should respect the :include_blank option for single selects' do
392
+ concat(semantic_form_for(@freds_post) do |builder|
393
+ concat(builder.input(:authors, :as => :select, :multiple => false, :include_blank => true))
394
+ end)
395
+
396
+ output_buffer.should have_tag("form li select option[@value='']")
397
+ end
398
+
399
+ it 'should respect the :include_blank option for multiple selects' do
400
+ concat(semantic_form_for(@freds_post) do |builder|
401
+ concat(builder.input(:authors, :as => :select, :multiple => true, :include_blank => true))
402
+ end)
403
+
404
+ output_buffer.should have_tag("form li select option[@value='']")
405
+ end
406
+
407
+ it 'should have one option with a "selected" attribute' do
408
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
409
+ end
410
+ end
411
+
412
+ describe 'when :prompt => "choose something" is set' do
413
+ before do
414
+ @new_post.stub!(:author_id).and_return(nil)
415
+ concat(semantic_form_for(@new_post) do |builder|
416
+ concat(builder.input(:author, :as => :select, :prompt => "choose author"))
417
+ end)
418
+ end
419
+
420
+ it 'should have a select with prompt' do
421
+ output_buffer.should have_tag("form li select option[@value='']", /choose author/)
422
+ end
423
+
424
+ it 'should not have a blank select option' do
425
+ output_buffer.should_not have_tag("form li select option[@value='']", "")
426
+ end
427
+ end
428
+
429
+ describe 'when no object is given' do
430
+ before(:each) do
431
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
432
+ concat(builder.input(:author, :as => :select, :collection => ::Author.all))
433
+ end)
434
+ end
435
+
436
+ it 'should generate label' do
437
+ output_buffer.should have_tag('form li label', /Author/)
438
+ output_buffer.should have_tag("form li label[@for='project_author']")
439
+ end
440
+
441
+ it 'should generate select inputs' do
442
+ output_buffer.should have_tag('form li select#project_author')
443
+ output_buffer.should have_tag('form li select option', :count => ::Author.all.size + 1)
444
+ end
445
+
446
+ it 'should generate an option to each item' do
447
+ ::Author.all.each do |author|
448
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
449
+ end
450
+ end
451
+ end
452
+
453
+ describe 'when no association exists' do
454
+ before(:each) do
455
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
456
+ concat(builder.input(:author_name, :as => :select, :collection => ::Author.all))
457
+ end)
458
+ end
459
+
460
+ it 'should still generate a valid name attribute' do
461
+ output_buffer.should have_tag("form li select[@name='project[author_name]']")
462
+ end
463
+ end
464
+
465
+ describe 'when a grouped collection collection is given' do
466
+ before(:each) do
467
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
468
+ @grouped_opts = [['one', ['pencil', 'crayon', 'pen']],
469
+ ['two', ['eyes', 'hands', 'feet']],
470
+ ['three', ['wickets', 'witches', 'blind mice']]]
471
+ concat(builder.input(:author, :as => :select, :collection => grouped_options_for_select(@grouped_opts, "hands")))
472
+ end)
473
+ end
474
+
475
+ it 'should generate an option to each item' do
476
+ @grouped_opts.each do |opt_pair|
477
+ output_buffer.should have_tag("form li select optgroup[@label='#{opt_pair[0]}']")
478
+ opt_pair[1].each do |v|
479
+ output_buffer.should have_tag("form li select optgroup[@label='#{opt_pair[0]}'] option[@value='#{v}']")
480
+ end
481
+ end
482
+ output_buffer.should have_tag("form li select optgroup option[@selected]","hands")
483
+ end
484
+ end
485
+
486
+ describe "enum" do
487
+ before do
488
+ @output_buffer = ''
489
+ @some_meta_descriptions = ["One", "Two", "Three"]
490
+ @new_post.stub!(:meta_description).any_number_of_times
491
+ end
492
+
493
+ describe ":as is not set" do
494
+ before do
495
+ concat(semantic_form_for(@new_post) do |builder|
496
+ concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
497
+ end)
498
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
499
+ concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
500
+ end)
501
+ end
502
+
503
+ it "should render a select field" do
504
+ output_buffer.should have_tag("form li select", :count => 2)
505
+ end
506
+ end
507
+
508
+ describe ":as is set" do
509
+ before do
510
+ # Should not be a case, but just checking :as got highest priority in setting input type.
511
+ concat(semantic_form_for(@new_post) do |builder|
512
+ concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
513
+ end)
514
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
515
+ concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
516
+ end)
517
+ end
518
+
519
+ it "should render a text field" do
520
+ output_buffer.should have_tag("form li input[@type='text']", :count => 2)
521
+ end
522
+ end
523
+ end
524
+
525
+ describe 'when a namespace is provided' do
526
+ before do
527
+ concat(semantic_form_for(@freds_post, :namespace => 'context2') do |builder|
528
+ concat(builder.input(:authors, :as => :select))
529
+ end)
530
+ end
531
+ it_should_have_input_wrapper_with_id("context2_post_authors_input")
532
+ it_should_have_select_with_id("context2_post_author_ids")
533
+ it_should_have_label_for("context2_post_author_ids")
534
+ end
535
+
536
+ context "when required" do
537
+ it "should add the required attribute to the select's html options" do
538
+ concat(semantic_form_for(@new_post) do |builder|
539
+ concat(builder.input(:author, :as => :select, :required => true))
540
+ end)
541
+ output_buffer.should have_tag("select[@required]")
542
+ end
543
+ end
544
+
545
+ end