formtastic 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ module Formtastic
2
+ module LayoutHelper
3
+
4
+ def formtastic_stylesheet_link_tag
5
+ stylesheet_link_tag("formtastic") +
6
+ stylesheet_link_tag("formtastic_changes")
7
+ end
8
+
9
+ end
10
+ end
data/rails/init.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  # coding: utf-8
2
2
  require File.join(File.dirname(__FILE__), *%w[.. lib formtastic])
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib formtastic layout_helper])
3
4
  ActionView::Base.send :include, Formtastic::SemanticFormHelper
5
+ ActionView::Base.send :include, Formtastic::LayoutHelper
@@ -121,9 +121,6 @@ describe 'SemanticFormBuilder#commit_button' do
121
121
  end
122
122
 
123
123
  describe 'label' do
124
- before do
125
- ::Post.stub!(:human_name).and_return('Post')
126
- end
127
124
 
128
125
  # No object
129
126
  describe 'when used without object' do
@@ -240,6 +237,10 @@ describe 'SemanticFormBuilder#commit_button' do
240
237
  }
241
238
  ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
242
239
  end
240
+
241
+ after do
242
+ ::I18n.backend.store_translations :en, :formtastic => nil
243
+ end
243
244
 
244
245
  it 'should render an input with localized label (I18n)' do
245
246
  semantic_form_for(@new_post) do |builder|
@@ -261,6 +262,8 @@ describe 'SemanticFormBuilder#commit_button' do
261
262
  concat(builder.commit_button)
262
263
  end
263
264
  output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create"][@class~="create"]})
265
+ ::I18n.backend.store_translations :en, :formtastic => nil
266
+
264
267
  end
265
268
 
266
269
  end
@@ -334,11 +337,30 @@ describe 'SemanticFormBuilder#commit_button' do
334
337
  concat(builder.commit_button)
335
338
  end
336
339
  output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save"][@class~="update"]})
340
+ ::I18n.backend.store_translations :en, :formtastic => {}
337
341
  end
338
342
 
339
343
  end
340
344
  end
341
345
  end
342
346
  end
343
-
347
+
348
+ describe 'when the model is two words' do
349
+ before do
350
+ output_buffer = ''
351
+ class ::UserPost; def id; end; def self.human_name; "Userpost"; end; end # Rails does crappy human_name
352
+ @new_user_post = ::UserPost.new
353
+
354
+ @new_user_post.stub!(:new_record?).and_return(true)
355
+ semantic_form_for(@new_user_post, :url => '') do |builder|
356
+ concat(builder.commit_button())
357
+ end
358
+ end
359
+
360
+ it "should render the string as the value of the button" do
361
+ output_buffer.should have_tag('li input[@value="Create User post"]')
362
+ end
363
+
364
+ end
365
+
344
366
  end
@@ -224,107 +224,6 @@ module CustomMacros
224
224
  end
225
225
  end
226
226
 
