formtastic 1.2.4 → 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +46 -0
  5. data/.yardopts +1 -0
  6. data/Appraisals +43 -0
  7. data/CHANGELOG +54 -0
  8. data/DEPRECATIONS +52 -0
  9. data/Gemfile +3 -0
  10. data/README.md +629 -0
  11. data/RELEASE_PROCESS +6 -0
  12. data/Rakefile +35 -0
  13. data/app/assets/stylesheets/formtastic.css +289 -0
  14. data/app/assets/stylesheets/formtastic_ie6.css +33 -0
  15. data/app/assets/stylesheets/formtastic_ie7.css +23 -0
  16. data/formtastic.gemspec +42 -0
  17. data/gemfiles/rails_3.2.gemfile +9 -0
  18. data/gemfiles/rails_4.0.4.gemfile +8 -0
  19. data/gemfiles/rails_4.1.gemfile +8 -0
  20. data/gemfiles/rails_4.2.gemfile +8 -0
  21. data/gemfiles/rails_4.gemfile +8 -0
  22. data/gemfiles/rails_5.0.gemfile +8 -0
  23. data/gemfiles/rails_edge.gemfile +15 -0
  24. data/lib/formtastic.rb +40 -1945
  25. data/lib/formtastic/action_class_finder.rb +18 -0
  26. data/lib/formtastic/actions.rb +11 -0
  27. data/lib/formtastic/actions/base.rb +156 -0
  28. data/lib/formtastic/actions/button_action.rb +67 -0
  29. data/lib/formtastic/actions/buttonish.rb +17 -0
  30. data/lib/formtastic/actions/input_action.rb +70 -0
  31. data/lib/formtastic/actions/link_action.rb +88 -0
  32. data/lib/formtastic/deprecation.rb +42 -0
  33. data/lib/formtastic/engine.rb +11 -0
  34. data/lib/formtastic/form_builder.rb +124 -0
  35. data/lib/formtastic/helpers.rb +16 -0
  36. data/lib/formtastic/helpers/action_helper.rb +162 -0
  37. data/lib/formtastic/helpers/actions_helper.rb +168 -0
  38. data/lib/formtastic/helpers/enum.rb +13 -0
  39. data/lib/formtastic/helpers/errors_helper.rb +81 -0
  40. data/lib/formtastic/helpers/fieldset_wrapper.rb +80 -0
  41. data/lib/formtastic/helpers/file_column_detection.rb +16 -0
  42. data/lib/formtastic/helpers/form_helper.rb +203 -0
  43. data/lib/formtastic/helpers/input_helper.rb +407 -0
  44. data/lib/formtastic/helpers/inputs_helper.rb +411 -0
  45. data/lib/formtastic/helpers/reflection.rb +37 -0
  46. data/lib/formtastic/html_attributes.rb +32 -0
  47. data/lib/formtastic/i18n.rb +4 -2
  48. data/lib/formtastic/input_class_finder.rb +18 -0
  49. data/lib/formtastic/inputs.rb +39 -0
  50. data/lib/formtastic/inputs/base.rb +76 -0
  51. data/lib/formtastic/inputs/base/associations.rb +31 -0
  52. data/lib/formtastic/inputs/base/choices.rb +108 -0
  53. data/lib/formtastic/inputs/base/collections.rb +159 -0
  54. data/lib/formtastic/inputs/base/database.rb +22 -0
  55. data/lib/formtastic/inputs/base/datetime_pickerish.rb +85 -0
  56. data/lib/formtastic/inputs/base/errors.rb +58 -0
  57. data/lib/formtastic/inputs/base/fileish.rb +23 -0
  58. data/lib/formtastic/inputs/base/hints.rb +31 -0
  59. data/lib/formtastic/inputs/base/html.rb +53 -0
  60. data/lib/formtastic/inputs/base/labelling.rb +52 -0
  61. data/lib/formtastic/inputs/base/naming.rb +42 -0
  62. data/lib/formtastic/inputs/base/numeric.rb +50 -0
  63. data/lib/formtastic/inputs/base/options.rb +17 -0
  64. data/lib/formtastic/inputs/base/placeholder.rb +17 -0
  65. data/lib/formtastic/inputs/base/stringish.rb +38 -0
  66. data/lib/formtastic/inputs/base/timeish.rb +241 -0
  67. data/lib/formtastic/inputs/base/validations.rb +215 -0
  68. data/lib/formtastic/inputs/base/wrapping.rb +50 -0
  69. data/lib/formtastic/inputs/boolean_input.rb +118 -0
  70. data/lib/formtastic/inputs/check_boxes_input.rb +197 -0
  71. data/lib/formtastic/inputs/color_input.rb +42 -0
  72. data/lib/formtastic/inputs/country_input.rb +86 -0
  73. data/lib/formtastic/inputs/datalist_input.rb +41 -0
  74. data/lib/formtastic/inputs/date_picker_input.rb +93 -0
  75. data/lib/formtastic/inputs/date_select_input.rb +34 -0
  76. data/lib/formtastic/inputs/datetime_picker_input.rb +103 -0
  77. data/lib/formtastic/inputs/datetime_select_input.rb +12 -0
  78. data/lib/formtastic/inputs/email_input.rb +41 -0
  79. data/lib/formtastic/inputs/file_input.rb +42 -0
  80. data/lib/formtastic/inputs/hidden_input.rb +62 -0
  81. data/lib/formtastic/inputs/number_input.rb +88 -0
  82. data/lib/formtastic/inputs/password_input.rb +41 -0
  83. data/lib/formtastic/inputs/phone_input.rb +42 -0
  84. data/lib/formtastic/inputs/radio_input.rb +163 -0
  85. data/lib/formtastic/inputs/range_input.rb +95 -0
  86. data/lib/formtastic/inputs/search_input.rb +41 -0
  87. data/lib/formtastic/inputs/select_input.rb +235 -0
  88. data/lib/formtastic/inputs/string_input.rb +36 -0
  89. data/lib/formtastic/inputs/text_input.rb +48 -0
  90. data/lib/formtastic/inputs/time_picker_input.rb +99 -0
  91. data/lib/formtastic/inputs/time_select_input.rb +38 -0
  92. data/lib/formtastic/inputs/time_zone_input.rb +58 -0
  93. data/lib/formtastic/inputs/url_input.rb +41 -0
  94. data/lib/formtastic/localized_string.rb +17 -0
  95. data/lib/formtastic/localizer.rb +152 -0
  96. data/lib/formtastic/namespaced_class_finder.rb +99 -0
  97. data/lib/formtastic/util.rb +35 -16
  98. data/lib/formtastic/version.rb +3 -0
  99. data/lib/generators/formtastic/form/form_generator.rb +64 -37
  100. data/lib/generators/formtastic/input/input_generator.rb +46 -0
  101. data/lib/generators/formtastic/install/install_generator.rb +13 -5
  102. data/lib/generators/templates/_form.html.erb +10 -4
  103. data/lib/generators/templates/_form.html.haml +8 -4
  104. data/lib/generators/templates/_form.html.slim +8 -0
  105. data/lib/generators/templates/formtastic.rb +77 -44
  106. data/lib/generators/templates/input.rb +19 -0
  107. data/lib/locale/en.yml +3 -0
  108. data/sample/basic_inputs.html +224 -0
  109. data/sample/config.ru +69 -0
  110. data/sample/index.html +14 -0
  111. data/spec/action_class_finder_spec.rb +12 -0
  112. data/spec/actions/button_action_spec.rb +63 -0
  113. data/spec/actions/generic_action_spec.rb +521 -0
  114. data/spec/actions/input_action_spec.rb +59 -0
  115. data/spec/actions/link_action_spec.rb +92 -0
  116. data/spec/builder/custom_builder_spec.rb +116 -0
  117. data/spec/builder/error_proc_spec.rb +27 -0
  118. data/spec/builder/semantic_fields_for_spec.rb +142 -0
  119. data/spec/fast_spec_helper.rb +12 -0
  120. data/spec/generators/formtastic/form/form_generator_spec.rb +131 -0
  121. data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
  122. data/spec/generators/formtastic/install/install_generator_spec.rb +47 -0
  123. data/spec/helpers/action_helper_spec.rb +19 -0
  124. data/spec/helpers/actions_helper_spec.rb +143 -0
  125. data/spec/helpers/form_helper_spec.rb +218 -0
  126. data/spec/helpers/input_helper_spec.rb +6 -0
  127. data/spec/helpers/inputs_helper_spec.rb +655 -0
  128. data/spec/helpers/namespaced_action_helper_spec.rb +43 -0
  129. data/spec/helpers/namespaced_input_helper_spec.rb +36 -0
  130. data/spec/helpers/reflection_helper_spec.rb +32 -0
  131. data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
  132. data/spec/i18n_spec.rb +210 -0
  133. data/spec/input_class_finder_spec.rb +10 -0
  134. data/spec/inputs/base/collections_spec.rb +76 -0
  135. data/spec/inputs/base/validations_spec.rb +342 -0
  136. data/spec/inputs/boolean_input_spec.rb +254 -0
  137. data/spec/inputs/check_boxes_input_spec.rb +546 -0
  138. data/spec/inputs/color_input_spec.rb +97 -0
  139. data/spec/inputs/country_input_spec.rb +133 -0
  140. data/spec/inputs/custom_input_spec.rb +55 -0
  141. data/spec/inputs/datalist_input_spec.rb +61 -0
  142. data/spec/inputs/date_picker_input_spec.rb +449 -0
  143. data/spec/inputs/date_select_input_spec.rb +235 -0
  144. data/spec/inputs/datetime_picker_input_spec.rb +490 -0
  145. data/spec/inputs/datetime_select_input_spec.rb +193 -0
  146. data/spec/inputs/email_input_spec.rb +85 -0
  147. data/spec/inputs/file_input_spec.rb +89 -0
  148. data/spec/inputs/hidden_input_spec.rb +135 -0
  149. data/spec/inputs/include_blank_spec.rb +78 -0
  150. data/spec/inputs/label_spec.rb +149 -0
  151. data/spec/inputs/number_input_spec.rb +815 -0
  152. data/spec/inputs/password_input_spec.rb +99 -0
  153. data/spec/inputs/phone_input_spec.rb +85 -0
  154. data/spec/inputs/placeholder_spec.rb +71 -0
  155. data/spec/inputs/radio_input_spec.rb +328 -0
  156. data/spec/inputs/range_input_spec.rb +505 -0
  157. data/spec/inputs/readonly_spec.rb +50 -0
  158. data/spec/inputs/search_input_spec.rb +84 -0
  159. data/spec/inputs/select_input_spec.rb +615 -0
  160. data/spec/inputs/string_input_spec.rb +260 -0
  161. data/spec/inputs/text_input_spec.rb +187 -0
  162. data/spec/inputs/time_picker_input_spec.rb +455 -0
  163. data/spec/inputs/time_select_input_spec.rb +248 -0
  164. data/spec/inputs/time_zone_input_spec.rb +143 -0
  165. data/spec/inputs/url_input_spec.rb +85 -0
  166. data/spec/inputs/with_options_spec.rb +43 -0
  167. data/spec/localizer_spec.rb +130 -0
  168. data/spec/namespaced_class_finder_spec.rb +79 -0
  169. data/spec/spec.opts +2 -0
  170. data/spec/spec_helper.rb +525 -0
  171. data/spec/support/custom_macros.rb +564 -0
  172. data/spec/support/deprecation.rb +6 -0
  173. data/spec/support/shared_examples.rb +1313 -0
  174. data/spec/support/specialized_class_finder_shared_example.rb +27 -0
  175. data/spec/support/test_environment.rb +31 -0
  176. data/spec/util_spec.rb +66 -0
  177. metadata +434 -161
  178. data/README.textile +0 -682
  179. data/generators/form/USAGE +0 -16
  180. data/generators/form/form_generator.rb +0 -111
  181. data/generators/formtastic/formtastic_generator.rb +0 -26
  182. data/init.rb +0 -5
  183. data/lib/formtastic/layout_helper.rb +0 -12
  184. data/lib/formtastic/railtie.rb +0 -14
  185. data/lib/generators/templates/formtastic.css +0 -145
  186. data/lib/generators/templates/formtastic_changes.css +0 -14
  187. data/lib/generators/templates/rails2/_form.html.erb +0 -5
  188. data/lib/generators/templates/rails2/_form.html.haml +0 -4
  189. data/rails/init.rb +0 -2
