justinfrench-formtastic 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/formtastic.rb +17 -3
  2. data/spec/formtastic_spec.rb +68 -8
  3. metadata +2 -2
data/lib/formtastic.rb CHANGED
@@ -241,7 +241,7 @@ module Formtastic #:nodoc:
241
241
  if @object && args.empty?
242
242
  args = @object.class.reflections.map { |n,_| n if _.macro == :belongs_to }
243
243
  args += @object.class.content_columns.map(&:name)
244
- args -= %w[created_at updated_at created_on updated_on]
244
+ args -= %w[created_at updated_at created_on updated_on lock_version]
245
245
  args.compact!
246
246
  end
247
247
  contents = args.map { |method| input(method.to_sym) }
@@ -279,8 +279,14 @@ module Formtastic #:nodoc:
279
279
  #
280
280
  # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" />
281
281
  #
282
- def commit_button(value=nil, options={})
283
- value ||= save_or_create_button_text
282
+ # And you can pass html atributes down to the input, with or without the button text:
283
+ #
284
+ # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" />
285
+ # <%= form.commit_button :class => "pretty" %> => <input name="commit" type="submit" value="Save Post" class="pretty" />
286
+
287
+ def commit_button(*args)
288
+ value = args.first.is_a?(String) ? args.shift : save_or_create_button_text
289
+ options = args.shift || {}
284
290
  button_html = options.delete(:button_html) || {}
285
291
  template.content_tag(:li, self.submit(value, button_html), :class => "commit")
286
292
  end
@@ -476,6 +482,7 @@ module Formtastic #:nodoc:
476
482
  #
477
483
  # <label for="book_author_id">Author</label>
478
484
  # <select id="book_author_id" name="book[author_id]">
485
+ # <option value=""></option>
479
486
  # <option value="1">Justin French</option>
480
487
  # <option value="2">Jane Doe</option>
481
488
  # </select>
@@ -486,6 +493,7 @@ module Formtastic #:nodoc:
486
493
  #
487
494
  # <label for="book_chapter_ids">Chapters</label>
488
495
  # <select id="book_chapter_ids" name="book[chapter_ids]">
496
+ # <option value=""></option>
489
497
  # <option value="1">Chapter 1</option>
490
498
  # <option value="2">Chapter 2</option>
491
499
  # </select>
@@ -496,6 +504,7 @@ module Formtastic #:nodoc:
496
504
  #
497
505
  # <label for="book_author_ids">Authors</label>
498
506
  # <select id="book_author_ids" name="book[author_ids]">
507
+ # <option value=""></option>
499
508
  # <option value="1">Justin French</option>
500
509
  # <option value="2">Jane Doe</option>
501
510
  # </select>
@@ -543,9 +552,12 @@ module Formtastic #:nodoc:
543
552
  #
544
553
  # f.input :authors, :input_html => {:size => 20, :multiple => true}
545
554
  #
555
+ # By default, all select inputs will have a blank option at the top of the list. You can add
556
+ # a prompt with the :prompt option, or disable the blank option with :include_blank => false.
546
557
  def select_input(method, options)
547
558
  collection = find_collection_for_column(method, options)
548
559
  html_options = options.delete(:input_html) || {}
560
+ options[:include_blank] ||= true
549
561
 
550
562
  reflection = find_reflection(method)
551
563
  if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
@@ -959,6 +971,8 @@ module Formtastic #:nodoc:
959
971
  # method. This methods is currently used by radio and datetime inputs.
960
972
  #
961
973
  def field_set_and_list_wrapping_for_method(method, options, contents)
974
+ contents = contents.join if contents.respond_to?(:join)
975
+
962
976
  template.content_tag(:fieldset,
963
977
  %{<legend>#{self.label(method, options.slice(:label, :required).merge!(:as_span => true))}</legend>} +
964
978
  template.content_tag(:ol, contents)
@@ -1395,7 +1395,7 @@ describe 'Formtastic' do
1395
1395
  end
1396
1396
 
1397
1397
  it 'should have a select option for each Author' do
1398
- output_buffer.should have_tag('form li select option', :count => Author.find(:all).size)
1398
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size + 1)
1399
1399
  Author.find(:all).each do |author|
1400
1400
  output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
1401
1401
  end
@@ -1437,7 +1437,7 @@ describe 'Formtastic' do
1437
1437
  end
1438
1438
 
1439
1439
  it 'should have a select option for each Post' do
1440
- output_buffer.should have_tag('form li select option', :count => Post.find(:all).size)
1440
+ output_buffer.should have_tag('form li select option', :count => Post.find(:all).size + 1)
1441
1441
  Post.find(:all).each do |post|
1442
1442
  output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
1443
1443
  end
@@ -1479,7 +1479,7 @@ describe 'Formtastic' do
1479
1479
  end
1480
1480
 
1481
1481
  it 'should have a select option for each Author' do
1482
- output_buffer.should have_tag('form li select option', :count => Author.find(:all).size)
1482
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size + 1)
1483
1483
  Author.find(:all).each do |author|