227
- def it_should_select_existing_datetime_else_current(*datetime_parts)
228
- describe "default value" do
229
- before do
230
- @new_post.should_receive(:publish_at=).any_number_of_times
231
- end
232
-
233
- describe "when attribute value is present" do
234
- before do
235
- @output_buffer = ''
236
- publish_at_value = 1.year.ago + 2.month + 3.day + 4.hours + 5.minutes # No comment =)
237
- @new_post.stub!(:publish_at).and_return(publish_at_value)
238
-
239
- semantic_form_for(@new_post) do |builder|
240
- concat(builder.input(:publish_at, :as => :datetime))
241
- end
242
- end
243
-
244
- it "should select the present value by default" do
245
- # puts output_buffer
246
- output_buffer.should have_tag("form li select#post_publish_at_1i option[@selected='selected'][@value='#{@new_post.publish_at.year}']") if datetime_parts.include?(:year)
247
- output_buffer.should have_tag("form li select#post_publish_at_2i option[@selected='selected'][@value='#{@new_post.publish_at.month}']") if datetime_parts.include?(:month)
248
- output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@new_post.publish_at.day}']") if datetime_parts.include?(:day)
249
- output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@new_post.publish_at.strftime("%H")}']") if datetime_parts.include?(:hour)
250
- output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@new_post.publish_at.strftime("%M")}']") if datetime_parts.include?(:minute)
251
- #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@new_post.publish_at.sec}']") if datetime_parts.include?(:second)
252
- end
253
- end
254
-
255
- describe "when no attribute value is present" do
256
- before do
257
- @output_buffer = ''
258
- @new_post.stub!(:publish_at).and_return(nil)
259
- @current_time = ::Time.now
260
-
261
- semantic_form_for(@new_post) do |builder|
262
- concat(builder.input(:publish_at, :as => :datetime))
263
- end
264
- end
265
-
266
- it "should select the current day/time by default" do
267
- # puts output_buffer
268
- output_buffer.should have_tag("form li select#post_publish_at_1i option[@selected='selected'][@value='#{@current_time.year}']") if datetime_parts.include?(:year)
269
- output_buffer.should have_tag("form li select#post_publish_at_2i option[@selected='selected'][@value='#{@current_time.month}']") if datetime_parts.include?(:month)
270
- output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@current_time.day}']") if datetime_parts.include?(:day)
271
- output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@current_time.strftime("%H")}']") if datetime_parts.include?(:hour)
272
- output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@current_time.strftime("%M")}']") if datetime_parts.include?(:minute)
273
- #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@custom_default_time.sec}']") if datetime_parts.include?(:second)
274
- end
275
-
276
- # TODO: Scenario when current time is not a possible choice (because of specified date/time ranges)?
277
- end
278
- end
279
- end
280
-
281
- def it_should_select_explicit_default_value_if_set(*datetime_parts)
282
- describe 'when :selected is set' do
283
- before do
284
- @output_buffer = ''
285
- end
286
-
287
- # Note: Not possible to override default selected value for time_zone input
288
- # without overriding Rails core helper. This Rails helper works "a bit different". =/
289
- #
290
- describe "no selected items" do
291
- before do
292
- @default_time = 2.days.ago
293
- @new_post.stub!(:publish_at).and_return(@default_time)
294
-
295
- semantic_form_for(@new_post) do |builder|
296
- concat(builder.input(:publish_at, :as => :time_zone, :selected => nil))
297
- end
298
- end
299
-
300
- it 'should not have any selected item(s)' do
301
- output_buffer.should_not have_tag("form li select#post_publish_at_1i option[@selected='selected']")
302
- end
303
- end
304
-
305
- describe "single selected item" do
306
- before do
307
- @custom_default_time = 5.days.ago
308
- @new_post.stub!(:publish_at).and_return(2.days.ago)
309
-
310
- semantic_form_for(@new_post) do |builder|
311
- concat(builder.input(:publish_at, :as => :datetime, :selected => @custom_default_time))
312
- end
313
- end
314
-
315
- it "should select the specified value" do
316
- output_buffer.should have_tag("form li select#post_publish_at_1i option[@selected='selected'][@value='#{@custom_default_time.year}']") if datetime_parts.include?(:year)
317
- output_buffer.should have_tag("form li select#post_publish_at_2i option[@selected='selected'][@value='#{@custom_default_time.month}']") if datetime_parts.include?(:month)
318
- output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@custom_default_time.day}']") if datetime_parts.include?(:day)
319
- output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@custom_default_time.strftime("%H")}']") if datetime_parts.include?(:hour)
320
- output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@custom_default_time.strftime("%M")}']") if datetime_parts.include?(:minute)
321
- #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@custom_default_time.sec}']") if datetime_parts.include?(:second)
322
- end
323
- end
324
-
325
- end
326
- end
327
-
328
227
  def it_should_use_the_collection_when_provided(as, countable)
329
228
  describe 'when the :collection option is provided' do
330
229
 
@@ -40,6 +40,12 @@ describe 'SemanticFormHelper' do
40
40
  output_buffer.should have_tag("form.post")
41
41
  end
