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,163 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'string 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(:title, :as => :string))
17
+ end)
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class(:string)
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_title_input")
24
+ it_should_have_label_with_text(/Title/)
25
+ it_should_have_label_for("post_title")
26
+ it_should_have_input_with_id("post_title")
27
+ it_should_have_input_with_type(:text)
28
+ it_should_have_input_with_name("post[title]")
29
+ it_should_have_maxlength_matching_column_limit
30
+ it_should_use_default_text_field_size_when_not_nil(:string)
31
+ it_should_not_use_default_text_field_size_when_nil(:string)
32
+ it_should_apply_custom_input_attributes_when_input_html_provided(:string)
33
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
34
+ it_should_apply_error_logic_for_input_type(:string)
35
+
36
+ def input_field_for_method_should_have_maxlength(method, maxlength)
37
+ concat(semantic_form_for(@new_post) do |builder|
38
+ concat(builder.input(method))
39
+ end)
40
+ output_buffer.should have_tag("form li input[@maxlength='#{maxlength}']")
41
+ end
42
+
43
+ describe 'and its a ActiveModel' do
44
+ let(:default_maxlength) { 50 }
45
+
46
+ before do
47
+ @new_post.stub!(:class).and_return(::PostModel)
48
+ end
49
+
50
+ after do
51
+ @new_post.stub!(:class).and_return(::Post)
52
+ end
53
+
54
+ describe 'and validates_length_of was called for the method' do
55
+ def should_have_maxlength(maxlength, options)
56
+ @new_post.class.should_receive(:validators_on).with(:title).at_least(1).and_return([
57
+ active_model_length_validator([:title], options[:options])
58
+ ])
59
+
60
+ concat(semantic_form_for(@new_post) do |builder|
61
+ concat(builder.input(:title))
62
+ end)
63
+
64
+ output_buffer.should have_tag("form li input##{@new_post.class.name.underscore}_title[@maxlength='#{maxlength}']")
65
+ end
66
+
67
+ it 'should have maxlength if the optional :if or :unless options are not supplied' do
68
+ should_have_maxlength(42, :options => {:maximum => 42})
69
+ end
70
+
71
+ it 'should have default maxlength if the optional :if condition is not satisifed' do
72
+ should_have_maxlength(default_maxlength, :options => {:maximum => 42, :if => false})
73
+ end
74
+
75
+ it 'should have default_maxlength if the optional :if proc evaluates to false' do
76
+ should_have_maxlength(default_maxlength, :options => {:maximum => 42, :if => proc { |record| false }})
77
+ end
78
+
79
+ it 'should have maxlength if the optional :if proc evaluates to true' do
80
+ should_have_maxlength(42, :options => { :maximum => 42, :if => proc { |record| true } })
81
+ end
82
+
83
+ it 'should have default maxlength if the optional :if with a method name evaluates to false' do
84
+ @new_post.should_receive(:specify_maxlength).at_least(1).and_return(false)
85
+ should_have_maxlength(default_maxlength, :options => { :maximum => 42, :if => :specify_maxlength })
86
+ end
87
+
88
+ it 'should have maxlength if the optional :if with a method name evaluates to true' do
89
+ @new_post.should_receive(:specify_maxlength).at_least(1).and_return(true)
90
+ should_have_maxlength(42, :options => { :maximum => 42, :if => :specify_maxlength })
91
+ end
92
+
93
+ it 'should have default maxlength if the optional :unless proc evaluates to true' do
94
+ should_have_maxlength(default_maxlength, :options => { :maximum => 42, :unless => proc { |record| true } })
95
+ end
96
+
97
+ it 'should have maxlength if the optional :unless proc evaluates to false' do
98
+ should_have_maxlength(42, :options => { :maximum => 42, :unless => proc { |record| false } })
99
+ end
100
+
101
+ it 'should have default maxlength if the optional :unless with a method name evaluates to true' do
102
+ @new_post.should_receive(:specify_maxlength).at_least(1).and_return(true)
103
+ should_have_maxlength(default_maxlength, :options => { :maximum => 42, :unless => :specify_maxlength })
104
+ end
105
+
106
+ it 'should have maxlength if the optional :unless with a method name evaluates to false' do
107
+ @new_post.should_receive(:specify_maxlength).at_least(1).and_return(false)
108
+ should_have_maxlength(42, :options => { :maximum => 42, :unless => :specify_maxlength })
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "when namespace is provided" do
115
+
116
+ before do
117
+ concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
118
+ concat(builder.input(:title, :as => :string))
119
+ end)
120
+ end
121
+
122
+ it_should_have_input_wrapper_with_id("context2_post_title_input")
123
+ it_should_have_label_and_input_with_id("context2_post_title")
124
+
125
+ end
126
+
127
+ describe "when no object is provided" do
128
+ before do
129
+ concat(semantic_form_for(:project, :url => 'http://test.host/') do |builder|
130
+ concat(builder.input(:title, :as => :string))
131
+ end)
132
+ end
133
+
134
+ it_should_have_label_with_text(/Title/)
135
+ it_should_have_label_for("project_title")
136
+ it_should_have_input_with_id("project_title")
137
+ it_should_have_input_with_type(:text)
138
+ it_should_have_input_with_name("project[title]")
139
+ end
140
+
141
+ describe "when size is nil" do
142
+ before do
143
+ concat(semantic_form_for(:project, :url => 'http://test.host/') do |builder|
144
+ concat(builder.input(:title, :as => :string, :input_html => {:size => nil}))
145
+ end)
146
+ end
147
+
148
+ it "should have no size attribute" do
149
+ output_buffer.should_not have_tag("input[@size]")
150
+ end
151
+ end
152
+
153
+ describe "when required" do
154
+ it "should add the required attribute to the input's html options" do
155
+ concat(semantic_form_for(@new_post) do |builder|
156
+ concat(builder.input(:title, :as => :string, :required => true))
157
+ end)
158
+ output_buffer.should have_tag("input[@required]")
159
+ end
160
+ end
161
+
162
+ end
163
+
@@ -0,0 +1,158 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'text input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+
12
+ concat(semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:body, :as => :text))
14
+ end)
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("text")
18
+ it_should_have_input_wrapper_with_class(:input)
19
+ it_should_have_input_wrapper_with_id("post_body_input")
20
+ it_should_have_label_with_text(/Body/)
21
+ it_should_have_label_for("post_body")
22
+ it_should_have_textarea_with_id("post_body")
23
+ it_should_have_textarea_with_name("post[body]")
24
+ it_should_apply_error_logic_for_input_type(:number)
25
+
26
+ it 'should use input_html to style inputs' do
27
+ output_buffer.replace ''
28
+ concat(semantic_form_for(@new_post) do |builder|
29
+ concat(builder.input(:title, :as => :text, :input_html => { :class => 'myclass' }))
30
+ end)
31
+ output_buffer.should have_tag("form li textarea.myclass")
32
+ end
33
+
34
+ it "should have a cols attribute when :cols is a number in :input_html" do
35
+ output_buffer.replace ''
36
+ concat(semantic_form_for(@new_post) do |builder|
37
+ concat(builder.input(:title, :as => :text, :input_html => { :cols => 42 }))
38
+ end)
39
+ output_buffer.should have_tag("form li textarea[@cols='42']")
40
+ end
41
+
42
+ it "should not have a cols attribute when :cols is nil in :input_html" do
43
+ output_buffer.replace ''
44
+ concat(semantic_form_for(@new_post) do |builder|
45
+ concat(builder.input(:title, :as => :text, :input_html => { :cols => nil }))
46
+ end)
47
+ output_buffer.should_not have_tag("form li textarea[@cols]")
48
+ end
49
+
50
+ it "should have a rows attribute when :rows is a number in :input_html" do
51
+ output_buffer.replace ''
52
+ concat(semantic_form_for(@new_post) do |builder|
53
+ concat(builder.input(:title, :as => :text, :input_html => { :rows => 42 }))
54
+ end)
55
+ output_buffer.should have_tag("form li textarea[@rows='42']")
56
+
57
+ end
58
+
59
+ it "should not have a rows attribute when :rows is nil in :input_html" do
60
+ output_buffer.replace ''
61
+ concat(semantic_form_for(@new_post) do |builder|
62
+ concat(builder.input(:title, :as => :text, :input_html => { :rows => nil }))
63
+ end)
64
+ output_buffer.should_not have_tag("form li textarea[@rows]")
65
+ end
66
+
67
+ describe "when namespace is provided" do
68
+
69
+ before do
70
+ @output_buffer = ''
71
+ mock_everything
72
+
73
+ concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
74
+ concat(builder.input(:body, :as => :text))
75
+ end)
76
+ end
77
+
78
+ it_should_have_input_wrapper_with_id("context2_post_body_input")
79
+ it_should_have_textarea_with_id("context2_post_body")
80
+ it_should_have_label_for("context2_post_body")
81
+
82
+ end
83
+
84
+ context "when required" do
85
+ it "should add the required attribute to the input's html options" do
86
+ concat(semantic_form_for(@new_post) do |builder|
87
+ concat(builder.input(:title, :as => :text, :required => true))
88
+ end)
89
+ output_buffer.should have_tag("textarea[@required]")
90
+ end
91
+ end
92
+
93
+ context "when :autofocus is provided in :input_html" do
94
+ before(:each) do
95
+ concat(semantic_form_for(@new_post) do |builder|
96
+ concat(builder.input(:title, :input_html => {:autofocus => true}))
97
+ end)
98
+ end
99
+
100
+ it_should_have_input_wrapper_with_class("autofocus")
101
+
102
+ it "should add the autofocus attribute to the input's html options" do
103
+ output_buffer.should have_tag("input[@autofocus]")
104
+ end
105
+ end
106
+
107
+ context "when :rows is missing in :input_html" do
108
+ before do
109
+ output_buffer.replace ''
110
+ end
111
+
112
+ it "should have a rows attribute matching default_text_area_height if numeric" do
113
+ with_config :default_text_area_height, 12 do
114
+ concat(semantic_form_for(@new_post) do |builder|
115
+ concat(builder.input(:title, :as => :text))
116
+ end)
117
+ output_buffer.should have_tag("form li textarea[@rows='12']")
118
+ end
119
+ end
120
+
121
+ it "should not have a rows attribute if default_text_area_height is nil" do
122
+ with_config :default_text_area_height, nil do
123
+ concat(semantic_form_for(@new_post) do |builder|
124
+ concat(builder.input(:title, :as => :text))
125
+ end)
126
+ output_buffer.should_not have_tag("form li textarea[@rows]")
127
+ end
128
+
129
+ end
130
+ end
131
+
132
+ context "when :cols is missing in :input_html" do
133
+ before do
134
+ output_buffer.replace ''
135
+ end
136
+
137
+ it "should have a cols attribute matching default_text_area_width if numeric" do
138
+ with_config :default_text_area_width, 10 do
139
+ concat(semantic_form_for(@new_post) do |builder|
140
+ concat(builder.input(:title, :as => :text))
141
+ end)
142
+ output_buffer.should have_tag("form li textarea[@cols='10']")
143
+ end
144
+ end
145
+
146
+ it "should not have a cols attribute if default_text_area_width is nil" do
147
+ with_config :default_text_area_width, nil do
148
+ concat(semantic_form_for(@new_post) do |builder|
149
+ concat(builder.input(:title, :as => :text))
150
+ end)
151
+ output_buffer.should_not have_tag("form li textarea[@cols]")
152
+ end
153
+
154
+ end
155
+ end
156
+
157
+ end
158
+
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'time input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe "general" do
14
+ before do
15
+ ::I18n.backend.reload!
16
+ output_buffer.replace ''
17
+ end
18
+
19
+ describe "with :ignore_date" do
20
+ before do
21
+ concat(semantic_form_for(@new_post) do |builder|
22
+ concat(builder.input(:publish_at, :as => :time, :ignore_date => true))
23
+ end)
24
+ end
25
+
26
+ it 'should not have an input for day, month and year' do
27
+ output_buffer.should_not have_tag('#post_publish_at_1i')
28
+ output_buffer.should_not have_tag('#post_publish_at_2i')
29
+ output_buffer.should_not have_tag('#post_publish_at_3i')
30
+ end
31
+
32
+ it 'should have an input for hour and minute' do
33
+ output_buffer.should have_tag('#post_publish_at_4i')
34
+ output_buffer.should have_tag('#post_publish_at_5i')
35
+ end
36
+
37
+ end
38
+
39
+ describe "without seconds" do
40
+ before do
41
+ concat(semantic_form_for(@new_post) do |builder|
42
+ concat(builder.input(:publish_at, :as => :time))
43
+ end)
44
+ end
45
+
46
+ it_should_have_input_wrapper_with_class("time")
47
+ it_should_have_input_wrapper_with_class(:input)
48
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
49
+ it_should_have_a_nested_fieldset
50
+ it_should_have_a_nested_fieldset_with_class('fragments')
51
+ it_should_have_a_nested_ordered_list_with_class('fragments-group')
52
+ it_should_apply_error_logic_for_input_type(:time)
53
+
54
+ it 'should have a legend and label with the label text inside the fieldset' do
55
+ output_buffer.should have_tag('form li.time fieldset legend.label label', /Publish at/)
56
+ end
57
+
58
+ it 'should associate the legend label with the first select' do
59
+ output_buffer.should have_tag('form li.time fieldset legend.label label[@for="post_publish_at_1i"]')
60
+ end
61
+
62
+ it 'should have an ordered list of two items inside the fieldset' do
63
+ output_buffer.should have_tag('form li.time fieldset ol.fragments-group')
64
+ output_buffer.should have_tag('form li.time fieldset ol li.fragment', :count => 2)
65
+ end
66
+
67
+ it 'should have five labels for hour and minute' do
68
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2)
69
+ output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i)
70
+ output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i)
71
+ end
72
+
73
+ it 'should have two selects for hour and minute' do
74
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
75
+ end
76
+ end
77
+
78
+ describe "with seconds" do
79
+ before do
80
+ concat(semantic_form_for(@new_post) do |builder|
81
+ concat(builder.input(:publish_at, :as => :time, :include_seconds => true))
82
+ end)
83
+ end
84
+
85
+ it 'should have five labels for hour and minute' do
86
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => 3)
87
+ output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i)
88
+ output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i)
89
+ output_buffer.should have_tag('form li.time fieldset ol li label', /second/i)
90
+ end
91
+
92
+ it 'should have three selects for hour, minute and seconds' do
93
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 3)
94
+ end
95
+
96
+ it 'should generate a sanitized label and matching ids for attribute' do
97
+ 4.upto(6) do |i|
98
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_publish_at_#{i}i']")
99
+ output_buffer.should have_tag("form li fieldset ol li #post_publish_at_#{i}i")
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ describe ':labels option' do
106
+ fields = [:hour, :minute, :second]
107
+ fields.each do |field|
108
+ it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
109
+ output_buffer.replace ''
110
+ concat(semantic_form_for(@new_post) do |builder|
111
+ concat(builder.input(:created_at, :as => :time, :include_seconds => true, :labels => { field => "another #{field} label" }))
112
+ end)
113
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => fields.length)
114
+ fields.each do |f|
115
+ output_buffer.should have_tag('form li.time fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
116
+ end
117
+ end
118
+
119
+ it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
120
+ output_buffer.replace ''
121
+ concat(semantic_form_for(@new_post) do |builder|
122
+ concat(builder.input(:created_at, :as => :time, :include_seconds => true, :labels => { field => "" }))
123
+ end)
124
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => fields.length-1)
125
+ fields.each do |f|
126
+ output_buffer.should have_tag('form li.time fieldset ol li label', /#{f}/i) unless field == f
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ describe ':namespace option' do
133
+ before do
134
+ concat(semantic_form_for(@new_post, :namespace => 'form2') do |builder|
135
+ concat(builder.input(:publish_at, :as => :time))
136
+ end)
137
+ end
138
+
139
+ it 'should have a tag matching the namespace' do
140
+ output_buffer.should have_tag('#form2_post_publish_at_input')
141
+ output_buffer.should have_tag('#form2_post_publish_at_4i')
142
+ output_buffer.should have_tag('#form2_post_publish_at_5i')
143
+ end
144
+ end
145
+
146
+ describe "when required" do
147
+ it "should add the required attribute to the input's html options" do
148
+ concat(semantic_form_for(@new_post) do |builder|
149
+ concat(builder.input(:title, :as => :time, :required => true))
150
+ end)
151
+ output_buffer.should have_tag("select[@required]", :count => 2)
152
+ end
153
+ end
154
+
155
+ end