techthumb-formtastic 1.rails3.sha
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +584 -0
- data/Rakefile +127 -0
- data/generators/form/USAGE +16 -0
- data/generators/form/form_generator.rb +120 -0
- data/generators/form/templates/view__form.html.erb +5 -0
- data/generators/form/templates/view__form.html.haml +4 -0
- data/generators/formtastic/formtastic_generator.rb +24 -0
- data/generators/formtastic/templates/formtastic.css +131 -0
- data/generators/formtastic/templates/formtastic.rb +54 -0
- data/generators/formtastic/templates/formtastic_changes.css +14 -0
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
- data/init.rb +5 -0
- data/lib/formtastic/i18n.rb +35 -0
- data/lib/formtastic/layout_helper.rb +10 -0
- data/lib/formtastic/railtie.rb +12 -0
- data/lib/formtastic/util.rb +36 -0
- data/lib/formtastic.rb +1870 -0
- data/lib/generators/formtastic/form/form_generator.rb +86 -0
- data/lib/generators/formtastic/install/install_generator.rb +24 -0
- data/lib/locale/en.yml +8 -0
- data/rails/init.rb +2 -0
- data/spec/buttons_spec.rb +166 -0
- data/spec/commit_button_spec.rb +401 -0
- data/spec/custom_builder_spec.rb +98 -0
- data/spec/defaults_spec.rb +20 -0
- data/spec/error_proc_spec.rb +27 -0
- data/spec/errors_spec.rb +105 -0
- data/spec/form_helper_spec.rb +142 -0
- data/spec/helpers/layout_helper_spec.rb +21 -0
- data/spec/i18n_spec.rb +152 -0
- data/spec/include_blank_spec.rb +74 -0
- data/spec/input_spec.rb +786 -0
- data/spec/inputs/boolean_input_spec.rb +104 -0
- data/spec/inputs/check_boxes_input_spec.rb +426 -0
- data/spec/inputs/country_input_spec.rb +118 -0
- data/spec/inputs/date_input_spec.rb +168 -0
- data/spec/inputs/datetime_input_spec.rb +310 -0
- data/spec/inputs/file_input_spec.rb +34 -0
- data/spec/inputs/hidden_input_spec.rb +78 -0
- data/spec/inputs/numeric_input_spec.rb +44 -0
- data/spec/inputs/password_input_spec.rb +46 -0
- data/spec/inputs/radio_input_spec.rb +243 -0
- data/spec/inputs/select_input_spec.rb +546 -0
- data/spec/inputs/string_input_spec.rb +64 -0
- data/spec/inputs/text_input_spec.rb +34 -0
- data/spec/inputs/time_input_spec.rb +206 -0
- data/spec/inputs/time_zone_input_spec.rb +110 -0
- data/spec/inputs_spec.rb +476 -0
- data/spec/label_spec.rb +89 -0
- data/spec/semantic_errors_spec.rb +98 -0
- data/spec/semantic_fields_for_spec.rb +45 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +289 -0
- data/spec/support/custom_macros.rb +494 -0
- data/spec/support/output_buffer.rb +4 -0
- data/spec/support/test_environment.rb +45 -0
- metadata +235 -0
data/spec/input_spec.rb
ADDED
@@ -0,0 +1,786 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'SemanticFormBuilder#input' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'with inline order customization' do
|
14
|
+
it 'should allow input, hints, errors as order' do
|
15
|
+
::Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
|
16
|
+
|
17
|
+
semantic_form_for(@new_post) do |builder|
|
18
|
+
builder.should_receive(:inline_input_for).once.ordered
|
19
|
+
builder.should_receive(:inline_hints_for).once.ordered
|
20
|
+
builder.should_receive(:inline_errors_for).once.ordered
|
21
|
+
concat(builder.input(:title))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should allow hints, input, errors as order' do
|
26
|
+
::Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
|
27
|
+
|
28
|
+
semantic_form_for(@new_post) do |builder|
|
29
|
+
builder.should_receive(:inline_hints_for).once.ordered
|
30
|
+
builder.should_receive(:inline_input_for).once.ordered
|
31
|
+
builder.should_receive(:inline_errors_for).once.ordered
|
32
|
+
concat(builder.input(:title))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'arguments and options' do
|
38
|
+
|
39
|
+
it 'should require the first argument (the method on form\'s object)' do
|
40
|
+
lambda {
|
41
|
+
@form = semantic_form_for(@new_post) do |builder|
|
42
|
+
concat(builder.input()) # no args passed in at all
|
43
|
+
end
|
44
|
+
}.should raise_error(ArgumentError)
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ':required option' do
|
48
|
+
|
49
|
+
describe 'when true' do
|
50
|
+
|
51
|
+
before do
|
52
|
+
@old_string = ::Formtastic::SemanticFormBuilder.required_string
|
53
|
+
@new_string = ::Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
|
54
|
+
@new_post.class.should_not_receive(:reflect_on_all_validations)
|
55
|
+
end
|
56
|
+
|
57
|
+
after do
|
58
|
+
::Formtastic::SemanticFormBuilder.required_string = @old_string
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should set a "required" class' do
|
62
|
+
form = semantic_form_for(@new_post) do |builder|
|
63
|
+
concat(builder.input(:title, :required => true))
|
64
|
+
end
|
65
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
66
|
+
output_buffer.should_not have_tag('form li.optional')
|
67
|
+
output_buffer.should have_tag('form li.required')
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should append the "required" string to the label' do
|
71
|
+
form = semantic_form_for(@new_post) do |builder|
|
72
|
+
concat(builder.input(:title, :required => true))
|
73
|
+
end
|
74
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
75
|
+
output_buffer.should have_tag('form li.required label', /#{@new_string}$/)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'when false' do
|
81
|
+
|
82
|
+
before do
|
83
|
+
@string = ::Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
|
84
|
+
@new_post.class.should_not_receive(:reflect_on_all_validations)
|
85
|
+
end
|
86
|
+
|
87
|
+
after do
|
88
|
+
::Formtastic::SemanticFormBuilder.optional_string = ''
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should set an "optional" class' do
|
92
|
+
form = semantic_form_for(@new_post) do |builder|
|
93
|
+
concat(builder.input(:title, :required => false))
|
94
|
+
end
|
95
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
96
|
+
output_buffer.should_not have_tag('form li.required')
|
97
|
+
output_buffer.should have_tag('form li.optional')
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should append the "optional" string to the label' do
|
101
|
+
form = semantic_form_for(@new_post) do |builder|
|
102
|
+
concat(builder.input(:title, :required => false))
|
103
|
+
end
|
104
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
105
|
+
output_buffer.should have_tag('form li.optional label', /#{@string}$/)
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'when not provided' do
|
111
|
+
|
112
|
+
describe 'and an object was not given' do
|
113
|
+
|
114
|
+
it 'should use the default value' do
|
115
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
|
116
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
|
117
|
+
|
118
|
+
form = semantic_form_for(:project, :url => 'http://test.host/') do |builder|
|
119
|
+
concat(builder.input(:title))
|
120
|
+
end
|
121
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
122
|
+
output_buffer.should_not have_tag('form li.required')
|
123
|
+
output_buffer.should have_tag('form li.optional')
|
124
|
+
|
125
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'and an object was given' do
|
131
|
+
|
132
|
+
describe 'and the validation reflection plugin is available' do
|
133
|
+
|
134
|
+
before do
|
135
|
+
::Post.stub!(:reflect_on_validations_for).and_return([])
|
136
|
+
@new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
|
137
|
+
end
|
138
|
+
|
139
|
+
describe 'and validates_presence_of was called for the method' do
|
140
|
+
it 'should be required' do
|
141
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
|
142
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil)
|
143
|
+
])
|
144
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
|
145
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true})
|
146
|
+
])
|
147
|
+
|
148
|
+
form = semantic_form_for(@new_post) do |builder|
|
149
|
+
concat(builder.input(:title))
|
150
|
+
concat(builder.input(:body))
|
151
|
+
end
|
152
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
153
|
+
output_buffer.should have_tag('form li.required')
|
154
|
+
output_buffer.should_not have_tag('form li.optional')
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should be not be required if the optional :if condition is not satisifed' do
|
158
|
+
should_be_required(:required => false, :options => { :if => false })
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should not be required if the optional :if proc evaluates to false' do
|
162
|
+
should_be_required(:required => false, :options => { :if => proc { |record| false } })
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should be required if the optional :if proc evaluates to true' do
|
166
|
+
should_be_required(:required => true, :options => { :if => proc { |record| true } })
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should not be required if the optional :unless proc evaluates to true' do
|
170
|
+
should_be_required(:required => false, :options => { :unless => proc { |record| true } })
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'should be required if the optional :unless proc evaluates to false' do
|
174
|
+
should_be_required(:required => true, :options => { :unless => proc { |record| false } })
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should be required if the optional :if with a method string evaluates to true' do
|
178
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
179
|
+
should_be_required(:required => true, :options => { :if => :required_condition })
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'should be required if the optional :if with a method string evaluates to false' do
|
183
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
184
|
+
should_be_required(:required => false, :options => { :if => :required_condition })
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'should not be required if the optional :unless with a method string evaluates to false' do
|
188
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
189
|
+
should_be_required(:required => true, :options => { :unless => :required_condition })
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should be required if the optional :unless with a method string evaluates to true' do
|
193
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
194
|
+
should_be_required(:required => false, :options => { :unless => :required_condition })
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# TODO make a matcher for this?
|
199
|
+
def should_be_required(options)
|
200
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
|
201
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options])
|
202
|
+
])
|
203
|
+
|
204
|
+
form = semantic_form_for(@new_post) do |builder|
|
205
|
+
concat(builder.input(:body))
|
206
|
+
end
|
207
|
+
|
208
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
209
|
+
|
210
|
+
if options[:required]
|
211
|
+
output_buffer.should_not have_tag('form li.optional')
|
212
|
+
output_buffer.should have_tag('form li.required')
|
213
|
+
else
|
214
|
+
output_buffer.should have_tag('form li.optional')
|
215
|
+
output_buffer.should_not have_tag('form li.required')
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe 'and validates_presence_of was not called for the method' do
|
220
|
+
before do
|
221
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([])
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'should not be required' do
|
225
|
+
form = semantic_form_for(@new_post) do |builder|
|
226
|
+
concat(builder.input(:title))
|
227
|
+
end
|
228
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
229
|
+
output_buffer.should_not have_tag('form li.required')
|
230
|
+
output_buffer.should have_tag('form li.optional')
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
describe 'and its a ActiveModel' do
|
237
|
+
before do
|
238
|
+
@new_post.stub!(:class).and_return(::PostModel)
|
239
|
+
end
|
240
|
+
|
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
|
246
|
+
|
247
|
+
@new_post.class.should_receive(:validators_on).with(:title).and_return([
|
248
|
+
active_model_presence_validator([:title])
|
249
|
+
])
|
250
|
+
|
251
|
+
@new_post.class.should_receive(:validators_on).with(:body).and_return([
|
252
|
+
active_model_presence_validator([:body], {:if => true})
|
253
|
+
])
|
254
|
+
|
255
|
+
form = semantic_form_for(@new_post) do |builder|
|
256
|
+
concat(builder.input(:title))
|
257
|
+
concat(builder.input(:body))
|
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
|
263
|
+
|
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
|
267
|
+
|
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
|
271
|
+
|
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
|
275
|
+
|
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
|
279
|
+
|
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
|
283
|
+
|
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
|
288
|
+
|
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
|
293
|
+
|
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 })
|
297
|
+
end
|
298
|
+
|
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
|
304
|
+
|
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
|
+
])
|
310
|
+
|
311
|
+
form = semantic_form_for(@new_post) do |builder|
|
312
|
+
concat(builder.input(:body))
|
313
|
+
end
|
314
|
+
|
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')
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
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
|
330
|
+
|
331
|
+
it 'should not be required' do
|
332
|
+
form = semantic_form_for(@new_post) do |builder|
|
333
|
+
concat(builder.input(:title))
|
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')
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
end
|
342
|
+
|
343
|
+
describe 'and the validation reflection plugin is not available' do
|
344
|
+
|
345
|
+
it 'should use the default value' do
|
346
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
|
347
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
|
348
|
+
|
349
|
+
form = semantic_form_for(@new_post) do |builder|
|
350
|
+
concat(builder.input(:title))
|
351
|
+
end
|
352
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
353
|
+
output_buffer.should_not have_tag('form li.required')
|
354
|
+
output_buffer.should have_tag('form li.optional')
|
355
|
+
|
356
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
|
357
|
+
end
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
end
|
366
|
+
|
367
|
+
describe ':as option' do
|
368
|
+
|
369
|
+
describe 'when not provided' do
|
370
|
+
|
371
|
+
it 'should default to a string for forms without objects unless column is password' do
|
372
|
+
form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
373
|
+
concat(builder.input(:anything))
|
374
|
+
end
|
375
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
376
|
+
output_buffer.should have_tag('form li.string')
|
377
|
+
end
|
378
|
+
|
379
|
+
it 'should default to password for forms without objects if column is password' do
|
380
|
+
form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
381
|
+
concat(builder.input(:password))
|
382
|
+
concat(builder.input(:password_confirmation))
|
383
|
+
concat(builder.input(:confirm_password))
|
384
|
+
end
|
385
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
386
|
+
output_buffer.should have_tag('form li.password', :count => 3)
|
387
|
+
end
|
388
|
+
|
389
|
+
it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do
|
390
|
+
@new_post.stub!(:method_without_a_database_column)
|
391
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
392
|
+
default_input_type(nil, :method_without_a_database_column).should == :string
|
393
|
+
end
|
394
|
+
|
395
|
+
it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do
|
396
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
397
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
398
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
399
|
+
end
|
400
|
+
|
401
|
+
it 'should default to :password for methods on objects that don\'t respond to "column_for_attribute" but "password" is in the method name' do
|
402
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
403
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
404
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
405
|
+
end
|
406
|
+
|
407
|
+
it 'should default to :select for column names ending in "_id"' do
|
408
|
+
default_input_type(:integer, :user_id).should == :select
|
409
|
+
default_input_type(:integer, :section_id).should == :select
|
410
|
+
end
|
411
|
+
|
412
|
+
it 'should default to :password for :string column types with "password" in the method name' do
|
413
|
+
default_input_type(:string, :password).should == :password
|
414
|
+
default_input_type(:string, :hashed_password).should == :password
|
415
|
+
default_input_type(:string, :password_hash).should == :password
|
416
|
+
end
|
417
|
+
|
418
|
+
it 'should default to :text for :text column types' do
|
419
|
+
default_input_type(:text).should == :text
|
420
|
+
end
|
421
|
+
|
422
|
+
it 'should default to :date for :date column types' do
|
423
|
+
default_input_type(:date).should == :date
|
424
|
+
end
|
425
|
+
|
426
|
+
it 'should default to :datetime for :datetime and :timestamp column types' do
|
427
|
+
default_input_type(:datetime).should == :datetime
|
428
|
+
default_input_type(:timestamp).should == :datetime
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'should default to :time for :time column types' do
|
432
|
+
default_input_type(:time).should == :time
|
433
|
+
end
|
434
|
+
|
435
|
+
it 'should default to :boolean for :boolean column types' do
|
436
|
+
default_input_type(:boolean).should == :boolean
|
437
|
+
end
|
438
|
+
|
439
|
+
it 'should default to :string for :string column types' do
|
440
|
+
default_input_type(:string).should == :string
|
441
|
+
end
|
442
|
+
|
443
|
+
it 'should default to :numeric for :integer, :float and :decimal column types' do
|
444
|
+
default_input_type(:integer).should == :numeric
|
445
|
+
default_input_type(:float).should == :numeric
|
446
|
+
default_input_type(:decimal).should == :numeric
|
447
|
+
end
|
448
|
+
|
449
|
+
it 'should default to :country for :string columns named country' do
|
450
|
+
default_input_type(:string, :country).should == :country
|
451
|
+
end
|
452
|
+
|
453
|
+
describe 'defaulting to file column' do
|
454
|
+
::Formtastic::SemanticFormBuilder.file_methods.each do |method|
|
455
|
+
it "should default to :file for attributes that respond to ##{method}" do
|
456
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
457
|
+
column = mock('column')
|
458
|
+
|
459
|
+
::Formtastic::SemanticFormBuilder.file_methods.each do |test|
|
460
|
+
### TODO: Check if this is ok
|
461
|
+
column.stub!(method).with(test).and_return(method == test)
|
462
|
+
end
|
463
|
+
|
464
|
+
@new_post.should_receive(method).and_return(column)
|
465
|
+
|
466
|
+
semantic_form_for(@new_post) do |builder|
|
467
|
+
builder.send(:default_input_type, method).should == :file
|
468
|
+
end
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
it 'should call the corresponding input method' do
|
476
|
+
[:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style|
|
477
|
+
@new_post.stub!(:generic_column_name)
|
478
|
+
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
|
479
|
+
semantic_form_for(@new_post) do |builder|
|
480
|
+
builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input")
|
481
|
+
concat(builder.input(:generic_column_name, :as => input_style))
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
[:string, :password, :numeric, :text, :file].each do |input_style|
|
486
|
+
@new_post.stub!(:generic_column_name)
|
487
|
+
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
|
488
|
+
semantic_form_for(@new_post) do |builder|
|
489
|
+
builder.should_receive(:basic_input_helper).once.and_return("fake HTML output from #input")
|
490
|
+
concat(builder.input(:generic_column_name, :as => input_style))
|
491
|
+
end
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
end
|
496
|
+
|
497
|
+
describe ':label option' do
|
498
|
+
|
499
|
+
describe 'when provided' do
|
500
|
+
it 'should be passed down to the label tag' do
|
501
|
+
form = semantic_form_for(@new_post) do |builder|
|
502
|
+
concat(builder.input(:title, :label => "Kustom"))
|
503
|
+
end
|
504
|
+
|
505
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
506
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
507
|
+
end
|
508
|
+
|
509
|
+
it 'should not generate a label if false' do
|
510
|
+
form = semantic_form_for(@new_post) do |builder|
|
511
|
+
concat(builder.input(:title, :label => false))
|
512
|
+
end
|
513
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
514
|
+
output_buffer.should_not have_tag("form li label")
|
515
|
+
end
|
516
|
+
|
517
|
+
it 'should be dupped if frozen' do
|
518
|
+
form = semantic_form_for(@new_post) do |builder|
|
519
|
+
concat(builder.input(:title, :label => "Kustom".freeze))
|
520
|
+
end
|
521
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
522
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
describe 'when not provided' do
|
527
|
+
describe 'when localized label is provided' do
|
528
|
+
describe 'and object is given' do
|
529
|
+
describe 'and label_str_method not default' do
|
530
|
+
it 'should render a label with localized label (I18n)' do
|
531
|
+
with_config :label_str_method, :capitalize do
|
532
|
+
@localized_label_text = 'Localized title'
|
533
|
+
@new_post.stub!(:meta_description)
|
534
|
+
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text)
|
535
|
+
|
536
|
+
form = semantic_form_for(@new_post) do |builder|
|
537
|
+
concat(builder.input(:meta_description))
|
538
|
+
end
|
539
|
+
|
540
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
541
|
+
output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
|
542
|
+
end
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
describe 'when localized label is NOT provided' do
|
549
|
+
describe 'and object is not given' do
|
550
|
+
it 'should default the humanized method name, passing it down to the label tag' do
|
551
|
+
::Formtastic::SemanticFormBuilder.label_str_method = :humanize
|
552
|
+
|
553
|
+
form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
554
|
+
concat(builder.input(:meta_description))
|
555
|
+
end
|
556
|
+
|
557
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
558
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
559
|
+
end
|
560
|
+
end
|
561
|
+
|
562
|
+
describe 'and object is given' do
|
563
|
+
it 'should delegate the label logic to class human attribute name and pass it down to the label tag' do
|
564
|
+
@new_post.stub!(:meta_description) # a two word method name
|
565
|
+
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
|
566
|
+
|
567
|
+
form = semantic_form_for(@new_post) do |builder|
|
568
|
+
concat(builder.input(:meta_description))
|
569
|
+
end
|
570
|
+
|
571
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
572
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
describe 'and object is given with label_str_method set to :capitalize' do
|
577
|
+
it 'should capitalize method name, passing it down to the label tag' do
|
578
|
+
with_config :label_str_method, :capitalize do
|
579
|
+
@new_post.stub!(:meta_description)
|
580
|
+
|
581
|
+
form = semantic_form_for(@new_post) do |builder|
|
582
|
+
concat(builder.input(:meta_description))
|
583
|
+
end
|
584
|
+
|
585
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
586
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
|
587
|
+
end
|
588
|
+
end
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
describe 'when localized label is provided' do
|
593
|
+
before do
|
594
|
+
@localized_label_text = 'Localized title'
|
595
|
+
@default_localized_label_text = 'Default localized title'
|
596
|
+
::I18n.backend.store_translations :en,
|
597
|
+
:formtastic => {
|
598
|
+
:labels => {
|
599
|
+
:title => @default_localized_label_text,
|
600
|
+
:published => @default_localized_label_text,
|
601
|
+
:post => {
|
602
|
+
:title => @localized_label_text,
|
603
|
+
:published => @default_localized_label_text
|
604
|
+
}
|
605
|
+
}
|
606
|
+
}
|
607
|
+
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
608
|
+
end
|
609
|
+
|
610
|
+
it 'should render a label with localized label (I18n)' do
|
611
|
+
form = semantic_form_for(@new_post) do |builder|
|
612
|
+
concat(builder.input(:title, :label => true))
|
613
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
614
|
+
end
|
615
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
616
|
+
output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
|
617
|
+
end
|
618
|
+
|
619
|
+
it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
|
620
|
+
::I18n.backend.store_translations :en,
|
621
|
+
:formtastic => {
|
622
|
+
:labels => {
|
623
|
+
:post => {
|
624
|
+
:title => nil,
|
625
|
+
:published => nil
|
626
|
+
}
|
627
|
+
}
|
628
|
+
}
|
629
|
+
form = semantic_form_for(@new_post) do |builder|
|
630
|
+
concat(builder.input(:title, :label => true))
|
631
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
632
|
+
end
|
633
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
634
|
+
output_buffer.should have_tag('form li label', Regexp.new('^' + @default_localized_label_text))
|
635
|
+
end
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
end
|
640
|
+
|
641
|
+
describe ':hint option' do
|
642
|
+
|
643
|
+
describe 'when provided' do
|
644
|
+
it 'should be passed down to the paragraph tag' do
|
645
|
+
hint_text = "this is the title of the post"
|
646
|
+
form = semantic_form_for(@new_post) do |builder|
|
647
|
+
concat(builder.input(:title, :hint => hint_text))
|
648
|
+
end
|
649
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
650
|
+
output_buffer.should have_tag("form li p.inline-hints", hint_text)
|
651
|
+
end
|
652
|
+
end
|
653
|
+
|
654
|
+
describe 'when not provided' do
|
655
|
+
describe 'when localized hint (I18n) is provided' do
|
656
|
+
before do
|
657
|
+
@localized_hint_text = "This is the localized hint."
|
658
|
+
@default_localized_hint_text = "This is the default localized hint."
|
659
|
+
::I18n.backend.store_translations :en,
|
660
|
+
:formtastic => {
|
661
|
+
:hints => {
|
662
|
+
:title => @default_localized_hint_text,
|
663
|
+
}
|
664
|
+
}
|
665
|
+
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
666
|
+
end
|
667
|
+
|
668
|
+
after do
|
669
|
+
::I18n.backend.reload!
|
670
|
+
end
|
671
|
+
|
672
|
+
describe 'when provided value (hint value) is set to TRUE' do
|
673
|
+
it 'should render a hint paragraph containing a localized hint (I18n)' do
|
674
|
+
::I18n.backend.store_translations :en,
|
675
|
+
:formtastic => {
|
676
|
+
:hints => {
|
677
|
+
:post => {
|
678
|
+
:title => @localized_hint_text
|
679
|
+
}
|
680
|
+
}
|
681
|
+
}
|
682
|
+
form = semantic_form_for(@new_post) do |builder|
|
683
|
+
concat(builder.input(:title, :hint => true))
|
684
|
+
end
|
685
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
686
|
+
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
|
687
|
+
end
|
688
|
+
|
689
|
+
it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
|
690
|
+
form = semantic_form_for(@new_post) do |builder|
|
691
|
+
concat(builder.input(:title, :hint => true))
|
692
|
+
end
|
693
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
694
|
+
output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
describe 'when provided value (label value) is set to FALSE' do
|
699
|
+
it 'should not render a hint paragraph' do
|
700
|
+
form = semantic_form_for(@new_post) do |builder|
|
701
|
+
concat(builder.input(:title, :hint => false))
|
702
|
+
end
|
703
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
704
|
+
output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|
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
|
+
|
726
|
+
describe 'when localized hint (I18n) is not provided' do
|
727
|
+
it 'should not render a hint paragraph' do
|
728
|
+
form = semantic_form_for(@new_post) do |builder|
|
729
|
+
concat(builder.input(:title))
|
730
|
+
end
|
731
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
732
|
+
output_buffer.should_not have_tag('form li p.inline-hints')
|
733
|
+
end
|
734
|
+
end
|
735
|
+
end
|
736
|
+
|
737
|
+
end
|
738
|
+
|
739
|
+
describe ':wrapper_html option' do
|
740
|
+
|
741
|
+
describe 'when provided' do
|
742
|
+
it 'should be passed down to the li tag' do
|
743
|
+
form = semantic_form_for(@new_post) do |builder|
|
744
|
+
concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
|
745
|
+
end
|
746
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
747
|
+
output_buffer.should have_tag("form li#another_id")
|
748
|
+
end
|
749
|
+
|
750
|
+
it 'should append given classes to li default classes' do
|
751
|
+
form = semantic_form_for(@new_post) do |builder|
|
752
|
+
concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
|
753
|
+
end
|
754
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
755
|
+
output_buffer.should have_tag("form li.string")
|
756
|
+
output_buffer.should have_tag("form li.required")
|
757
|
+
output_buffer.should have_tag("form li.another_class")
|
758
|
+
end
|
759
|
+
|
760
|
+
it 'should allow classes to be an array' do
|
761
|
+
form = semantic_form_for(@new_post) do |builder|
|
762
|
+
concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
|
763
|
+
end
|
764
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
765
|
+
output_buffer.should have_tag("form li.string")
|
766
|
+
output_buffer.should have_tag("form li.my_class")
|
767
|
+
output_buffer.should have_tag("form li.another_class")
|
768
|
+
end
|
769
|
+
end
|
770
|
+
|
771
|
+
describe 'when not provided' do
|
772
|
+
it 'should use default id and class' do
|
773
|
+
form = semantic_form_for(@new_post) do |builder|
|
774
|
+
concat(builder.input(:title))
|
775
|
+
end
|
776
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
777
|
+
output_buffer.should have_tag("form li#post_title_input")
|
778
|
+
output_buffer.should have_tag("form li.string")
|
779
|
+
end
|
780
|
+
end
|
781
|
+
|
782
|
+
end
|
783
|
+
end
|
784
|
+
|
785
|
+
end
|
786
|
+
|