formtastic-rails3 0.9.10.1 → 1.0.0.beta3

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 (42) hide show
  1. data/README.textile +32 -16
  2. data/Rakefile +11 -11
  3. data/lib/formtastic.rb +95 -30
  4. data/lib/formtastic/railtie.rb +1 -1
  5. data/lib/formtastic/util.rb +3 -2
  6. data/lib/generators/formtastic/form/form_generator.rb +4 -4
  7. data/spec/buttons_spec.rb +1 -1
  8. data/spec/commit_button_spec.rb +1 -1
  9. data/spec/custom_builder_spec.rb +1 -1
  10. data/spec/defaults_spec.rb +1 -1
  11. data/spec/error_proc_spec.rb +1 -1
  12. data/spec/errors_spec.rb +1 -1
  13. data/spec/form_helper_spec.rb +1 -1
  14. data/spec/helpers/layout_helper_spec.rb +21 -0
  15. data/spec/i18n_spec.rb +1 -1
  16. data/spec/include_blank_spec.rb +1 -1
  17. data/spec/input_spec.rb +99 -93
  18. data/spec/inputs/boolean_input_spec.rb +1 -1
  19. data/spec/inputs/check_boxes_input_spec.rb +120 -6
  20. data/spec/inputs/country_input_spec.rb +1 -1
  21. data/spec/inputs/date_input_spec.rb +1 -1
  22. data/spec/inputs/datetime_input_spec.rb +1 -1
  23. data/spec/inputs/file_input_spec.rb +1 -1
  24. data/spec/inputs/hidden_input_spec.rb +1 -1
  25. data/spec/inputs/numeric_input_spec.rb +1 -1
  26. data/spec/inputs/password_input_spec.rb +1 -1
  27. data/spec/inputs/radio_input_spec.rb +61 -2
  28. data/spec/inputs/select_input_spec.rb +1 -1
  29. data/spec/inputs/string_input_spec.rb +36 -19
  30. data/spec/inputs/text_input_spec.rb +1 -1
  31. data/spec/inputs/time_input_spec.rb +24 -2
  32. data/spec/inputs/time_zone_input_spec.rb +1 -1
  33. data/spec/inputs_spec.rb +37 -2
  34. data/spec/label_spec.rb +42 -1
  35. data/spec/semantic_errors_spec.rb +1 -1
  36. data/spec/semantic_fields_for_spec.rb +1 -1
  37. data/spec/spec_helper.rb +15 -39
  38. data/spec/{custom_macros.rb → support/custom_macros.rb} +0 -7
  39. data/spec/support/output_buffer.rb +4 -0
  40. data/spec/support/test_environment.rb +45 -0
  41. metadata +20 -14
  42. data/spec/layout_helper_spec.rb +0 -31
@@ -4,7 +4,7 @@ require 'rails'
4
4
 
5
5
  module Formtastic
6
6
  class Railtie < Rails::Railtie
7
- initializer :after_initialize do
7
+ initializer 'formtastic.initialize', :after => :after_initialize do
8
8
  ActionView::Base.send :include, Formtastic::SemanticFormHelper
9
9
  ActionView::Base.send(:include, Formtastic::LayoutHelper)
10
10
  end
@@ -13,7 +13,8 @@ module Formtastic
13
13
  def html_safe(text)
14
14
  return text if text.nil?
15
15
  return text.html_safe if defined?(ActiveSupport::SafeBuffer)
16
- return text.html_safe!
16
+ return text.html_safe! if text.respond_to?(:html_safe!)
17
+ text
17
18
  end
18
19
 
19
20
  def rails_safe_buffer_class
@@ -29,7 +30,7 @@ module Formtastic
29
30
  if defined?(ActionPack::VERSION::MAJOR)
30
31
  ActionPack::VERSION::MAJOR
31
32
  end
32
- version >= 3
33
+ !version.blank? && version >= 3
33
34
  end
34
35
  end
35
36
  end
@@ -5,6 +5,9 @@ module Formtastic
5
5
  "generated code will be printed out directly in the terminal, and also copied " <<