@@ -0,0 +1,505 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'range 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(@bob) do |builder|
16
+ concat(builder.input(:age, :as => :range))
17
+ end)
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class(:range)
21
+ it_should_have_input_wrapper_with_class(:input)
22
+ it_should_have_input_wrapper_with_class(:numeric)
23
+ it_should_have_input_wrapper_with_class(:stringish)
24
+ it_should_have_input_wrapper_with_id("author_age_input")
25
+ it_should_have_label_with_text(/Age/)
26
+ it_should_have_label_for("author_age")
27
+ it_should_have_input_with_id("author_age")
28
+ it_should_have_input_with_type(:range)
29
+ it_should_have_input_with_name("author[age]")
30
+
31
+ end
32
+
33
+ describe "when namespace is provided" do
34
+
35
+ before do
36
+ concat(semantic_form_for(@james, :namespace => "context2") do |builder|
37
+ concat(builder.input(:age, :as => :range))
38
+ end)
39
+ end
40
+
41
+ it_should_have_input_wrapper_with_id("context2_author_age_input")
42
+ it_should_have_label_and_input_with_id("context2_author_age")
43
+
44
+ end
45
+
46
+ describe "when index is provided" do
47
+
48
+ before do
49
+ @output_buffer = ''
50
+ mock_everything
51
+
52
+ concat(semantic_form_for(@new_post) do |builder|
53
+ concat(builder.fields_for(:author, :index => 3) do |author|
54
+ concat(author.input(:name, :as => :range))
55
+ end)
56
+ end)
57
+ end
58
+
59
+ it 'should index the id of the wrapper' do
60
+ output_buffer.should have_tag("li#post_author_attributes_3_name_input")
61
+ end
62
+
63
+ it 'should index the id of the select tag' do
64
+ output_buffer.should have_tag("input#post_author_attributes_3_name")
65
+ end
66
+
67
+ it 'should index the name of the select tag' do
68
+ output_buffer.should have_tag("input[@name='post[author_attributes][3][name]']")
69
+ end
70
+
71
+ end
72
+
73
+
74
+ describe "when validations require a minimum value (:greater_than)" do
75
+ before do
76
+ @new_post.class.stub(:validators_on).with(:title).and_return([
77
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than=>2})
78
+ ])
79
+ end
80
+
81
+ it "should allow :input_html to override :min" do
82
+ concat(semantic_form_for(@new_post) do |builder|
83
+ builder.input(:title, :as => :range, :input_html => { :min => 5 })
84
+ end)
85
+ output_buffer.should have_tag('input[@min="5"]')
86
+ end
87
+
88
+ it "should allow :input_html to override :min through :in" do
89
+ concat(semantic_form_for(@new_post) do |builder|
90
+ builder.input(:title, :as => :range, :input_html => { :in => 5..102 })
91
+ end)
92
+ output_buffer.should have_tag('input[@min="5"]')
93
+ end
94
+
95
+ it "should allow options to override :min" do
96
+ concat(semantic_form_for(@new_post) do |builder|
97
+ builder.input(:title, :as => :range, :min => 5)
98
+ end)
99
+ output_buffer.should have_tag('input[@min="5"]')
100
+ end
101
+
102
+ it "should allow options to override :min through :in" do
103
+ concat(semantic_form_for(@new_post) do |builder|
104
+ builder.input(:title, :as => :range, :in => 5..102)
105
+ end)
106
+ output_buffer.should have_tag('input[@min="5"]')
107
+ end
108
+
109
+ describe "and the column is an integer" do
110
+ before do
111
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
112
+ end
113
+
114
+ it "should add a min attribute to the input one greater than the validation" do
115
+ concat(semantic_form_for(@new_post) do |builder|
116
+ builder.input(:title, :as => :range)
117
+ end)
118
+ output_buffer.should have_tag('input[@min="3"]')
119
+ end
120
+ end
121
+
122
+ describe "and the column is a float" do
123
+ before do
124
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
125
+ end
126
+
127
+ it "should raise an error" do
128
+ lambda {
129
+ concat(semantic_form_for(@new_post) do |builder|
130
+ builder.input(:title, :as => :range)
131
+ end)
132
+ }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
133
+ end
134
+ end
135
+
136
+ describe "and the column is a big decimal" do
137
+ before do
138
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
139
+ end
140
+
141
+ it "should raise an error" do
142
+ lambda {
143
+ concat(semantic_form_for(@new_post) do |builder|
144
+ builder.input(:title, :as => :range)
145
+ end)
146
+ }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ describe "when validations require a minimum value (:greater_than_or_equal_to)" do
153
+ before do
154
+ @new_post.class.stub(:validators_on).with(:title).and_return([
155
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than_or_equal_to=>2})
156
+ ])
157
+ end
158
+
159
+ it "should allow :input_html to override :min" do
160
+ concat(semantic_form_for(@new_post) do |builder|
161
+ builder.input(:title, :as => :range, :input_html => { :min => 5 })
162
+ end)
163
+ output_buffer.should have_tag('input[@min="5"]')
164
+ end
165
+
166
+ it "should allow options to override :min" do
167
+ concat(semantic_form_for(@new_post) do |builder|
168
+ builder.input(:title, :as => :range, :min => 5)
169
+ end)
170
+ output_buffer.should have_tag('input[@min="5"]')
171
+ end
172
+
173
+ it "should allow :input_html to override :min with :in" do
174
+ concat(semantic_form_for(@new_post) do |builder|
175
+ builder.input(:title, :as => :range, :input_html => { :in => 5..102 })
176
+ end)
177
+ output_buffer.should have_tag('input[@min="5"]')
178
+ end
179
+
180
+ it "should allow options to override :min with :in" do
181
+ concat(semantic_form_for(@new_post) do |builder|
182
+ builder.input(:title, :as => :range, :in => 5..102)
183
+ end)
184
+ output_buffer.should have_tag('input[@min="5"]')
185
+ end
186
+
187
+
188
+ [:integer, :decimal, :float].each do |column_type|
189
+ describe "and the column is a #{column_type}" do
190
+ before do
191
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
192
+ end
193
+
194
+ it "should add a max attribute to the input equal to the validation" do
195
+ concat(semantic_form_for(@new_post) do |builder|
196
+ builder.input(:title, :as => :range)
197
+ end)
198
+ output_buffer.should have_tag('input[@min="2"]')
199
+ end
200
+ end
201
+ end
202
+
203
+ describe "and there is no column" do
204
+ before do
205
+ @new_post.stub(:column_for_attribute).with(:title).and_return(nil)
206
+ end
207
+
208
+ it "should add a max attribute to the input equal to the validation" do
209
+ concat(semantic_form_for(@new_post) do |builder|
210
+ builder.input(:title, :as => :range)
211
+ end)
212
+ output_buffer.should have_tag('input[@min="2"]')
213
+ end
214
+ end
215
+ end
216
+
217
+ describe "when validations do not require a minimum value" do
218
+
219
+ it "should default to 1" do
220
+ concat(semantic_form_for(@new_post) do |builder|
221
+ builder.input(:title, :as => :range)
222
+ end)
223
+ output_buffer.should have_tag('input[@min="1"]')
224
+ end
225
+
226
+ end
227
+
228
+ describe "when validations require a maximum value (:less_than)" do
229
+ before do
230
+ @new_post.class.stub(:validators_on).with(:title).and_return([
231
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than=>20})
232
+ ])
233
+ end
234
+
235
+ it "should allow :input_html to override :max" do
236
+ concat(semantic_form_for(@new_post) do |builder|
237
+ builder.input(:title, :as => :range, :input_html => { :max => 102 })
238
+ end)
239
+ output_buffer.should have_tag('input[@max="102"]')
240
+ end
241
+
242
+ it "should allow option to override :max" do
243
+ concat(semantic_form_for(@new_post) do |builder|
244
+ builder.input(:title, :as => :range, :max => 102)
245
+ end)
246
+ output_buffer.should have_tag('input[@max="102"]')
247
+ end
248
+
249
+ it "should allow :input_html to override :max with :in" do
250
+ concat(semantic_form_for(@new_post) do |builder|
251
+ builder.input(:title, :as => :range, :input_html => { :in => 1..102 })
252
+ end)
253
+ output_buffer.should have_tag('input[@max="102"]')
254
+ end
255
+
256
+ it "should allow option to override :max with :in" do
257
+ concat(semantic_form_for(@new_post) do |builder|
258
+ builder.input(:title, :as => :range, :in => 1..102)
259
+ end)
260
+ output_buffer.should have_tag('input[@max="102"]')
261
+ end
262
+
263
+ describe "and the column is an integer" do
264
+ before do
265
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
266
+ end
267
+
268
+ it "should add a max attribute to the input one greater than the validation" do
269
+ concat(semantic_form_for(@new_post) do |builder|
270
+ builder.input(:title, :as => :range)
271
+ end)
272
+ output_buffer.should have_tag('input[@max="19"]')
273
+ end
274
+ end
275
+
276
+ describe "and the column is a float" do
277
+ before do
278
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
279
+ end
280
+
281
+ it "should raise an error" do
282
+ lambda {
283
+ concat(semantic_form_for(@new_post) do |builder|
284
+ builder.input(:title, :as => :range)
285
+ end)
286
+ }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
287
+ end
288
+ end
289
+
290
+ describe "and the column is a big decimal" do
291
+ before do
292
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
293
+ end
294
+
295
+ it "should raise an error" do
296
+ lambda {
297
+ concat(semantic_form_for(@new_post) do |builder|
298
+ builder.input(:title, :as => :range)
299
+ end)
300
+ }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
301
+ end
302
+ end
303
+
304
+ end
305
+
306
+ describe "when validations require a maximum value (:less_than_or_equal_to)" do
307
+ before do
308
+ @new_post.class.stub(:validators_on).with(:title).and_return([
309
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than_or_equal_to=>20})
310
+ ])
311
+ end
312
+
313
+ it "should allow :input_html to override :max" do
314
+ concat(semantic_form_for(@new_post) do |builder|
315
+ builder.input(:title, :as => :range, :input_html => { :max => 102 })
316
+ end)
317
+ output_buffer.should have_tag('input[@max="102"]')
318
+ end
319
+
320
+ it "should allow options to override :max" do
321
+ concat(semantic_form_for(@new_post) do |builder|
322
+ builder.input(:title, :as => :range, :max => 102)
323
+ end)
324
+ output_buffer.should have_tag('input[@max="102"]')
325
+ end
326
+
327
+ it "should allow :input_html to override :max with :in" do
328
+ concat(semantic_form_for(@new_post) do |builder|
329
+ builder.input(:title, :as => :range, :input_html => { :in => 1..102 })
330
+ end)
331
+ output_buffer.should have_tag('input[@max="102"]')
332
+ end
333
+
334
+ it "should allow options to override :max with :in" do
335
+ concat(semantic_form_for(@new_post) do |builder|
336
+ builder.input(:title, :as => :range, :in => 1..102)
337
+ end)
338
+ output_buffer.should have_tag('input[@max="102"]')
339
+ end
340
+
341
+ [:integer, :decimal, :float].each do |column_type|
342
+ describe "and the column is a #{column_type}" do
343
+ before do
344
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
345
+ end
346
+
347
+ it "should add a max attribute to the input equal to the validation" do
348
+ concat(semantic_form_for(@new_post) do |builder|
349
+ builder.input(:title, :as => :range)
350
+ end)
351
+ output_buffer.should have_tag('input[@max="20"]')
352
+ end
353
+ end
354
+ end
355
+
356
+ describe "and there is no column" do
357
+ before do
358
+ @new_post.stub(:column_for_attribute).with(:title).and_return(nil)
359
+ end
360
+
361
+ it "should add a max attribute to the input equal to the validation" do
362
+ concat(semantic_form_for(@new_post) do |builder|
363
+ builder.input(:title, :as => :range)
364
+ end)
365
+ output_buffer.should have_tag('input[@max="20"]')
366
+ end
367
+ end
368
+ end
369
+
370
+ describe "when validations do not require a maximum value" do
371
+
372
+ it "should default to 1" do
373
+ concat(semantic_form_for(@new_post) do |builder|
374
+ builder.input(:title, :as => :range)
375
+ end)
376
+ output_buffer.should have_tag('input[@max="100"]')
377
+ end
378
+
379
+ end
380
+
381
+ describe "when validations require conflicting minimum values (:greater_than, :greater_than_or_equal_to)" do
382
+ before do
383
+ @new_post.class.stub(:validators_on).with(:title).and_return([
384
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than => 20, :greater_than_or_equal_to=>2})
385
+ ])
386
+ end
387
+
388
+ it "should add a max attribute to the input equal to the :greater_than_or_equal_to validation" do
389
+ concat(semantic_form_for(@new_post) do |builder|
390
+ builder.input(:title, :as => :range)
391
+ end)
392
+ output_buffer.should have_tag('input[@min="2"]')
393
+ end
394
+ end
395
+
396
+ describe "when validations require conflicting maximum values (:less_than, :less_than_or_equal_to)" do
397
+ before do
398
+ @new_post.class.stub(:validators_on).with(:title).and_return([
399
+ active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than => 20, :less_than_or_equal_to=>2})
400
+ ])
401
+ end
402
+
403
+ it "should add a max attribute to the input equal to the :greater_than_or_equal_to validation" do
404
+ concat(semantic_form_for(@new_post) do |builder|
405
+ builder.input(:title, :as => :range)
406
+ end)
407
+ output_buffer.should have_tag('input[@max="2"]')
408
+ end
409
+ end
410
+
411
+ describe "when validations require only an integer (:only_integer)" do
412
+
413
+ before do
414
+ @new_post.class.stub(:validators_on).with(:title).and_return([
415
+ active_model_numericality_validator([:title], {:allow_nil=>false, :only_integer=>true})
416
+ ])
417
+ end
418
+
419
+ it "should add a step=1 attribute to the input to signify that only whole numbers are allowed" do
420
+ concat(semantic_form_for(@new_post) do |builder|
421
+ builder.input(:title, :as => :range)
422
+ end)
423
+ output_buffer.should have_tag('input[@step="1"]')
424
+ end
425
+
426
+ it "should let input_html override :step" do
427
+ concat(semantic_form_for(@new_post) do |builder|
428
+ builder.input(:title, :as => :range, :input_html => { :step => 3 })
429
+ end)
430
+ output_buffer.should have_tag('input[@step="3"]')
431
+ end
432
+
433
+ it "should let options override :step" do
434
+ concat(semantic_form_for(@new_post) do |builder|
435
+ builder.input(:title, :as => :range, :step => 3)
436
+ end)
437
+ output_buffer.should have_tag('input[@step="3"]')
438
+ end
439
+
440
+ end
441
+
442
+ describe "when validations require a :step (non standard)" do
443
+
444
+ before do
445
+ @new_post.class.stub(:validators_on).with(:title).and_return([
446
+ active_model_numericality_validator([:title], {:allow_nil=>false, :only_integer=>true, :step=>2})
447
+ ])
448
+ end
449
+
450
+ it "should add a step=1 attribute to the input to signify that only whole numbers are allowed" do
451
+ concat(semantic_form_for(@new_post) do |builder|
452
+ builder.input(:title, :as => :range)
453
+ end)
454
+ output_buffer.should have_tag('input[@step="2"]')
455
+ end
456
+
457
+ it "should let input_html override :step" do
458
+ concat(semantic_form_for(@new_post) do |builder|
459
+ builder.input(:title, :as => :range, :input_html => { :step => 3 })
460
+ end)
461
+ output_buffer.should have_tag('input[@step="3"]')
462
+ end
463
+
464
+ it "should let options override :step" do
465
+ concat(semantic_form_for(@new_post) do |builder|
466
+ builder.input(:title, :as => :range, :step => 3)
467
+ end)
468
+ output_buffer.should have_tag('input[@step="3"]')
469
+ end
470
+
471
+ end
472
+
473
+ describe "when validations do not specify :step (non standard) or :only_integer" do
474
+
475
+ before do
476
+ @new_post.class.stub(:validators_on).with(:title).and_return([
477
+ active_model_numericality_validator([:title], {:allow_nil=>false})
478
+ ])
479
+ end
480
+
481
+ it "should default step to 1" do
482
+ concat(semantic_form_for(@new_post) do |builder|
483
+ builder.input(:title, :as => :range)
484
+ end)
485
+ output_buffer.should have_tag('input[@step="1"]')
486
+ end
487
+
488
+ it "should let input_html set :step" do
489
+ concat(semantic_form_for(@new_post) do |builder|
490
+ builder.input(:title, :as => :range, :input_html => { :step => 3 })
491
+ end)
492
+ output_buffer.should have_tag('input[@step="3"]')
493
+ end
494
+
495
+ it "should let options set :step" do
496
+ concat(semantic_form_for(@new_post) do |builder|
497
+ builder.input(:title, :as => :range, :step => 3)
498
+ end)
499
+ output_buffer.should have_tag('input[@step="3"]')
500
+ end
501
+
502
+ end
503
+
504
+ end
505
+