42
42
 
43
+ it 'adds a namespaced class to the generated form' do
44
+ semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder|
45
+ end
46
+ output_buffer.should have_tag("form.namespaced_post")
47
+ end
48
+
43
49
  describe 'allows :html options' do
44
50
  before(:each) do
45
51
  semantic_form_for(:post, ::Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
data/spec/input_spec.rb CHANGED
@@ -399,6 +399,26 @@ describe 'SemanticFormBuilder#input' do
399
399
  end
400
400
 
401
401
  describe 'when not provided' do
402
+ describe 'when localized label is provided' do
403
+ describe 'and object is given' do
404
+ describe 'and label_str_method not default' do
405
+ it 'should render a label with localized label (I18n)' do
406
+ with_config :label_str_method, :capitalize do
407
+ @localized_label_text = 'Localized title'
408
+ @new_post.stub!(:meta_description)
409
+ @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text)
410
+
411
+ semantic_form_for(@new_post) do |builder|
412
+ concat(builder.input(:meta_description))
413
+ end
414
+
415
+ output_buffer.should have_tag('form li label', @localized_label_text)
416
+ end
417
+ end
418
+ end
419
+ end
420
+ end
421
+
402
422
  describe 'when localized label is NOT provided' do
403
423
  describe 'and object is not given' do
404
424
  it 'should default the humanized method name, passing it down to the label tag' do
@@ -424,6 +444,20 @@ describe 'SemanticFormBuilder#input' do
424
444
  output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
425
445
  end
426
446
  end
447
+
448
+ describe 'and object is given with label_str_method set to :capitalize' do
449
+ it 'should capitalize method name, passing it down to the label tag' do
450
+ with_config :label_str_method, :capitalize do
451
+ @new_post.stub!(:meta_description)
452
+
453
+ semantic_form_for(@new_post) do |builder|
454
+ concat(builder.input(:meta_description))
455
+ end
456
+
457
+ output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
458
+ end
459
+ end
460
+ end
427
461
  end
428
462
 
429
463
  describe 'when localized label is provided' do
@@ -65,8 +65,10 @@ describe 'boolean input' do
65
65
  before do
66
66
  @new_post.stub!(:allow_comments).and_return(true)
67
67
 
68
- semantic_form_for(@new_post) do |builder|
69
- concat(builder.input(:allow_comments, :as => :boolean, :selected => false))
68
+ with_deprecation_silenced do
69
+ semantic_form_for(@new_post) do |builder|
70
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => false))
71
+ end
70
72
  end
71
73
  end
72
74
 
@@ -78,9 +80,11 @@ describe 'boolean input' do
78
80
  describe "selected" do
79
81
  before do
80
82
  @new_post.stub!(:allow_comments).and_return(false)
81
-
82
- semantic_form_for(@new_post) do |builder|
83
- concat(builder.input(:allow_comments, :as => :boolean, :selected => true))
83
+
84
+ with_deprecation_silenced do
85
+ semantic_form_for(@new_post) do |builder|
86
+ concat(builder.input(:allow_comments, :as => :boolean, :selected => true))
87
+ end
84
88
  end
85
89
  end
86
90
 
@@ -22,10 +22,15 @@ describe 'check_boxes input' do
22
22
  it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes)
23
23
  it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
24
24
 
25
- it 'should generate a legend - classified as a label - containing label text for the input' do
26
- output_buffer.should have_tag('form li fieldset legend.label')
27
- output_buffer.should have_tag('form li fieldset legend.label', /Posts/)
25
+ it 'should generate a legend containing a label with text for the input' do
26
+ output_buffer.should have_tag('form li fieldset legend.label label')
27
+ output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
28
28
  end
29
+
30
+ it 'should not link the label within the legend to any input' do
31
+ output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
32
+ end
33
+
29
34
 
30
35
  it 'should generate an ordered list with a list item for each choice' do
31
36
  output_buffer.should have_tag('form li fieldset ol')
@@ -110,8 +115,10 @@ describe 'check_boxes input' do
110
115
  before do