6
6
  "to clipboard. Can optionally be saved into partial directly."
7
7
 
8
+ argument :name, :type => :string, :required => true, :banner => 'ExistingModelName'
9
+ argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
10
+
8
11
  class_option :haml, :type => :boolean, :default => false, :group => :formtastic,
9
12
  :desc => "Generate HAML instead of ERB"
10
13
 
@@ -19,11 +22,8 @@ module Formtastic
19
22
  @source_root ||= File.expand_path(File.join('..', '..', '..', '..', 'generators', 'form', 'templates'), File.dirname(__FILE__))
20
23
  end
21
24
 
22
- def self.banner
23
- "rails generate formtastic:form ExistingModelName [options]"
24
- end
25
-
26
25
  def create_or_show
26
+ @attributes = self.columns if @attributes.empty?
27
27
  if options[:partial]
28
28
  empty_directory "app/views/#{controller_path}"
29
29
  template "view__form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
data/spec/buttons_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#buttons' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#commit_button' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::SemanticFormHelper.builder' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::SemanticFormBuilder-defaults' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Rails field_error_proc' do
5
5
 
data/spec/errors_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#errors_on' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormHelper' do
5
5
 
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Formtastic::LayoutHelper do
5
+
6
+ include FormtasticSpecHelper
7
+ include Formtastic::LayoutHelper
8
+
9
+ describe '#formtastic_stylesheet_link_tag' do
10
+
11
+ it 'should render a link to formtastic.css' do
12
+ formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic.css']")
13
+ end
14
+
15
+ it 'should render a link to formtastic_changes.css' do
16
+ formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic_changes.css']")
17
+ end
18
+
19
+ end
20
+ end
21
+
data/spec/i18n_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::I18n' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe "*select: options[:include_blank]" do
5
5
 
data/spec/input_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#input' do
5
5
 
@@ -136,13 +136,6 @@ describe 'SemanticFormBuilder#input' do
136
136
  @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
137
137
  end
138
138
 
139
- unless defined?(Rspec)
140
- after do
141
- ::Post.unstub!(:reflect_on_validations_for)
142
- @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(false)
143
- end
144
- end
145
-
146
139
  describe 'and validates_presence_of was called for the method' do
147
140
  it 'should be required' do
148
141
  @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
@@ -240,115 +233,111 @@ describe 'SemanticFormBuilder#input' do
240
233
 
241
234
  end
242
235
 
243
- unless defined?(Rspec)
244
- describe 'and its a ActiveModel' do
245
- before do
246
- ::Post.stub!(:validators_on).and_return([])
247
- @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(true)
248
- end
249
-
250
- after do
251
- ::Post.unstub!(:validators_on)
252
- @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(false)
253
- end
254
- describe 'and validates_presence_of was called for the method' do
255
- it 'should be required' do
236
+ describe 'and its a ActiveModel' do
237
+ before do
238
+ @new_post.stub!(:class).and_return(::PostModel)
239
+ end
256
240
 
257
- @new_post.class.should_receive(:validators_on).with(:title).and_return([
258
- active_model_presence_validator([:title])
259
- ])
241
+ after do
242
+ @new_post.stub!(:class).and_return(::Post)
243
+ end
244
+ describe 'and validates_presence_of was called for the method' do
245
+ it 'should be required' do
260
246
 
261
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
262
- active_model_presence_validator([:body], {:if => true})
263
- ])
247
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([
248
+ active_model_presence_validator([:title])
249
+ ])
264
250
 
265
- form = semantic_form_for(@new_post) do |builder|
266
- concat(builder.input(:title))
267
- concat(builder.input(:body))
268
- end
269
- output_buffer.concat(form) if Formtastic::Util.rails3?
270
- output_buffer.should have_tag('form li.required')
271
- output_buffer.should_not have_tag('form li.optional')
272
- end
251
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
252
+ active_model_presence_validator([:body], {:if => true})
253
+ ])
273
254
 
