ShadowBelmolve-formtastic 0.2.1 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +191 -176
- data/Rakefile +65 -22
- 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_stylesheets → formtastic}/templates/formtastic.css +2 -0
- data/generators/formtastic/templates/formtastic.rb +51 -0
- data/generators/{formtastic_stylesheets → formtastic}/templates/formtastic_changes.css +0 -0
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +5 -10
- data/lib/formtastic.rb +1234 -895
- data/lib/formtastic/i18n.rb +32 -0
- data/lib/locale/en.yml +2 -2
- data/rails/init.rb +1 -1
- data/spec/buttons_spec.rb +149 -0
- data/spec/commit_button_spec.rb +344 -0
- data/spec/custom_builder_spec.rb +62 -0
- data/spec/custom_macros.rb +561 -0
- data/spec/defaults_spec.rb +20 -0
- data/spec/error_proc_spec.rb +27 -0
- data/spec/errors_spec.rb +85 -0
- data/spec/form_helper_spec.rb +120 -0
- data/spec/i18n_spec.rb +131 -0
- data/spec/include_blank_spec.rb +70 -0
- data/spec/input_spec.rb +608 -0
- data/spec/inputs/boolean_input_spec.rb +93 -0
- data/spec/inputs/check_boxes_input_spec.rb +162 -0
- data/spec/inputs/country_input_spec.rb +80 -0
- data/spec/inputs/date_input_spec.rb +45 -0
- data/spec/inputs/datetime_input_spec.rb +155 -0
- data/spec/inputs/file_input_spec.rb +33 -0
- data/spec/inputs/hidden_input_spec.rb +52 -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 +149 -0
- data/spec/inputs/select_input_spec.rb +459 -0
- data/spec/inputs/string_input_spec.rb +47 -0
- data/spec/inputs/text_input_spec.rb +33 -0
- data/spec/inputs/time_input_spec.rb +44 -0
- data/spec/inputs/time_zone_input_spec.rb +102 -0
- data/spec/inputs_spec.rb +395 -0
- data/spec/label_spec.rb +48 -0
- data/spec/nested_forms_spec.rb +50 -0
- data/spec/semantic_fields_for_spec.rb +44 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +212 -0
- metadata +121 -16
- data/lib/justin_french/formtastic.rb +0 -10
- data/spec/formtastic_spec.rb +0 -3072
- data/spec/test_helper.rb +0 -14
data/spec/input_spec.rb
ADDED
@@ -0,0 +1,608 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.dirname(__FILE__) + '/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
|
+
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
|
+
semantic_form_for(@new_post) do |builder|
|
63
|
+
concat(builder.input(:title, :required => true))
|
64
|
+
end
|
65
|
+
output_buffer.should_not have_tag('form li.optional')
|
66
|
+
output_buffer.should have_tag('form li.required')
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should append the "required" string to the label' do
|
70
|
+
semantic_form_for(@new_post) do |builder|
|
71
|
+
concat(builder.input(:title, :required => true))
|
72
|
+
end
|
73
|
+
output_buffer.should have_tag('form li.required label', /#{@new_string}$/)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe 'when false' do
|
79
|
+
|
80
|
+
before do
|
81
|
+
@string = ::Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
|
82
|
+
@new_post.class.should_not_receive(:reflect_on_all_validations)
|
83
|
+
end
|
84
|
+
|
85
|
+
after do
|
86
|
+
::Formtastic::SemanticFormBuilder.optional_string = ''
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should set an "optional" class' do
|
90
|
+
semantic_form_for(@new_post) do |builder|
|
91
|
+
concat(builder.input(:title, :required => false))
|
92
|
+
end
|
93
|
+
output_buffer.should_not have_tag('form li.required')
|
94
|
+
output_buffer.should have_tag('form li.optional')
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should append the "optional" string to the label' do
|
98
|
+
semantic_form_for(@new_post) do |builder|
|
99
|
+
concat(builder.input(:title, :required => false))
|
100
|
+
end
|
101
|
+
output_buffer.should have_tag('form li.optional label', /#{@string}$/)
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
describe 'when not provided' do
|
107
|
+
|
108
|
+
describe 'and an object was not given' do
|
109
|
+
|
110
|
+
it 'should use the default value' do
|
111
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
|
112
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
|
113
|
+
|
114
|
+
semantic_form_for(:project, :url => 'http://test.host/') do |builder|
|
115
|
+
concat(builder.input(:title))
|
116
|
+
end
|
117
|
+
output_buffer.should_not have_tag('form li.required')
|
118
|
+
output_buffer.should have_tag('form li.optional')
|
119
|
+
|
120
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'and an object was given' do
|
126
|
+
|
127
|
+
describe 'and the validation reflection plugin is available' do
|
128
|
+
|
129
|
+
before do
|
130
|
+
@new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
|
131
|
+
end
|
132
|
+
|
133
|
+
describe 'and validates_presence_of was called for the method' do
|
134
|
+
it 'should be required' do
|
135
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
|
136
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil)
|
137
|
+
])
|
138
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
|
139
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true})
|
140
|
+
])
|
141
|
+
|
142
|
+
semantic_form_for(@new_post) do |builder|
|
143
|
+
concat(builder.input(:title))
|
144
|
+
concat(builder.input(:body))
|
145
|
+
end
|
146
|
+
output_buffer.should have_tag('form li.required')
|
147
|
+
output_buffer.should_not have_tag('form li.optional')
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should be not be required if the optional :if condition is not satisifed' do
|
151
|
+
should_be_required(:required => false, :options => { :if => false })
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should not be required if the optional :if proc evaluates to false' do
|
155
|
+
should_be_required(:required => false, :options => { :if => proc { |record| false } })
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should be required if the optional :if proc evaluates to true' do
|
159
|
+
should_be_required(:required => true, :options => { :if => proc { |record| true } })
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should not be required if the optional :unless proc evaluates to true' do
|
163
|
+
should_be_required(:required => false, :options => { :unless => proc { |record| true } })
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'should be required if the optional :unless proc evaluates to false' do
|
167
|
+
should_be_required(:required => true, :options => { :unless => proc { |record| false } })
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should be required if the optional :if with a method string evaluates to true' do
|
171
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
172
|
+
should_be_required(:required => true, :options => { :if => :required_condition })
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'should be required if the optional :if with a method string evaluates to false' do
|
176
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
177
|
+
should_be_required(:required => false, :options => { :if => :required_condition })
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'should not be required if the optional :unless with a method string evaluates to false' do
|
181
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
182
|
+
should_be_required(:required => true, :options => { :unless => :required_condition })
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should be required if the optional :unless with a method string evaluates to true' do
|
186
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
187
|
+
should_be_required(:required => false, :options => { :unless => :required_condition })
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# TODO make a matcher for this?
|
192
|
+
def should_be_required(options)
|
193
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
|
194
|
+
mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options])
|
195
|
+
])
|
196
|
+
|
197
|
+
semantic_form_for(@new_post) do |builder|
|
198
|
+
concat(builder.input(:body))
|
199
|
+
end
|
200
|
+
|
201
|
+
if options[:required]
|
202
|
+
output_buffer.should_not have_tag('form li.optional')
|
203
|
+
output_buffer.should have_tag('form li.required')
|
204
|
+
else
|
205
|
+
output_buffer.should have_tag('form li.optional')
|
206
|
+
output_buffer.should_not have_tag('form li.required')
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe 'and validates_presence_of was not called for the method' do
|
211
|
+
before do
|
212
|
+
@new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([])
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should not be required' do
|
216
|
+
semantic_form_for(@new_post) do |builder|
|
217
|
+
concat(builder.input(:title))
|
218
|
+
end
|
219
|
+
output_buffer.should_not have_tag('form li.required')
|
220
|
+
output_buffer.should have_tag('form li.optional')
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
describe 'and the validation reflection plugin is not available' do
|
227
|
+
|
228
|
+
it 'should use the default value' do
|
229
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
|
230
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
|
231
|
+
|
232
|
+
semantic_form_for(@new_post) do |builder|
|
233
|
+
concat(builder.input(:title))
|
234
|
+
end
|
235
|
+
output_buffer.should_not have_tag('form li.required')
|
236
|
+
output_buffer.should have_tag('form li.optional')
|
237
|
+
|
238
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
describe ':as option' do
|
250
|
+
|
251
|
+
describe 'when not provided' do
|
252
|
+
|
253
|
+
it 'should default to a string for forms without objects unless column is password' do
|
254
|
+
semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
255
|
+
concat(builder.input(:anything))
|
256
|
+
end
|
257
|
+
output_buffer.should have_tag('form li.string')
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'should default to password for forms without objects if column is password' do
|
261
|
+
semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
262
|
+
concat(builder.input(:password))
|
263
|
+
concat(builder.input(:password_confirmation))
|
264
|
+
concat(builder.input(:confirm_password))
|
265
|
+
end
|
266
|
+
output_buffer.should have_tag('form li.password', :count => 3)
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do
|
270
|
+
@new_post.stub!(:method_without_a_database_column)
|
271
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
272
|
+
default_input_type(nil, :method_without_a_database_column).should == :string
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do
|
276
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
277
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
278
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
279
|
+
end
|
280
|
+
|
281
|
+
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
|
282
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
283
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
284
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
285
|
+
end
|
286
|
+
|
287
|
+
it 'should default to :select for column names ending in "_id"' do
|
288
|
+
default_input_type(:integer, :user_id).should == :select
|
289
|
+
default_input_type(:integer, :section_id).should == :select
|
290
|
+
end
|
291
|
+
|
292
|
+
it 'should default to :password for :string column types with "password" in the method name' do
|
293
|
+
default_input_type(:string, :password).should == :password
|
294
|
+
default_input_type(:string, :hashed_password).should == :password
|
295
|
+
default_input_type(:string, :password_hash).should == :password
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'should default to :text for :text column types' do
|
299
|
+
default_input_type(:text).should == :text
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'should default to :date for :date column types' do
|
303
|
+
default_input_type(:date).should == :date
|
304
|
+
end
|
305
|
+
|
306
|
+
it 'should default to :datetime for :datetime and :timestamp column types' do
|
307
|
+
default_input_type(:datetime).should == :datetime
|
308
|
+
default_input_type(:timestamp).should == :datetime
|
309
|
+
end
|
310
|
+
|
311
|
+
it 'should default to :time for :time column types' do
|
312
|
+
default_input_type(:time).should == :time
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'should default to :boolean for :boolean column types' do
|
316
|
+
default_input_type(:boolean).should == :boolean
|
317
|
+
end
|
318
|
+
|
319
|
+
it 'should default to :string for :string column types' do
|
320
|
+
default_input_type(:string).should == :string
|
321
|
+
end
|
322
|
+
|
323
|
+
it 'should default to :numeric for :integer, :float and :decimal column types' do
|
324
|
+
default_input_type(:integer).should == :numeric
|
325
|
+
default_input_type(:float).should == :numeric
|
326
|
+
default_input_type(:decimal).should == :numeric
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'should default to :country for :string columns named country' do
|
330
|
+
default_input_type(:string, :country).should == :country
|
331
|
+
end
|
332
|
+
|
333
|
+
describe 'defaulting to file column' do
|
334
|
+
::Formtastic::SemanticFormBuilder.file_methods.each do |method|
|
335
|
+
it "should default to :file for attributes that respond to ##{method}" do
|
336
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
337
|
+
column = mock('column')
|
338
|
+
|
339
|
+
::Formtastic::SemanticFormBuilder.file_methods.each do |test|
|
340
|
+
column.stub!(:respond_to?).with(test).and_return(method == test)
|
341
|
+
end
|
342
|
+
|
343
|
+
@new_post.should_receive(method).and_return(column)
|
344
|
+
|
345
|
+
semantic_form_for(@new_post) do |builder|
|
346
|
+
builder.send(:default_input_type, method).should == :file
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
it 'should call the corresponding input method' do
|
355
|
+
[:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style|
|
356
|
+
@new_post.stub!(:generic_column_name)
|
357
|
+
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
|
358
|
+
semantic_form_for(@new_post) do |builder|
|
359
|
+
builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input")
|
360
|
+
concat(builder.input(:generic_column_name, :as => input_style))
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
[:string, :password, :numeric, :text, :file].each do |input_style|
|
365
|
+
@new_post.stub!(:generic_column_name)
|
366
|
+
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
|
367
|
+
semantic_form_for(@new_post) do |builder|
|
368
|
+
builder.should_receive(:basic_input_helper).once.and_return("fake HTML output from #input")
|
369
|
+
concat(builder.input(:generic_column_name, :as => input_style))
|
370
|
+
end
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
|
376
|
+
describe ':label option' do
|
377
|
+
|
378
|
+
describe 'when provided' do
|
379
|
+
it 'should be passed down to the label tag' do
|
380
|
+
semantic_form_for(@new_post) do |builder|
|
381
|
+
concat(builder.input(:title, :label => "Kustom"))
|
382
|
+
end
|
383
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
384
|
+
end
|
385
|
+
|
386
|
+
it 'should not generate a label if false' do
|
387
|
+
semantic_form_for(@new_post) do |builder|
|
388
|
+
concat(builder.input(:title, :label => false))
|
389
|
+
end
|
390
|
+
output_buffer.should_not have_tag("form li label")
|
391
|
+
end
|
392
|
+
|
393
|
+
it 'should be dupped if frozen' do
|
394
|
+
semantic_form_for(@new_post) do |builder|
|
395
|
+
concat(builder.input(:title, :label => "Kustom".freeze))
|
396
|
+
end
|
397
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
describe 'when not provided' do
|
402
|
+
describe 'when localized label is NOT provided' do
|
403
|
+
describe 'and object is not given' do
|
404
|
+
it 'should default the humanized method name, passing it down to the label tag' do
|
405
|
+
::Formtastic::SemanticFormBuilder.label_str_method = :humanize
|
406
|
+
|
407
|
+
semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
408
|
+
concat(builder.input(:meta_description))
|
409
|
+
end
|
410
|
+
|
411
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
describe 'and object is given' do
|
416
|
+
it 'should delegate the label logic to class human attribute name and pass it down to the label tag' do
|
417
|
+
@new_post.stub!(:meta_description) # a two word method name
|
418
|
+
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
|
419
|
+
|
420
|
+
semantic_form_for(@new_post) do |builder|
|
421
|
+
concat(builder.input(:meta_description))
|
422
|
+
end
|
423
|
+
|
424
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
describe 'and object is given with label_str_method set to :capitalize' do
|
429
|
+
it 'should capitalize method name, passing it down to the label tag' do
|
430
|
+
with_config :label_str_method, :capitalize do
|
431
|
+
@new_post.stub!(:meta_description)
|
432
|
+
|
433
|
+
semantic_form_for(@new_post) do |builder|
|
434
|
+
concat(builder.input(:meta_description))
|
435
|
+
end
|
436
|
+
|
437
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
end
|
442
|
+
|
443
|
+
describe 'when localized label is provided' do
|
444
|
+
before do
|
445
|
+
@localized_label_text = 'Localized title'
|
446
|
+
@default_localized_label_text = 'Default localized title'
|
447
|
+
::I18n.backend.store_translations :en,
|
448
|
+
:formtastic => {
|
449
|
+
:labels => {
|
450
|
+
:title => @default_localized_label_text,
|
451
|
+
:published => @default_localized_label_text,
|
452
|
+
:post => {
|
453
|
+
:title => @localized_label_text,
|
454
|
+
:published => @default_localized_label_text
|
455
|
+
}
|
456
|
+
}
|
457
|
+
}
|
458
|
+
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
459
|
+
end
|
460
|
+
|
461
|
+
it 'should render a label with localized label (I18n)' do
|
462
|
+
semantic_form_for(@new_post) do |builder|
|
463
|
+
concat(builder.input(:title, :label => true))
|
464
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
465
|
+
end
|
466
|
+
output_buffer.should have_tag('form li label', @localized_label_text)
|
467
|
+
end
|
468
|
+
|
469
|
+
it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
|
470
|
+
::I18n.backend.store_translations :en,
|
471
|
+
:formtastic => {
|
472
|
+
:labels => {
|
473
|
+
:post => {
|
474
|
+
:title => nil,
|
475
|
+
:published => nil
|
476
|
+
}
|
477
|
+
}
|
478
|
+
}
|
479
|
+
semantic_form_for(@new_post) do |builder|
|
480
|
+
concat(builder.input(:title, :label => true))
|
481
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
482
|
+
end
|
483
|
+
output_buffer.should have_tag('form li label', @default_localized_label_text)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
end
|
489
|
+
|
490
|
+
describe ':hint option' do
|
491
|
+
|
492
|
+
describe 'when provided' do
|
493
|
+
it 'should be passed down to the paragraph tag' do
|
494
|
+
hint_text = "this is the title of the post"
|
495
|
+
semantic_form_for(@new_post) do |builder|
|
496
|
+
concat(builder.input(:title, :hint => hint_text))
|
497
|
+
end
|
498
|
+
output_buffer.should have_tag("form li p.inline-hints", hint_text)
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
describe 'when not provided' do
|
503
|
+
describe 'when localized hint (I18n) is provided' do
|
504
|
+
before do
|
505
|
+
@localized_hint_text = "This is the localized hint."
|
506
|
+
@default_localized_hint_text = "This is the default localized hint."
|
507
|
+
::I18n.backend.store_translations :en,
|
508
|
+
:formtastic => {
|
509
|
+
:hints => {
|
510
|
+
:title => @default_localized_hint_text,
|
511
|
+
:post => {
|
512
|
+
:title => @localized_hint_text
|
513
|
+
}
|
514
|
+
}
|
515
|
+
}
|
516
|
+
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
517
|
+
end
|
518
|
+
|
519
|
+
describe 'when provided value (hint value) is set to TRUE' do
|
520
|
+
it 'should render a hint paragraph containing a localized hint (I18n)' do
|
521
|
+
semantic_form_for(@new_post) do |builder|
|
522
|
+
concat(builder.input(:title, :hint => true))
|
523
|
+
end
|
524
|
+
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
|
525
|
+
end
|
526
|
+
|
527
|
+
it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
|
528
|
+
::I18n.backend.store_translations :en,
|
529
|
+
:formtastic => {
|
530
|
+
:hints => {
|
531
|
+
:post => {
|
532
|
+
:title => nil
|
533
|
+
}
|
534
|
+
}
|
535
|
+
}
|
536
|
+
semantic_form_for(@new_post) do |builder|
|
537
|
+
concat(builder.input(:title, :hint => true))
|
538
|
+
end
|
539
|
+
output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
|
540
|
+
end
|
541
|
+
end
|
542
|
+
|
543
|
+
describe 'when provided value (label value) is set to FALSE' do
|
544
|
+
it 'should not render a hint paragraph' do
|
545
|
+
semantic_form_for(@new_post) do |builder|
|
546
|
+
concat(builder.input(:title, :hint => false))
|
547
|
+
end
|
548
|
+
output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
|
549
|
+
end
|
550
|
+
end
|
551
|
+
end
|
552
|
+
|
553
|
+
describe 'when localized hint (I18n) is not provided' do
|
554
|
+
it 'should not render a hint paragraph' do
|
555
|
+
semantic_form_for(@new_post) do |builder|
|
556
|
+
concat(builder.input(:title))
|
557
|
+
end
|
558
|
+
output_buffer.should_not have_tag('form li p.inline-hints')
|
559
|
+
end
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
end
|
564
|
+
|
565
|
+
describe ':wrapper_html option' do
|
566
|
+
|
567
|
+
describe 'when provided' do
|
568
|
+
it 'should be passed down to the li tag' do
|
569
|
+
semantic_form_for(@new_post) do |builder|
|
570
|
+
concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
|
571
|
+
end
|
572
|
+
output_buffer.should have_tag("form li#another_id")
|
573
|
+
end
|
574
|
+
|
575
|
+
it 'should append given classes to li default classes' do
|
576
|
+
semantic_form_for(@new_post) do |builder|
|
577
|
+
concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
|
578
|
+
end
|
579
|
+
output_buffer.should have_tag("form li.string")
|
580
|
+
output_buffer.should have_tag("form li.required")
|
581
|
+
output_buffer.should have_tag("form li.another_class")
|
582
|
+
end
|
583
|
+
|
584
|
+
it 'should allow classes to be an array' do
|
585
|
+
semantic_form_for(@new_post) do |builder|
|
586
|
+
concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
|
587
|
+
end
|
588
|
+
output_buffer.should have_tag("form li.string")
|
589
|
+
output_buffer.should have_tag("form li.my_class")
|
590
|
+
output_buffer.should have_tag("form li.another_class")
|
591
|
+
end
|
592
|
+
end
|
593
|
+
|
594
|
+
describe 'when not provided' do
|
595
|
+
it 'should use default id and class' do
|
596
|
+
semantic_form_for(@new_post) do |builder|
|
597
|
+
concat(builder.input(:title))
|
598
|
+
end
|
599
|
+
output_buffer.should have_tag("form li#post_title_input")
|
600
|
+
output_buffer.should have_tag("form li.string")
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
end
|
605
|
+
end
|
606
|
+
|
607
|
+
end
|
608
|
+
|