1484
1484
  output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
1485
1485
  end
@@ -1489,8 +1489,34 @@ describe 'Formtastic' do
1489
1489
  output_buffer.should have_tag('form li select option[@selected]', :count => 1)
1490
1490
  end
1491
1491
  end
1492
-
1493
- describe 'when :include_blank => true, :prompt => "choose something" is set' do
1492
+
1493
+ describe 'when :include_blank is not set' do
1494
+ before do
1495
+ @new_post.stub!(:author_id).and_return(nil)
1496
+ semantic_form_for(@new_post) do |builder|
1497
+ concat(builder.input(:author, :as => :select))
1498
+ end
1499
+ end
1500
+
1501
+ it 'should have a blank option by default' do
1502
+ output_buffer.should have_tag("form li select option[@value='']", //)
1503
+ end
1504
+ end
1505
+
1506
+ describe 'when :include_blank is set to false' do
1507
+ before do
1508
+ @new_post.stub!(:author_id).and_return(nil)
1509
+ semantic_form_for(@new_post) do |builder|
1510
+ concat(builder.input(:author, :as => :select, :include_blank => false))
1511
+ end
1512
+ end
1513
+
1514
+ it 'should not have a blank option' do
1515
+ output_buffer.should have_tag("form li select option[@value='']", //)
1516
+ end
1517
+ end
1518
+
1519
+ describe 'when :include_blank => true and :prompt => "choose something" is set' do
1494
1520
  before do
1495
1521
  @new_post.stub!(:author_id).and_return(nil)
1496
1522
  semantic_form_for(@new_post) do |builder|
@@ -1521,7 +1547,7 @@ describe 'Formtastic' do
1521
1547
 
1522
1548
  it 'should generate select inputs' do
1523
1549
  output_buffer.should have_tag('form li select#project_author')
1524
- output_buffer.should have_tag('form li select option', :count => Author.find(:all).size)
1550
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size + 1)
1525
1551
  end
1526
1552
 
1527
1553
  it 'should generate an option to each item' do
@@ -1680,7 +1706,7 @@ describe 'Formtastic' do
1680
1706
  semantic_form_for(@new_post) do |builder|
1681
1707
  concat(builder.input(:author, :as => type, :collection => @authors))
1682
1708
  end
1683
- output_buffer.should have_tag("form li.#{type} #{countable}", :count => @authors.size)
1709
+ output_buffer.should have_tag("form li.#{type} #{countable}", :count => @authors.size + (type == :select ? 1 : 0))
1684
1710
  end
1685
1711
 
1686
1712
  describe 'and the :collection is an array of strings' do
@@ -1856,7 +1882,7 @@ describe 'Formtastic' do
1856
1882
  end
1857
1883
 
1858
1884
  it "should generate two #{countable}" do
1859
- output_buffer.should have_tag("form li.#{type} #{countable}", :count => 2)
1885
+ output_buffer.should have_tag("form li.#{type} #{countable}", :count => (type == :select ? 3 : 2))
1860
1886
  output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="true"]})
1861
1887
  output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="false"]})
1862
1888
  end
@@ -2731,7 +2757,41 @@ describe 'Formtastic' do
2731
2757
  output_buffer.should have_tag('li.commit input#my_id')
2732
2758
  output_buffer.should have_tag('li.commit input.my_class')
2733
2759
  end
2760
+
2761
+ end
2734
2762
 
2763
+ describe 'when the first option is a string and the second is a hash' do
2764
+
2765
+ before do
2766
+ @new_post.stub!(:new_record?).and_return(false)
2767
+ semantic_form_for(@new_post) do |builder|
2768
+ concat(builder.commit_button("a string", :button_html => { :class => "pretty"}))
2769
+ end
2770
+ end
2771
+
2772
+ it "should render the string as the value of the button" do
2773
+ output_buffer.should have_tag('li input[@value="a string"]')
2774
+ end
2775
+
2776
+ it "should deal with the options hash" do
2777
+ output_buffer.should have_tag('li input.pretty')
2778
+ end
2779
+
2780
+ end
2781
+
2782
+ describe 'when the first option is a hash' do
2783
+
2784
+ before do
2785
+ @new_post.stub!(:new_record?).and_return(false)
2786
+ semantic_form_for(@new_post) do |builder|
2787
+ concat(builder.commit_button(:button_html => { :class => "pretty"}))
2788
+ end
2789
+ end
2790
+
2791
+ it "should deal with the options hash" do
2792
+ output_buffer.should have_tag('li input.pretty')
2793
+ end
2794
+
2735
2795
  end
2736
2796
 
2737
2797
  describe 'when used on an existing record' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: justinfrench-formtastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin French
@@ -9,7 +9,7 @@ autorequire: formtastic
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-21 00:00:00 -07:00
12
+ date: 2009-05-30 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15