274
- it 'should be not be required if the optional :if condition is not satisifed' do
275
- should_be_required(:required => false, :options => { :if => false })
255
+ form = semantic_form_for(@new_post) do |builder|
256
+ concat(builder.input(:title))
257
+ concat(builder.input(:body))
276
258
  end
259
+ output_buffer.concat(form) if Formtastic::Util.rails3?
260
+ output_buffer.should have_tag('form li.required')
261
+ output_buffer.should_not have_tag('form li.optional')
262
+ end
277
263
 
278
- it 'should not be required if the optional :if proc evaluates to false' do
279
- should_be_required(:required => false, :options => { :if => proc { |record| false } })
280
- end
264
+ it 'should be not be required if the optional :if condition is not satisifed' do
265
+ should_be_required(:required => false, :options => { :if => false })
266
+ end
281
267
 
282
- it 'should be required if the optional :if proc evaluates to true' do
283
- should_be_required(:required => true, :options => { :if => proc { |record| true } })
284
- end
268
+ it 'should not be required if the optional :if proc evaluates to false' do
269
+ should_be_required(:required => false, :options => { :if => proc { |record| false } })
270
+ end
285
271
 
286
- it 'should not be required if the optional :unless proc evaluates to true' do
287
- should_be_required(:required => false, :options => { :unless => proc { |record| true } })
288
- end
272
+ it 'should be required if the optional :if proc evaluates to true' do
273
+ should_be_required(:required => true, :options => { :if => proc { |record| true } })
274
+ end
289
275
 
290
- it 'should be required if the optional :unless proc evaluates to false' do
291
- should_be_required(:required => true, :options => { :unless => proc { |record| false } })
292
- end
276
+ it 'should not be required if the optional :unless proc evaluates to true' do
277
+ should_be_required(:required => false, :options => { :unless => proc { |record| true } })
278
+ end
293
279
 
294
- it 'should be required if the optional :if with a method string evaluates to true' do
295
- @new_post.should_receive(:required_condition).and_return(true)
296
- should_be_required(:required => true, :options => { :if => :required_condition })
297
- end
280
+ it 'should be required if the optional :unless proc evaluates to false' do
281
+ should_be_required(:required => true, :options => { :unless => proc { |record| false } })
282
+ end
298
283
 
299
- it 'should be required if the optional :if with a method string evaluates to false' do
300
- @new_post.should_receive(:required_condition).and_return(false)
301
- should_be_required(:required => false, :options => { :if => :required_condition })
302
- end
284
+ it 'should be required if the optional :if with a method string evaluates to true' do
285
+ @new_post.should_receive(:required_condition).and_return(true)
286
+ should_be_required(:required => true, :options => { :if => :required_condition })
287
+ end
303
288
 
304
- it 'should not be required if the optional :unless with a method string evaluates to false' do
305
- @new_post.should_receive(:required_condition).and_return(false)
306
- should_be_required(:required => true, :options => { :unless => :required_condition })
307
- end
289
+ it 'should be required if the optional :if with a method string evaluates to false' do
290
+ @new_post.should_receive(:required_condition).and_return(false)
291
+ should_be_required(:required => false, :options => { :if => :required_condition })
292
+ end
308
293
 
309
- it 'should be required if the optional :unless with a method string evaluates to true' do
310
- @new_post.should_receive(:required_condition).and_return(true)
311
- should_be_required(:required => false, :options => { :unless => :required_condition })
312
- end
294
+ it 'should not be required if the optional :unless with a method string evaluates to false' do
295
+ @new_post.should_receive(:required_condition).and_return(false)
296
+ should_be_required(:required => true, :options => { :unless => :required_condition })
313
297
  end
314
298
 
315
- # TODO make a matcher for this?
316
- def should_be_required(options)
317
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
318
- active_model_presence_validator([:body], options[:options])
319
- ])
299
+ it 'should be required if the optional :unless with a method string evaluates to true' do
300
+ @new_post.should_receive(:required_condition).and_return(true)
301
+ should_be_required(:required => false, :options => { :unless => :required_condition })
302
+ end
303
+ end
320
304
 