111
116
  @new_post.stub!(:author_ids).and_return(nil)
112
117
 
113
- semantic_form_for(@new_post) do |builder|
114
- concat(builder.input(:authors, :as => :check_boxes, :selected => nil))
118
+ with_deprecation_silenced do
119
+ semantic_form_for(@new_post) do |builder|
120
+ concat(builder.input(:authors, :as => :check_boxes, :selected => nil))
121
+ end
115
122
  end
116
123
  end
117
124
 
@@ -124,8 +131,10 @@ describe 'check_boxes input' do
124
131
  before do
125
132
  @new_post.stub!(:author_ids).and_return(nil)
126
133
 
127
- semantic_form_for(@new_post) do |builder|
128
- concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id))
134
+ with_deprecation_silenced do
135
+ semantic_form_for(@new_post) do |builder|
136
+ concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id))
137
+ end
129
138
  end
130
139
  end
131
140
 
@@ -139,9 +148,11 @@ describe 'check_boxes input' do
139
148
  describe "multiple selected items" do
140
149
  before do
141
150
  @new_post.stub!(:author_ids).and_return(nil)
142
-
143
- semantic_form_for(@new_post) do |builder|
144
- concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id]))
151
+
152
+ with_deprecation_silenced do
153
+ semantic_form_for(@new_post) do |builder|
154
+ concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id]))
155
+ end
145
156
  end
146
157
  end
147
158
 
@@ -155,8 +166,20 @@ describe 'check_boxes input' do
155
166
  end
156
167
 
157
168
  end
169
+
170
+ it 'should warn about :selected deprecation' do
171
+ with_deprecation_silenced do
172
+ ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
173
+ semantic_form_for(@new_post) do |builder|
174
+ concat(builder.input(:authors, :as => :check_boxes, :selected => @bob.id))
175
+ end
176
+ end
177
+ end
178
+
158
179
 
159
180
  end
181
+
182
+
160
183
 
161
184
  end
162
185
 
@@ -8,38 +8,150 @@ describe 'date input' do
8
8
  before do
9
9
  @output_buffer = ''
10
10
  mock_everything
11
+ end
12
+
13
+ describe "general" do
11
14
 
12
- semantic_form_for(@new_post) do |builder|
13
- concat(builder.input(:publish_at, :as => :date))
15
+ before do
16
+ output_buffer.replace ''
17
+ semantic_form_for(@new_post) do |builder|
18
+ concat(builder.input(:publish_at, :as => :date))
19
+ end
14
20
  end
15
- end
16
21
 
17
- it_should_have_input_wrapper_with_class("date")
18
- it_should_have_input_wrapper_with_id("post_publish_at_input")
19
- it_should_have_a_nested_fieldset
20
- it_should_apply_error_logic_for_input_type(:date)
21
-
22
- it 'should have a legend - classified as a label - containing the label text inside the fieldset' do
23
- output_buffer.should have_tag('form li.date fieldset legend.label', /Publish at/)
22
+ it_should_have_input_wrapper_with_class("date")
23
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
24
+ it_should_have_a_nested_fieldset
25
+ it_should_apply_error_logic_for_input_type(:date)
26
+
27
+ it 'should have a legend and label with the label text inside the fieldset' do
28
+ output_buffer.should have_tag('form li.date fieldset legend.label label', /Publish at/)
29
+ end
30
+
31
+ it 'should associate the legend label with the first select' do
32
+ output_buffer.should have_tag('form li.date fieldset legend.label')
33
+ output_buffer.should have_tag('form li.date fieldset legend.label label')
34
+ output_buffer.should have_tag('form li.date fieldset legend.label label[@for]')
35
+ output_buffer.should have_tag('form li.date fieldset legend.label label[@for="post_publish_at_1i"]')
36
+ end
37
+
38
+ it 'should have an ordered list of three items inside the fieldset' do
39
+ output_buffer.should have_tag('form li.date fieldset ol')
40
+ output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
41
+ end
42
+
43
+ it 'should have three labels for year, month and day' do
44
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
45
+ output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
46
+ output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
47
+ output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
48
+ end
49
+
50
+ it 'should have three selects for year, month and day' do
51
+ output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
52
+ end
24
53
  end
