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,93 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'boolean 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(: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.should have_tag('form li label', :count => 1)
23
+ output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
24
+ output_buffer.should have_tag('form li label', /Allow comments/)
25
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
26
+ end
27
+
28
+ it 'should generate a checkbox input' do
29
+ output_buffer.should have_tag('form li label input')
30
+ output_buffer.should have_tag('form li label input#post_allow_comments')
31
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
32
+ output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
33
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
34
+ end
35
+
36
+ it 'should allow checked and unchecked values to be sent' do
37
+ semantic_form_for(@new_post) do |builder|
38
+ concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
39
+ end
40
+
41
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]')
42
+ output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
43
+ end
44
+
45
+ it 'should generate a label and a checkbox even if no object is given' do
46
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
47
+ concat(builder.input(:allow_comments, :as => :boolean))
48
+ end
49
+
50
+ output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
51
+ output_buffer.should have_tag('form li label', /Allow comments/)
52
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
53
+
54
+ output_buffer.should have_tag('form li label input#project_allow_comments')
55
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
56
+ output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
57
+ end
58
+
59
+ describe 'when :selected is set' do
60
+ before do
61
+ @output_buffer = ActiveSupport::SafeBuffer.new
62
+ end
63
+
64
+ describe "not selected" do
65
+ before do
66
+ @new_post.stub!(:allow_comments).and_return(true)
67
+
68
+ semantic_form_for(@new_post) do |builder|
69
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => false))
70
+ end
71
+ end
72
+
73
+ it 'should not be selected' do
74
+ output_buffer.should_not have_tag("form li label input[@type='checkbox'][@checked='checked']")
75
+ end
76
+ end
77
+
78
+ describe "selected" do
79
+ before do
80
+ @new_post.stub!(:allow_comments).and_return(false)
81
+
82
+ semantic_form_for(@new_post) do |builder|
83
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => true))
84
+ end
85
+ end
86
+
87
+ it 'should be selected' do
88
+ output_buffer.should have_tag("form li label input[@type='checkbox'][@checked='checked']")
89
+ end
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,167 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../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 = ActiveSupport::SafeBuffer.new
11
+ mock_everything
12
+
13
+ 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.should have_tag('form li fieldset legend.label label')
27
+ output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
28
+ end
29
+
30
+ it 'should not link the label within the legend to any input' do
31
+ output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
32
+ end
33
+
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 => ::Post.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
+ it 'should generate hidden inputs with default value blank' do
45
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
46
+ end
47
+
48
+ describe "each choice" do
49
+
50
+ it 'should contain a label for the radio input with a nested input and label text' do
51
+ ::Post.find(:all).each do |post|
52
+ output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
53
+ output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
54
+ end
55
+ end
56
+
57
+ it 'should use values as li.class when value_as_class is true' do
58
+ ::Post.find(:all).each do |post|
59
+ output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
60
+ end
61
+ end
62
+
63
+ it 'should have a checkbox input for each post' do
64
+ ::Post.find(:all).each do |post|
65
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
66
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
67
+ end
68
+ end
69
+
70
+ it "should mark input as checked if it's the the existing choice" do
71
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
72
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
73
+ end
74
+ end
75
+
76
+ describe 'and no object is given' do
77
+ before(:each) do
78
+ output_buffer.replace ''
79
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
80
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
81
+ end
82
+ end
83
+
84
+ it 'should generate a fieldset with legend' do
85
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
86
+ end
87
+
88
+ it 'shold generate an li tag for each item in the collection' do
89
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
90
+ end
91
+
92
+ it 'should generate labels for each item' do
93
+ ::Author.find(:all).each do |author|
94
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
95
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
96
+ end
97
+ end
98
+
99
+ it 'should generate inputs for each item' do
100
+ ::Author.find(:all).each do |author|
101
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
102
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
103
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
104
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
105
+ end
106
+ end
107
+ end
108
+
109
+ describe 'when :selected is set' do
110
+ before do
111
+ @output_buffer = ActiveSupport::SafeBuffer.new
112
+ end
113
+
114
+ describe "no selected items" do
115
+ before do
116
+ @new_post.stub!(:author_ids).and_return(nil)
117
+
118
+ semantic_form_for(@new_post) do |builder|
119
+ concat(builder.input(:authors, :as => :check_boxes, :selected => nil))
120
+ end
121
+ end
122
+
123
+ it 'should not have any selected item(s)' do
124
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']")
125
+ end
126
+ end
127
+
128
+ describe "single selected item" do
129
+ before do
130
+ @new_post.stub!(:author_ids).and_return(nil)
131
+
132
+ semantic_form_for(@new_post) do |builder|
133
+ concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id))
134
+ end
135
+ end
136
+
137
+ it "should have one item selected; the specified one" do
138
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 1)
139
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
140
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']")
141
+ end
142
+ end
143
+
144
+ describe "multiple selected items" do
145
+ before do
146
+ @new_post.stub!(:author_ids).and_return(nil)
147
+
148
+ semantic_form_for(@new_post) do |builder|
149
+ concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id]))
150
+ end
151
+ end
152
+
153
+ it "should have multiple items selected; the specified ones" do
154
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 2)
155
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
156
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@bob.id}']")
157
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
158
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']")
159
+ end
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
@@ -0,0 +1,80 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'country input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
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
+ semantic_form_for(@new_post) do |builder|
29
+ builder.stub!(:country_select).and_return(ActiveSupport::SafeBuffer.new("<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.should have_tag('form li label')
42
+ output_buffer.should have_tag('form li label[@for="post_country"]')
43
+ output_buffer.should have_tag('form li label', /Country/)
44
+ end
45
+
46
+ it "should generate a select" do
47
+ output_buffer.should have_tag("form li select")
48
+ end
49
+
50
+ end
51
+
52
+ describe ":priority_countries option" do
53
+
54
+ it "should be passed down to the country_select helper when provided" do
55
+ priority_countries = ["Foo", "Bah"]
56
+ semantic_form_for(@new_post) do |builder|
57
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
58
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
59
+
60
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
61
+ end
62
+ end
63
+
64
+ it "should default to the @@priority_countries config when absent" do
65
+ priority_countries = ::Formtastic::SemanticFormBuilder.priority_countries
66
+ priority_countries.should_not be_empty
67
+ priority_countries.should_not be_nil
68
+
69
+ semantic_form_for(@new_post) do |builder|
70
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
71
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
72
+
73
+ concat(builder.input(:country, :as => :country))
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
@@ -0,0 +1,80 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'currency input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ActiveSupport::SafeBuffer.new
10
+ mock_everything
11
+ end
12
+
13
+ describe "when currency_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(:currency, :as => :currency))
19
+ end
20
+ }.should raise_error
21
+ end
22
+
23
+ end
24
+
25
+ describe "when currency_select is available as a helper (from a plugin)" do
26
+
27
+ before do
28
+ semantic_form_for(@new_post) do |builder|
29
+ builder.stub!(:currency_select).and_return(ActiveSupport::SafeBuffer.new("<select><option>...</option></select>"))
30
+ concat(builder.input(:currency, :as => :currency))
31
+ end
32
+ end
33
+
34
+ it_should_have_input_wrapper_with_class("currency")
35
+ it_should_have_input_wrapper_with_id("post_currency_input")
36
+
37
+ # TODO -- needs stubbing inside the builder block, tricky!
38
+ #it_should_apply_error_logic_for_input_type(:currency)
39
+
40
+ it 'should generate a label for the input' do
41
+ output_buffer.should have_tag('form li label')
42
+ output_buffer.should have_tag('form li label[@for="post_currency"]')
43
+ output_buffer.should have_tag('form li label', /Currency/)
44
+ end
45
+
46
+ it "should generate a select" do
47
+ output_buffer.should have_tag("form li select")
48
+ end
49
+
50
+ end
51
+
52
+ describe ":priority_currencies option" do
53
+
54
+ it "should be passed down to the currency_select helper when provided" do
55
+ priority_currencies = ["Foo", "Bah"]
56
+ semantic_form_for(@new_post) do |builder|
57
+ builder.stub!(:currency_select).and_return(ActiveSupport::SafeBuffer.new("<select><option>...</option></select>"))
58
+ builder.should_receive(:currency_select).with(:currency, priority_currencies, {}, {}).and_return("<select><option>...</option></select>")
59
+
60
+ concat(builder.input(:currency, :as => :currency, :priority_currencies => priority_currencies))
61
+ end
62
+ end
63
+
64
+ it "should default to the @@priority_currencies config when absent" do
65
+ priority_currencies = ::Formtastic::SemanticFormBuilder.priority_currencies
66
+ priority_currencies.should_not be_empty
67
+ priority_currencies.should_not be_nil
68
+
69
+ semantic_form_for(@new_post) do |builder|
70
+ builder.stub!(:currency_select).and_return("<select><option>...</option></select>")
71
+ builder.should_receive(:currency_select).with(:currency, priority_currencies, {}, {}).and_return("<select><option>...</option></select>")
72
+
73
+ concat(builder.input(:currency, :as => :currency))
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
@@ -0,0 +1,143 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'date 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(:publish_at, :as => :date))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("date")
18
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
19
+ it_should_have_a_nested_fieldset
20
+ it_should_apply_error_logic_for_input_type(:date)
21
+
22
+ it 'should have a legend and label with the label text inside the fieldset' do
23
+ output_buffer.should have_tag('form li.date fieldset legend.label label', /Publish at/)
24
+ end
25
+
26
+ it 'should associate the legend label with the first select' do
27
+ output_buffer.should have_tag('form li.date fieldset legend.label')
28
+ output_buffer.should have_tag('form li.date fieldset legend.label label')
29
+ output_buffer.should have_tag('form li.date fieldset legend.label label[@for]')
30
+ output_buffer.should have_tag('form li.date fieldset legend.label label[@for="post_publish_at_1i"]')
31
+ end
32
+
33
+ it 'should have an ordered list of three items inside the fieldset' do
34
+ output_buffer.should have_tag('form li.date fieldset ol')
35
+ output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
36
+ end
37
+
38
+ it 'should have three labels for year, month and day' do
39
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
40
+ output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
41
+ output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
42
+ output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
43
+ end
44
+
45
+ it 'should have three selects for year, month and day' do
46
+ output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
47
+ end
48
+
49
+ describe ':default option' do
50
+
51
+ describe "when the object has a value" do
52
+ it "should select the object value (ignoring :default)" do
53
+ output_buffer.replace ''
54
+ @new_post.stub!(:created_at => Time.mktime(2012))
55
+ semantic_form_for(@new_post) do |builder|
56
+ concat(builder.input(:created_at, :as => :date, :default => Time.mktime(1999)))
57
+ end
58
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
59
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1)
60
+ end
61
+ end
62
+
63
+ describe 'when the object has no value' do
64
+ it "should select the :default if provided as a Date" do
65
+ output_buffer.replace ''
66
+ @new_post.stub!(:created_at => nil)
67
+ semantic_form_for(@new_post) do |builder|
68
+ concat(builder.input(:created_at, :as => :date, :default => Date.new(1999)))
69
+ end
70
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
71
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
72
+ end
73
+
74
+ it "should select the :default if provided as a Time" do
75
+ output_buffer.replace ''
76
+ @new_post.stub!(:created_at => nil)
77
+ semantic_form_for(@new_post) do |builder|
78
+ concat(builder.input(:created_at, :as => :date, :default => Time.mktime(1999)))
79
+ end
80
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
81
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
82
+ end
83
+
84
+ it "should not select an option if the :default is provided as nil" do
85
+ output_buffer.replace ''
86
+ @new_post.stub!(:created_at => nil)
87
+ semantic_form_for(@new_post) do |builder|
88
+ concat(builder.input(:created_at, :as => :date, :default => nil))
89
+ end
90
+ output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]")
91
+ end
92
+
93
+ it "should select Time.now if a :default is not provided" do
94
+ output_buffer.replace ''
95
+ @new_post.stub!(:created_at => nil)
96
+ semantic_form_for(@new_post) do |builder|
97
+ concat(builder.input(:created_at, :as => :date))
98
+ end
99
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
100
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='#{Time.now.year}'][@selected]", :count => 1)
101
+
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ describe ':labels option' do
108
+ fields = [:year, :month, :day]
109
+ fields.each do |field|
110
+ it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
111
+ output_buffer.replace ''
112
+ semantic_form_for(@new_post) do |builder|
113
+ concat(builder.input(:created_at, :as => :date, :labels => { field => "another #{field} label" }))
114
+ end
115
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length)
116
+ fields.each do |f|
117
+ output_buffer.should have_tag('form li.date fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
118
+ end
119
+ end
120
+
121
+ it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
122
+ output_buffer.replace ''
123
+ semantic_form_for(@new_post) do |builder|
124
+ concat(builder.input(:created_at, :as => :date, :labels => { field => "" }))
125
+ end
126
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length-1)
127
+ fields.each do |f|
128
+ output_buffer.should have_tag('form li.date fieldset ol li label', /#{f}/i) unless field == f
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ it 'should warn about :selected deprecation' do
135
+ with_deprecation_silenced do
136
+ ::ActiveSupport::Deprecation.should_receive(:warn)
137
+ semantic_form_for(@new_post) do |builder|
138
+ concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999)))
139
+ end
140
+ end
141
+ end
142
+
143
+ end