321
- form = semantic_form_for(@new_post) do |builder|
322
- concat(builder.input(:body))
323
- end
305
+ # TODO make a matcher for this?
306
+ def should_be_required(options)
307
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
308
+ active_model_presence_validator([:body], options[:options])
309
+ ])
324
310
 
325
- output_buffer.concat(form) if Formtastic::Util.rails3?
311
+ form = semantic_form_for(@new_post) do |builder|
312
+ concat(builder.input(:body))
313
+ end
326
314
 
327
- if options[:required]
328
- output_buffer.should_not have_tag('form li.optional')
329
- output_buffer.should have_tag('form li.required')
330
- else
331
- output_buffer.should have_tag('form li.optional')
332
- output_buffer.should_not have_tag('form li.required')
333
- end
315
+ output_buffer.concat(form) if Formtastic::Util.rails3?
316
+
317
+ if options[:required]
318
+ output_buffer.should_not have_tag('form li.optional')
319
+ output_buffer.should have_tag('form li.required')
320
+ else
321
+ output_buffer.should have_tag('form li.optional')
322
+ output_buffer.should_not have_tag('form li.required')
334
323
  end
324
+ end
335
325
 
336
- describe 'and validates_presence_of was not called for the method' do
337
- before do
338
- @new_post.class.should_receive(:validators_on).with(:title).and_return([])
339
- end
326
+ describe 'and validates_presence_of was not called for the method' do
327
+ before do
328
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([])
329
+ end
340
330
 
341
- it 'should not be required' do
342
- form = semantic_form_for(@new_post) do |builder|
343
- concat(builder.input(:title))
344
- end
345
- output_buffer.concat(form) if Formtastic::Util.rails3?
346
- output_buffer.should_not have_tag('form li.required')
347
- output_buffer.should have_tag('form li.optional')
331
+ it 'should not be required' do
332
+ form = semantic_form_for(@new_post) do |builder|
333
+ concat(builder.input(:title))
348
334
  end
335
+ output_buffer.concat(form) if Formtastic::Util.rails3?
336
+ output_buffer.should_not have_tag('form li.required')
337
+ output_buffer.should have_tag('form li.optional')
349
338
  end
350
-
351
339
  end
340
+
352
341
  end
353
342
 
354
343
  describe 'and the validation reflection plugin is not available' do
@@ -717,6 +706,23 @@ describe 'SemanticFormBuilder#input' do
717
706
  end
718
707
  end
719
708
 
709
+ describe 'when localized hint (I18n) is a model with attribute hints' do
710
+ it "should see the provided hash as a blank entry" do
711
+ ::I18n.backend.store_translations :en,
712
+ :formtastic => {
713
+ :hints => {
714
+ :title => { # movie title
715
+ :summary => @localized_hint_text # summary of movie
716
+ }
717
+ }
718
+ }
719
+ semantic_form_for(@new_post) do |builder|
720
+ concat(builder.input(:title, :hint => true))
721
+ end
722
+ output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
723
+ end
724
+ end
725
+
720
726
  describe 'when localized hint (I18n) is not provided' do
721
727
  it 'should not render a hint paragraph' do
722
728
  form = semantic_form_for(@new_post) do |builder|
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'boolean input' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'check_boxes input' do
5
5
 
@@ -45,9 +45,9 @@ describe 'check_boxes input' do
45
45
  output_buffer.should have_tag('form li input[@checked]', :count => 1)
46
46
  end
47
47
 
48
- it 'should generate hidden inputs with default value blank' do
48
+ it 'should not generate hidden inputs with default value blank' do
49
49
  output_buffer.concat(@form) if Formtastic::Util.rails3?
50
- output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
50
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
51
51
  end
52
52
 
53
53
  describe "each choice" do
@@ -67,12 +67,27 @@ describe 'check_boxes input' do
67
67
  end
68
68
  end
69
69
 