25
-
26
- it 'should have an ordered list of three items inside the fieldset' do
27
- output_buffer.should have_tag('form li.date fieldset ol')
28
- output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
54
+
55
+ describe ':selected option' do
56
+
57
+ describe "when the object has a value" do
58
+ it "should select the object value (ignoring :selected)" do
59
+ output_buffer.replace ''
60
+ @new_post.stub!(:created_at => Time.mktime(2012))
61
+ with_deprecation_silenced do
62
+ semantic_form_for(@new_post) do |builder|
63
+ concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
64
+ end
65
+ end
66
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
67
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1)
68
+ end
69
+ end
70
+
71
+ describe 'when the object has no value' do
72
+ it "should select the :selected if provided as a Date" do
73
+ output_buffer.replace ''
74
+ @new_post.stub!(:created_at => nil)
75
+ with_deprecation_silenced do
76
+ semantic_form_for(@new_post) do |builder|
77
+ concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999)))
78
+ end
79
+ end
80
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
81
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
82
+ end
83
+
84
+ it "should select the :selected if provided as a Time" do
85
+ output_buffer.replace ''
86
+ @new_post.stub!(:created_at => nil)
87
+ with_deprecation_silenced do
88
+ semantic_form_for(@new_post) do |builder|
89
+ concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
90
+ end
91
+ end
92
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
93
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
94
+ end
95
+
96
+ it "should not select an option if the :selected is provided as nil" do
97
+ output_buffer.replace ''
98
+ @new_post.stub!(:created_at => nil)
99
+ with_deprecation_silenced do
100
+ semantic_form_for(@new_post) do |builder|
101
+ concat(builder.input(:created_at, :as => :date, :selected => nil))
102
+ end
103
+ end
104
+ output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]")
105
+ end
106
+
107
+ it "should select Time.now if a :selected is not provided" do
108
+ output_buffer.replace ''
109
+ @new_post.stub!(:created_at => nil)
110
+ semantic_form_for(@new_post) do |builder|
111
+ concat(builder.input(:created_at, :as => :date))
112
+ end
113
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
114
+ output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='#{Time.now.year}'][@selected]", :count => 1)
115
+
116
+ end
117
+ end
118
+
29
119
  end
30
120
 
31
- it 'should have three labels for year, month and day' do
32
- output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
33
- output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
34
- output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
35
- output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
36
- end
121
+ describe ':labels option' do
122
+ fields = [:year, :month, :day]
123
+ fields.each do |field|
124
+ it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
125
+ output_buffer.replace ''
126
+ semantic_form_for(@new_post) do |builder|
127
+ concat(builder.input(:created_at, :as => :date, :labels => { field => "another #{field} label" }))
128
+ end
129
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length)
130
+ fields.each do |f|
131
+ output_buffer.should have_tag('form li.date fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
132
+ end
133
+ end
37
134
 
38
- it 'should have three selects for year, month and day' do
39
- output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
135
+ it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
136
+ output_buffer.replace ''
137
+ semantic_form_for(@new_post) do |builder|
138
+ concat(builder.input(:created_at, :as => :date, :labels => { field => "" }))
139
+ end
140
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length-1)
141
+ fields.each do |f|
142
+ output_buffer.should have_tag('form li.date fieldset ol li label', /#{f}/i) unless field == f
143
+ end
144
+ end
145
+ end
40
146
  end
41
147
 
42
- it_should_select_existing_datetime_else_current(:year, :month, :day)
43
- it_should_select_explicit_default_value_if_set(:year, :month, :day)
44
-
148
+ it 'should warn about :selected deprecation' do
149
+ with_deprecation_silenced do
150
+ ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
151
+ semantic_form_for(@new_post) do |builder|
152
+ concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999)))
153
+ end
154
+ end
155
+ end
156
+
45
157
  end