70
- it 'should have a checkbox input for each post' do
70
+ it 'should have a checkbox input but no hidden field for each post' do
71
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 checkbox and a hidden field for each post with :hidden_field => true' do
79
+ output_buffer.replace ''
80
+
81
+ form = semantic_form_for(@fred) do |builder|
82
+ concat(builder.input(:posts, :as => :check_boxes, :hidden_fields => true, :value_as_class => true))
83
+ end
84
+ output_buffer.concat(form) if Formtastic::Util.rails3?
85
+
72
86
  ::Post.find(:all).each do |post|
73
87
  output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
74
88
  output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
75
89
  end
90
+
76
91
  end
77
92
 
78
93
  it "should mark input as checked if it's the the existing choice" do
@@ -117,6 +132,44 @@ describe 'check_boxes input' do
117
132
  output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
118
133
  end
119
134
  end
135
+
136
+ it 'should html escape the label string' do
137
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
138
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
139
+ end
140
+ output_buffer.concat(form) if Formtastic::Util.rails3?
141
+ output_buffer.should have_tag('form li fieldset ol li label') do |label|
142
+ label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
143
+ end
144
+ end
145
+ end
146
+
147
+ describe 'when :hidden_fields is set to false' do
148
+ before do
149
+ @output_buffer = ''
150
+ mock_everything
151
+
152
+ form = semantic_form_for(@fred) do |builder|
153
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true, :hidden_fields => false))
154
+ end
155
+ output_buffer.concat(form) if Formtastic::Util.rails3?
156
+ end
157
+
158
+ it 'should have a checkbox input for each post' do
159
+ ::Post.find(:all).each do |post|
160
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
161
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.find(:all).length)
162
+ end
163
+ end
164
+
165
+ it "should mark input as checked if it's the the existing choice" do
166
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
167
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
168
+ end
169
+
170
+ it 'should not generate empty hidden inputs' do
171
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).length)
172
+ end
120
173
  end
121
174
 
122
175
  describe 'when :selected is set' do
@@ -250,10 +303,71 @@ describe 'check_boxes input' do
250
303
  end
251
304
 
252
305
  end
306
+
307
+ describe "with i18n of the legend label" do
308
+
309
+ before do
310
+ ::I18n.backend.store_translations :en, :formtastic => { :labels => { :post => { :authors => "Translated!" }}}
311
+ Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
312
+
313
+ @new_post.stub!(:author_ids).and_return(nil)
314
+ @form = semantic_form_for(@new_post) do |builder|
315
+ concat(builder.input(:authors, :as => :check_boxes))
316
+ end
317
+ end
318
+
319
+ after do
320
+ ::I18n.backend.reload!
321
+ Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
322
+ end
323
+
324
+ it "should do foo" do
325
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
326
+ output_buffer.should have_tag("legend.label label", /Translated/)
327
+ end
328
+
329
+ end
330
+
331
+ describe "when :label option is set" do
332
+ before do
333
+ @new_post.stub!(:author_ids).and_return(nil)
334
+ @form = semantic_form_for(@new_post) do |builder|
335
+ concat(builder.input(:authors, :as => :check_boxes, :label => 'The authors'))
336
+ end
337
+ end
338
+
339
+ it "should output the correct label title" do
340
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
341
+ output_buffer.should have_tag("legend.label label", /The authors/)
342
+ end
343
+ end
344
+ end
253
345
 
346
+ describe 'for a has_and_belongs_to_many association' do
347
+
348
+ before do
349
+ @output_buffer = ''
350
+ mock_everything
351
+
352
+ @form = semantic_form_for(@freds_post) do |builder|
353
+ concat(builder.input(:authors, :as => :check_boxes))
354
+ end
355
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
356
+ end
357
+
358
+ it 'should render checkboxes' do
359
+ # I'm aware these two lines test the same thing
360
+ output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
361
+ output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.find(:all).size)
362
+ end
363
+
364
+ it 'should only select checkboxes that are present in the association' do
365
+ # I'm aware these two lines test the same thing
366
+ output_buffer.should have_tag('input[checked="checked"]', :count => 1)
367
+ output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
368
+ end
369
+
254
370
  end
255
-
256
-
257
371
 
258
372
  end
259
373