formtastic 1.0.1 → 1.1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.textile +2 -2
  2. data/Rakefile +33 -8
  3. data/generators/formtastic/templates/formtastic.css +3 -18
  4. data/init.rb +5 -0
  5. data/lib/formtastic.rb +180 -82
  6. data/lib/formtastic/i18n.rb +8 -9
  7. data/lib/formtastic/layout_helper.rb +0 -1
  8. data/lib/formtastic/railtie.rb +12 -0
  9. data/lib/formtastic/util.rb +7 -0
  10. data/lib/generators/formtastic/form/form_generator.rb +86 -0
  11. data/lib/generators/formtastic/install/install_generator.rb +24 -0
  12. data/rails/init.rb +1 -6
  13. data/spec/buttons_spec.rb +25 -8
  14. data/spec/commit_button_spec.rb +67 -29
  15. data/spec/custom_builder_spec.rb +40 -4
  16. data/spec/defaults_spec.rb +1 -1
  17. data/spec/error_proc_spec.rb +1 -1
  18. data/spec/errors_spec.rb +3 -2
  19. data/spec/form_helper_spec.rb +33 -17
  20. data/spec/helpers/layout_helper_spec.rb +21 -0
  21. data/spec/i18n_spec.rb +13 -9
  22. data/spec/include_blank_spec.rb +9 -5
  23. data/spec/input_spec.rb +188 -48
  24. data/spec/inputs/boolean_input_spec.rb +14 -7
  25. data/spec/inputs/check_boxes_input_spec.rb +150 -20
  26. data/spec/inputs/country_input_spec.rb +9 -5
  27. data/spec/inputs/date_input_spec.rb +21 -9
  28. data/spec/inputs/datetime_input_spec.rb +33 -14
  29. data/spec/inputs/file_input_spec.rb +4 -3
  30. data/spec/inputs/hidden_input_spec.rb +11 -4
  31. data/spec/inputs/numeric_input_spec.rb +3 -3
  32. data/spec/inputs/password_input_spec.rb +3 -3
  33. data/spec/inputs/radio_input_spec.rb +29 -10
  34. data/spec/inputs/select_input_spec.rb +66 -26
  35. data/spec/inputs/string_input_spec.rb +5 -4
  36. data/spec/inputs/text_input_spec.rb +4 -3
  37. data/spec/inputs/time_input_spec.rb +26 -10
  38. data/spec/inputs/time_zone_input_spec.rb +11 -5
  39. data/spec/inputs_spec.rb +103 -39
  40. data/spec/label_spec.rb +5 -3
  41. data/spec/semantic_errors_spec.rb +7 -7
  42. data/spec/semantic_fields_for_spec.rb +5 -4
  43. data/spec/spec_helper.rb +86 -38
  44. data/spec/{custom_macros.rb → support/custom_macros.rb} +69 -35
  45. data/spec/support/output_buffer.rb +4 -0
  46. data/spec/support/test_environment.rb +45 -0
  47. metadata +26 -28
  48. data/spec/layout_helper_spec.rb +0 -29
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::SemanticFormHelper.builder' do
5
5
 
@@ -29,7 +29,16 @@ describe 'Formtastic::SemanticFormHelper.builder' do
29
29
  ::Formtastic::SemanticFormHelper.builder = ::Formtastic::SemanticFormBuilder
30
30
  ::Formtastic::SemanticFormHelper.builder.should == ::Formtastic::SemanticFormBuilder
31
31
  end
32
-
32
+
33
+ it 'should allow custom settings per form builder subclass' do
34
+ with_config(:all_fields_required_by_default, true) do
35
+ MyCustomFormBuilder.all_fields_required_by_default = false
36
+
37
+ MyCustomFormBuilder.all_fields_required_by_default.should be_false
38
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should be_true
39
+ end
40
+ end
41
+
33
42
  describe "when using a custom builder" do
34
43
 
35
44
  before do
@@ -43,7 +52,7 @@ describe 'Formtastic::SemanticFormHelper.builder' do
43
52
 
44
53
  describe "semantic_form_for" do
45
54
 
46
- it "should yeild and instance of the custom builder" do
55
+ it "should yield an instance of the custom builder" do
47
56
  semantic_form_for(@new_post) do |builder|
48
57
  builder.class.should == MyCustomFormBuilder
49
58
  end
@@ -56,7 +65,34 @@ describe 'Formtastic::SemanticFormHelper.builder' do
56
65
  end
57
66
 
58
67
  end
68
+
69
+ describe "semantic_fields_for" do
70
+
71
+ it "should yield an instance of the parent form builder" do
72
+ semantic_form_for(@new_post) do |builder|
73
+ builder.semantic_fields_for(:author) do |nested_builder|
74
+ nested_builder.class.should == MyCustomFormBuilder
75
+ end
76
+ end
77
+ end
78
+
79
+ end
59
80
 
60
81
  end
61
82
 
62
- end
83
+ describe "when using a builder passed to form options" do
84
+
85
+ describe "semantic_fields_for" do
86
+
87
+ it "should yield an instance of the parent form builder" do
88
+ semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
89
+ builder.semantic_fields_for(:author) do |nested_builder|
90
+ nested_builder.class.should == MyCustomFormBuilder
91
+ end
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::SemanticFormBuilder-defaults' do
5
5
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Rails field_error_proc' do
5
5
 
data/spec/errors_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#errors_on' do
5
5
 
@@ -91,9 +91,10 @@ describe 'SemanticFormBuilder#errors_on' do
91
91
  @errors.stub!(:[]).with(:author).and_return(['must not be blank'])
92
92
  @errors.stub!(:[]).with(:author_id).and_return(['is already taken', 'must not be blank']) # note the duplicate of association
93
93
 
94
- semantic_form_for(@new_post) do |builder|
94
+ form = semantic_form_for(@new_post) do |builder|
95
95
  concat(builder.input(:author))
96
96
  end
97
+ output_buffer.concat(form) if Formtastic::Util.rails3?
97
98
  output_buffer.should have_tag("ul.errors li", /must not be blank/, :count => 1)
98
99
  output_buffer.should have_tag("ul.errors li", /is already taken/, :count => 1)
99
100
  end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormHelper' do
5
5
 
@@ -13,54 +13,62 @@ describe 'SemanticFormHelper' do
13
13
  describe '#semantic_form_for' do
14
14
 
15
15
  it 'yields an instance of SemanticFormBuilder' do
16
- semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
16
+ semantic_form_for(@new_post, :url => '/hello') do |builder|
17
17
  builder.class.should == ::Formtastic::SemanticFormBuilder
18
18
  end
19
19
  end
20
20
 
21
21
  it 'adds a class of "formtastic" to the generated form' do
22
- semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
22
+ form = semantic_form_for(@new_post, :url => '/hello') do |builder|
23
23
  end
24
+ output_buffer.concat(form) if Formtastic::Util.rails3?
24
25
  output_buffer.should have_tag("form.formtastic")
25
26
  end
26
27
 
27
28
  it 'adds class matching the object name to the generated form when a symbol is provided' do
28
- semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
29
+ form = semantic_form_for(@new_post, :url => '/hello') do |builder|
29
30
  end
31
+ output_buffer.concat(form) if Formtastic::Util.rails3?
30
32
  output_buffer.should have_tag("form.post")
31
33
 
32
- semantic_form_for(:project, :url => '/hello') do |builder|
34
+ form = semantic_form_for(:project, :url => '/hello') do |builder|
33
35
  end
36
+ output_buffer.concat(form) if Formtastic::Util.rails3?
34
37
  output_buffer.should have_tag("form.project")
35
38
  end
36
39
 
37
40
  it 'adds class matching the object\'s class to the generated form when an object is provided' do
38
- semantic_form_for(@new_post) do |builder|
41
+ form = semantic_form_for(@new_post) do |builder|
39
42
  end
43
+ output_buffer.concat(form) if Formtastic::Util.rails3?
40
44
  output_buffer.should have_tag("form.post")
41
45
  end
42
46
 
43
47
  it 'adds a namespaced class to the generated form' do
44
- semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder|
48
+ form = semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder|
45
49
  end
50
+ output_buffer.concat(form) if Formtastic::Util.rails3?
46
51
  output_buffer.should have_tag("form.namespaced_post")
47
52
  end
48
53
 
49
54
  describe 'allows :html options' do
50
55
  before(:each) do
51
- semantic_form_for(:post, ::Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
56
+ @form = semantic_form_for(@new_post, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
52
57
  end
53
58
  end
54
59
 
55
60
  it 'to add a id of "something-special" to generated form' do
61
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
56
62
  output_buffer.should have_tag("form#something-special")
57
63
  end
58
64
 
59
65
  it 'to add a class of "something-extra" to generated form' do
66
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
60
67
  output_buffer.should have_tag("form.something-extra")
61
68
  end
62
69
 
63
70
  it 'to add enctype="multipart/form-data"' do
71
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
64
72
  output_buffer.should have_tag('form[@enctype="multipart/form-data"]')
65
73
  end
66
74
  end
@@ -71,16 +79,24 @@ describe 'SemanticFormHelper' do
71
79
  builder.object_name.should == "post"
72
80
  end
73
81
  end
74
-
82
+
75
83
  it 'can be called with a generic style and instance variable' do
76
- semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
77
- builder.object.class.should == ::Post
78
- builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
84
+ if rails3?
85
+ semantic_form_for(@new_post, :as => :post, :url => new_post_path) do |builder|
86
+ builder.object.class.should == ::Post
87
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
88
+ end
89
+ end
90
+ if rails2?
91
+ semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
92
+ builder.object.class.should == ::Post
93
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
94
+ end
79
95
  end
80
96
  end
81
97
 
82
98
  it 'can be called with a generic style and inline object' do
83
- semantic_form_for(:post, ::Post.new, :url => new_post_path) do |builder|
99
+ semantic_form_for(@new_post, :url => new_post_path) do |builder|
84
100
  builder.object.class.should == ::Post
85
101
  builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
86
102
  end
@@ -90,7 +106,7 @@ describe 'SemanticFormHelper' do
90
106
  it "yields an instance of the given builder" do
91
107
  class MyAwesomeCustomBuilder < ::Formtastic::SemanticFormBuilder
92
108
  end
93
- semantic_form_for(:post, ::Post.new, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
109
+ semantic_form_for(@new_post, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
94
110
  builder.class.should == MyAwesomeCustomBuilder
95
111
  end
96
112
  end
@@ -100,7 +116,7 @@ describe 'SemanticFormHelper' do
100
116
 
101
117
  describe '#semantic_fields_for' do
102
118
  it 'yields an instance of SemanticFormBuilder' do
103
- semantic_fields_for(:post, ::Post.new, :url => '/hello') do |builder|
119
+ semantic_fields_for(@new_post, :url => '/hello') do |builder|
104
120
  builder.class.should == ::Formtastic::SemanticFormBuilder
105
121
  end
106
122
  end
@@ -108,7 +124,7 @@ describe 'SemanticFormHelper' do
108
124
 
109
125
  describe '#semantic_form_remote_for' do
110
126
  it 'yields an instance of SemanticFormBuilder' do
111
- semantic_form_remote_for(:post, ::Post.new, :url => '/hello') do |builder|
127
+ semantic_form_remote_for(@new_post, :url => '/hello') do |builder|
112
128
  builder.class.should == ::Formtastic::SemanticFormBuilder
113
129
  end
114
130
  end
@@ -116,7 +132,7 @@ describe 'SemanticFormHelper' do
116
132
 
117
133
  describe '#semantic_form_for_remote' do
118
134
  it 'yields an instance of SemanticFormBuilder' do
119
- semantic_remote_form_for(:post, ::Post.new, :url => '/hello') do |builder|
135
+ semantic_remote_form_for(@new_post, :url => '/hello') do |builder|
120
136
  builder.class.should == ::Formtastic::SemanticFormBuilder
121
137
  end
122
138
  end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Formtastic::LayoutHelper do
5
+
6
+ include FormtasticSpecHelper
7
+ include Formtastic::LayoutHelper
8
+
9
+ describe '#formtastic_stylesheet_link_tag' do
10
+
11
+ it 'should render a link to formtastic.css' do
12
+ formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic.css']")
13
+ end
14
+
15
+ it 'should render a link to formtastic_changes.css' do
16
+ formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic_changes.css']")
17
+ end
18
+
19
+ end
20
+ end
21
+
data/spec/i18n_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'Formtastic::I18n' do
5
5
 
@@ -26,11 +26,11 @@ describe 'Formtastic::I18n' do
26
26
  @formtastic_strings = {
27
27
  :yes => 'Default Yes',
28
28
  :no => 'Default No',
29
- :create => 'Default Create {{model}}',
30
- :update => 'Default Update {{model}}',
29
+ :create => 'Default Create %{model}',
30
+ :update => 'Default Update %{model}',
31
31
  :custom_scope => {
32
32
  :duck => 'Duck',
33
- :duck_pond => '{{ducks}} ducks in a pond'
33
+ :duck_pond => '%{ducks} ducks in a pond'
34
34
  }
35
35
  }
36
36
  ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
@@ -71,7 +71,7 @@ describe 'Formtastic::I18n' do
71
71
 
72
72
  it "should use default strings" do
73
73
  (::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
74
- ::Formtastic::I18n.t(key, :model => '{{model}}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
74
+ ::Formtastic::I18n.t(key, :model => '%{model}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
75
75
  end
76
76
  end
77
77
 
@@ -109,34 +109,38 @@ describe 'Formtastic::I18n' do
109
109
  end
110
110
 
111
111
  it "should be able to translate with namespaced object" do
112
- semantic_form_for(@new_post) do |builder|
112
+ form = semantic_form_for(@new_post) do |builder|
113
113
  concat(builder.input(:title))
114
114
  end
115
+ output_buffer.concat(form) if Formtastic::Util.rails3?
115
116
  output_buffer.should have_tag("form label", /Hello post!/)
116
117
  end
117
118
 
118
119
  it "should be able to translate without form-object" do
119
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
120
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
120
121
  concat(builder.input(:title))
121
122
  end
123
+ output_buffer.concat(form) if Formtastic::Util.rails3?
122
124
  output_buffer.should have_tag("form label", /Hello project!/)
123
125
  end
124
126
 
125
127
  it 'should be able to translate nested objects with nested translations' do
126
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
128
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
127
129
  builder.semantic_fields_for(:task) do |f|
128
130
  concat(f.input(:name))
129
131
  end
130
132
  end
133
+ output_buffer.concat(form) if Formtastic::Util.rails3?
131
134
  output_buffer.should have_tag("form label", /Hello task name!/)
132
135
  end
133
136
 
134
137
  it 'should be able to translated nested objects with top level translations' do
135
- semantic_form_for(:order, :url => 'http://test.host') do |builder|
138
+ form = semantic_form_for(:order, :url => 'http://test.host') do |builder|
136
139
  builder.semantic_fields_for(:line_item) do |f|
137
140
  concat(f.input(:name))
138
141
  end
139
142
  end
143
+ output_buffer.concat(form) if Formtastic::Util.rails3?
140
144
  output_buffer.should have_tag("form label", /Hello line item name!/)
141
145
  end
142
146
 
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe "*select: options[:include_blank]" do
5
5
 
@@ -24,9 +24,10 @@ describe "*select: options[:include_blank]" do
24
24
  it 'blank value should be included if the default value specified in config is true' do
25
25
  ::Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = true
26
26
  @select_input_types.each do |as, attribute|
27
- semantic_form_for(@new_post) do |builder|
27
+ form = semantic_form_for(@new_post) do |builder|
28
28
  concat(builder.input(attribute, :as => as))
29
29
  end
30
+ output_buffer.concat(form) if Formtastic::Util.rails3?
30
31
  output_buffer.should have_tag("form li select option[@value='']", "")
31
32
  end
32
33
  end
@@ -34,9 +35,10 @@ describe "*select: options[:include_blank]" do
34
35
  it 'blank value should not be included if the default value specified in config is false' do
35
36
  ::Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = false
36
37
  @select_input_types.each do |as, attribute|
37
- semantic_form_for(@new_post) do |builder|
38
+ form = semantic_form_for(@new_post) do |builder|
38
39
  concat(builder.input(attribute, :as => as))
39
40
  end
41
+ output_buffer.concat(form) if Formtastic::Util.rails3?
40
42
  output_buffer.should_not have_tag("form li select option[@value='']", "")
41
43
  end
42
44
  end
@@ -49,9 +51,10 @@ describe "*select: options[:include_blank]" do
49
51
  describe 'when :include_blank is set to false' do
50
52
  it 'should not have a blank option' do
51
53
  @select_input_types.each do |as, attribute|
52
- semantic_form_for(@new_post) do |builder|
54
+ form = semantic_form_for(@new_post) do |builder|
53
55
  concat(builder.input(attribute, :as => as, :include_blank => false))
54
56
  end
57
+ output_buffer.concat(form) if Formtastic::Util.rails3?
55
58
  output_buffer.should_not have_tag("form li select option[@value='']", "")
56
59
  end
57
60
  end
@@ -60,9 +63,10 @@ describe "*select: options[:include_blank]" do
60
63
  describe 'when :include_blank => true is set' do
61
64
  it 'should have a blank select option' do
62
65
  @select_input_types.each do |as, attribute|
63
- semantic_form_for(@new_post) do |builder|
66
+ form = semantic_form_for(@new_post) do |builder|
64
67
  concat(builder.input(attribute, :as => as, :include_blank => true))
65
68
  end
69
+ output_buffer.concat(form) if Formtastic::Util.rails3?
66
70
  output_buffer.should have_tag("form li select option[@value='']", "")
67
71
  end
68
72
  end
data/spec/input_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#input' do
5
5
 
@@ -38,7 +38,7 @@ describe 'SemanticFormBuilder#input' do
38
38
 
39
39
  it 'should require the first argument (the method on form\'s object)' do
40
40
  lambda {
41
- semantic_form_for(@new_post) do |builder|
41
+ @form = semantic_form_for(@new_post) do |builder|
42
42
  concat(builder.input()) # no args passed in at all
43
43
  end
44
44
  }.should raise_error(ArgumentError)
@@ -59,17 +59,19 @@ describe 'SemanticFormBuilder#input' do
59
59
  end
60
60
 
61
61
  it 'should set a "required" class' do
62
- semantic_form_for(@new_post) do |builder|
62
+ form = semantic_form_for(@new_post) do |builder|
63
63
  concat(builder.input(:title, :required => true))
64
64
  end
65
+ output_buffer.concat(form) if Formtastic::Util.rails3?
65
66
  output_buffer.should_not have_tag('form li.optional')
66
67
  output_buffer.should have_tag('form li.required')
67
68
  end
68
69
 
69
70
  it 'should append the "required" string to the label' do
70
- semantic_form_for(@new_post) do |builder|
71
+ form = semantic_form_for(@new_post) do |builder|
71
72
  concat(builder.input(:title, :required => true))
72
73
  end
74
+ output_buffer.concat(form) if Formtastic::Util.rails3?
73
75
  output_buffer.should have_tag('form li.required label', /#{@new_string}$/)
74
76
  end
75
77
 
@@ -87,17 +89,19 @@ describe 'SemanticFormBuilder#input' do
87
89
  end
88
90
 
89
91
  it 'should set an "optional" class' do
90
- semantic_form_for(@new_post) do |builder|
92
+ form = semantic_form_for(@new_post) do |builder|
91
93
  concat(builder.input(:title, :required => false))
92
94
  end
95
+ output_buffer.concat(form) if Formtastic::Util.rails3?
93
96
  output_buffer.should_not have_tag('form li.required')
94
97
  output_buffer.should have_tag('form li.optional')
95
98
  end
96
99
 
97
100
  it 'should append the "optional" string to the label' do
98
- semantic_form_for(@new_post) do |builder|
101
+ form = semantic_form_for(@new_post) do |builder|
99
102
  concat(builder.input(:title, :required => false))
100
103
  end
104
+ output_buffer.concat(form) if Formtastic::Util.rails3?
101
105
  output_buffer.should have_tag('form li.optional label', /#{@string}$/)
102
106
  end
103
107
 
@@ -111,9 +115,10 @@ describe 'SemanticFormBuilder#input' do
111
115
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
112
116
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
113
117
 
114
- semantic_form_for(:project, :url => 'http://test.host/') do |builder|
118
+ form = semantic_form_for(:project, :url => 'http://test.host/') do |builder|
115
119
  concat(builder.input(:title))
116
120
  end
121
+ output_buffer.concat(form) if Formtastic::Util.rails3?
117
122
  output_buffer.should_not have_tag('form li.required')
118
123
  output_buffer.should have_tag('form li.optional')
119
124
 
@@ -127,6 +132,7 @@ describe 'SemanticFormBuilder#input' do
127
132
  describe 'and the validation reflection plugin is available' do
128
133
 
129
134
  before do
135
+ ::Post.stub!(:reflect_on_validations_for).and_return([])
130
136
  @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
131
137
  end
132
138
 
@@ -139,10 +145,11 @@ describe 'SemanticFormBuilder#input' do
139
145
  mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true})
140
146
  ])
141
147
 
142
- semantic_form_for(@new_post) do |builder|
148
+ form = semantic_form_for(@new_post) do |builder|
143
149
  concat(builder.input(:title))
144
150
  concat(builder.input(:body))
145
151
  end
152
+ output_buffer.concat(form) if Formtastic::Util.rails3?
146
153
  output_buffer.should have_tag('form li.required')
147
154
  output_buffer.should_not have_tag('form li.optional')
148
155
  end
@@ -150,54 +157,56 @@ describe 'SemanticFormBuilder#input' do
150
157
  it 'should be not be required if the optional :if condition is not satisifed' do
151
158
  should_be_required(:required => false, :options => { :if => false })
152
159
  end
153
-
160
+
154
161
  it 'should not be required if the optional :if proc evaluates to false' do
155
162
  should_be_required(:required => false, :options => { :if => proc { |record| false } })
156
163
  end
157
-
164
+
158
165
  it 'should be required if the optional :if proc evaluates to true' do
159
166
  should_be_required(:required => true, :options => { :if => proc { |record| true } })
160
167
  end
161
-
168
+
162
169
  it 'should not be required if the optional :unless proc evaluates to true' do
163
170
  should_be_required(:required => false, :options => { :unless => proc { |record| true } })
164
171
  end
165
-
172
+
166
173
  it 'should be required if the optional :unless proc evaluates to false' do
167
174
  should_be_required(:required => true, :options => { :unless => proc { |record| false } })
168
175
  end
169
-
176
+
170
177
  it 'should be required if the optional :if with a method string evaluates to true' do
171
178
  @new_post.should_receive(:required_condition).and_return(true)
172
179
  should_be_required(:required => true, :options => { :if => :required_condition })
173
180
  end
174
-
181
+
175
182
  it 'should be required if the optional :if with a method string evaluates to false' do
176
183
  @new_post.should_receive(:required_condition).and_return(false)
177
184
  should_be_required(:required => false, :options => { :if => :required_condition })
178
185
  end
179
-
186
+
180
187
  it 'should not be required if the optional :unless with a method string evaluates to false' do
181
188
  @new_post.should_receive(:required_condition).and_return(false)
182
189
  should_be_required(:required => true, :options => { :unless => :required_condition })
183
190
  end
184
-
191
+
185
192
  it 'should be required if the optional :unless with a method string evaluates to true' do
186
193
  @new_post.should_receive(:required_condition).and_return(true)
187
194
  should_be_required(:required => false, :options => { :unless => :required_condition })
188
195
  end
189
196
  end
190
-
197
+
191
198
  # TODO make a matcher for this?
192
199
  def should_be_required(options)
193
200
  @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
194
201
  mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options])
195
202
  ])
196
-
197
- semantic_form_for(@new_post) do |builder|
203
+
204
+ form = semantic_form_for(@new_post) do |builder|
198
205
  concat(builder.input(:body))
199
206
  end
200
-
207
+
208
+ output_buffer.concat(form) if Formtastic::Util.rails3?
209
+
201
210
  if options[:required]
202
211
  output_buffer.should_not have_tag('form li.optional')
203
212
  output_buffer.should have_tag('form li.required')
@@ -213,9 +222,117 @@ describe 'SemanticFormBuilder#input' do
213
222
  end
214
223
 
215
224
  it 'should not be required' do
216
- semantic_form_for(@new_post) do |builder|
225
+ form = semantic_form_for(@new_post) do |builder|
217
226
  concat(builder.input(:title))
218
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?
219
336
  output_buffer.should_not have_tag('form li.required')
220
337
  output_buffer.should have_tag('form li.optional')
221
338
  end
@@ -229,9 +346,10 @@ describe 'SemanticFormBuilder#input' do
229
346
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
230
347
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
231
348
 
232
- semantic_form_for(@new_post) do |builder|
349
+ form = semantic_form_for(@new_post) do |builder|
233
350
  concat(builder.input(:title))
234
351
  end
352
+ output_buffer.concat(form) if Formtastic::Util.rails3?
235
353
  output_buffer.should_not have_tag('form li.required')
236
354
  output_buffer.should have_tag('form li.optional')
237
355
 
@@ -251,18 +369,20 @@ describe 'SemanticFormBuilder#input' do
251
369
  describe 'when not provided' do
252
370
 
253
371
  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|
372
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
255
373
  concat(builder.input(:anything))
256
374
  end
375
+ output_buffer.concat(form) if Formtastic::Util.rails3?
257
376
  output_buffer.should have_tag('form li.string')
258
377
  end
259
378
 
260
379
  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|
380
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
262
381
  concat(builder.input(:password))
263
382
  concat(builder.input(:password_confirmation))
264
383
  concat(builder.input(:confirm_password))
265
384
  end
385
+ output_buffer.concat(form) if Formtastic::Util.rails3?
266
386
  output_buffer.should have_tag('form li.password', :count => 3)
267
387
  end
268
388
 
@@ -337,7 +457,8 @@ describe 'SemanticFormBuilder#input' do
337
457
  column = mock('column')
338
458
 
339
459
  ::Formtastic::SemanticFormBuilder.file_methods.each do |test|
340
- column.stub!(:respond_to?).with(test).and_return(method == test)
460
+ ### TODO: Check if this is ok
461
+ column.stub!(method).with(test).and_return(method == test)
341
462
  end
342
463
 
343
464
  @new_post.should_receive(method).and_return(column)
@@ -377,23 +498,27 @@ describe 'SemanticFormBuilder#input' do
377
498
 
378
499
  describe 'when provided' do
379
500
  it 'should be passed down to the label tag' do
380
- semantic_form_for(@new_post) do |builder|
501
+ form = semantic_form_for(@new_post) do |builder|
381
502
  concat(builder.input(:title, :label => "Kustom"))
382
503
  end
504
+
505
+ output_buffer.concat(form) if Formtastic::Util.rails3?
383
506
  output_buffer.should have_tag("form li label", /Kustom/)
384
507
  end
385
508
 
386
509
  it 'should not generate a label if false' do
387
- semantic_form_for(@new_post) do |builder|
510
+ form = semantic_form_for(@new_post) do |builder|
388
511
  concat(builder.input(:title, :label => false))
389
512
  end
513
+ output_buffer.concat(form) if Formtastic::Util.rails3?
390
514
  output_buffer.should_not have_tag("form li label")
391
515
  end
392
516
 
393
517
  it 'should be dupped if frozen' do
394
- semantic_form_for(@new_post) do |builder|
518
+ form = semantic_form_for(@new_post) do |builder|
395
519
  concat(builder.input(:title, :label => "Kustom".freeze))
396
520
  end
521
+ output_buffer.concat(form) if Formtastic::Util.rails3?
397
522
  output_buffer.should have_tag("form li label", /Kustom/)
398
523
  end
399
524
  end
@@ -408,11 +533,12 @@ describe 'SemanticFormBuilder#input' do
408
533
  @new_post.stub!(:meta_description)
409
534
  @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text)
410
535
 
411
- semantic_form_for(@new_post) do |builder|
536
+ form = semantic_form_for(@new_post) do |builder|
412
537
  concat(builder.input(:meta_description))
413
538
  end
414
539
 
415
- output_buffer.should have_tag('form li label', @localized_label_text)
540
+ output_buffer.concat(form) if Formtastic::Util.rails3?
541
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
416
542
  end
417
543
  end
418
544
  end
@@ -424,10 +550,11 @@ describe 'SemanticFormBuilder#input' do
424
550
  it 'should default the humanized method name, passing it down to the label tag' do
425
551
  ::Formtastic::SemanticFormBuilder.label_str_method = :humanize
426
552
 
427
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
553
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
428
554
  concat(builder.input(:meta_description))
429
555
  end
430
556
 
557
+ output_buffer.concat(form) if Formtastic::Util.rails3?
431
558
  output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
432
559
  end
433
560
  end
@@ -437,10 +564,11 @@ describe 'SemanticFormBuilder#input' do
437
564
  @new_post.stub!(:meta_description) # a two word method name
438
565
  @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
439
566
 
440
- semantic_form_for(@new_post) do |builder|
567
+ form = semantic_form_for(@new_post) do |builder|
441
568
  concat(builder.input(:meta_description))
442
569
  end
443
570
 
571
+ output_buffer.concat(form) if Formtastic::Util.rails3?
444
572
  output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
445
573
  end
446
574
  end
@@ -450,10 +578,11 @@ describe 'SemanticFormBuilder#input' do
450
578
  with_config :label_str_method, :capitalize do
451
579
  @new_post.stub!(:meta_description)
452
580
 
453
- semantic_form_for(@new_post) do |builder|
581
+ form = semantic_form_for(@new_post) do |builder|
454
582
  concat(builder.input(:meta_description))
455
583
  end
456
584
 
585
+ output_buffer.concat(form) if Formtastic::Util.rails3?
457
586
  output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
458
587
  end
459
588
  end
@@ -479,11 +608,12 @@ describe 'SemanticFormBuilder#input' do
479
608
  end
480
609
 
481
610
  it 'should render a label with localized label (I18n)' do
482
- semantic_form_for(@new_post) do |builder|
611
+ form = semantic_form_for(@new_post) do |builder|
483
612
  concat(builder.input(:title, :label => true))
484
613
  concat(builder.input(:published, :as => :boolean, :label => true))
485
614
  end
486
- output_buffer.should have_tag('form li label', @localized_label_text)
615
+ output_buffer.concat(form) if Formtastic::Util.rails3?
616
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
487
617
  end
488
618
 
489
619
  it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
@@ -496,11 +626,12 @@ describe 'SemanticFormBuilder#input' do
496
626
  }
497
627
  }
498
628
  }
499
- semantic_form_for(@new_post) do |builder|
629
+ form = semantic_form_for(@new_post) do |builder|
500
630
  concat(builder.input(:title, :label => true))
501
631
  concat(builder.input(:published, :as => :boolean, :label => true))
502
632
  end
503
- output_buffer.should have_tag('form li label', @default_localized_label_text)
633
+ output_buffer.concat(form) if Formtastic::Util.rails3?
634
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @default_localized_label_text))
504
635
  end
505
636
  end
506
637
  end
@@ -512,9 +643,10 @@ describe 'SemanticFormBuilder#input' do
512
643
  describe 'when provided' do
513
644
  it 'should be passed down to the paragraph tag' do
514
645
  hint_text = "this is the title of the post"
515
- semantic_form_for(@new_post) do |builder|
646
+ form = semantic_form_for(@new_post) do |builder|
516
647
  concat(builder.input(:title, :hint => hint_text))
517
648
  end
649
+ output_buffer.concat(form) if Formtastic::Util.rails3?
518
650
  output_buffer.should have_tag("form li p.inline-hints", hint_text)
519
651
  end
520
652
  end
@@ -532,11 +664,11 @@ describe 'SemanticFormBuilder#input' do
532
664
  }
533
665
  ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
534
666
  end
535
-
667
+
536
668
  after do
537
669
  ::I18n.backend.reload!
538
670
  end
539
-
671
+
540
672
  describe 'when provided value (hint value) is set to TRUE' do
541
673
  it 'should render a hint paragraph containing a localized hint (I18n)' do
542
674
  ::I18n.backend.store_translations :en,
@@ -547,25 +679,28 @@ describe 'SemanticFormBuilder#input' do
547
679
  }
548
680
  }
549
681
  }
550
- semantic_form_for(@new_post) do |builder|
682
+ form = semantic_form_for(@new_post) do |builder|
551
683
  concat(builder.input(:title, :hint => true))
552
684
  end
685
+ output_buffer.concat(form) if Formtastic::Util.rails3?
553
686
  output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
554
687
  end
555
688
 
556
689
  it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
557
- semantic_form_for(@new_post) do |builder|
690
+ form = semantic_form_for(@new_post) do |builder|
558
691
  concat(builder.input(:title, :hint => true))
559
692
  end
693
+ output_buffer.concat(form) if Formtastic::Util.rails3?
560
694
  output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
561
695
  end
562
696
  end
563
697
 
564
698
  describe 'when provided value (label value) is set to FALSE' do
565
699
  it 'should not render a hint paragraph' do
566
- semantic_form_for(@new_post) do |builder|
700
+ form = semantic_form_for(@new_post) do |builder|
567
701
  concat(builder.input(:title, :hint => false))
568
702
  end
703
+ output_buffer.concat(form) if Formtastic::Util.rails3?
569
704
  output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
570
705
  end
571
706
  end
@@ -590,9 +725,10 @@ describe 'SemanticFormBuilder#input' do
590
725
 
591
726
  describe 'when localized hint (I18n) is not provided' do
592
727
  it 'should not render a hint paragraph' do
593
- semantic_form_for(@new_post) do |builder|
728
+ form = semantic_form_for(@new_post) do |builder|
594
729
  concat(builder.input(:title))
595
730
  end
731
+ output_buffer.concat(form) if Formtastic::Util.rails3?
596
732
  output_buffer.should_not have_tag('form li p.inline-hints')
597
733
  end
598
734
  end
@@ -604,25 +740,28 @@ describe 'SemanticFormBuilder#input' do
604
740
 
605
741
  describe 'when provided' do
606
742
  it 'should be passed down to the li tag' do
607
- semantic_form_for(@new_post) do |builder|
743
+ form = semantic_form_for(@new_post) do |builder|
608
744
  concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
609
745
  end
746
+ output_buffer.concat(form) if Formtastic::Util.rails3?
610
747
  output_buffer.should have_tag("form li#another_id")
611
748
  end
612
749
 
613
750
  it 'should append given classes to li default classes' do
614
- semantic_form_for(@new_post) do |builder|
751
+ form = semantic_form_for(@new_post) do |builder|
615
752
  concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
616
753
  end
754
+ output_buffer.concat(form) if Formtastic::Util.rails3?
617
755
  output_buffer.should have_tag("form li.string")
618
756
  output_buffer.should have_tag("form li.required")
619
757
  output_buffer.should have_tag("form li.another_class")
620
758
  end
621
759
 
622
760
  it 'should allow classes to be an array' do
623
- semantic_form_for(@new_post) do |builder|
761
+ form = semantic_form_for(@new_post) do |builder|
624
762
  concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
625
763
  end
764
+ output_buffer.concat(form) if Formtastic::Util.rails3?
626
765
  output_buffer.should have_tag("form li.string")
627
766
  output_buffer.should have_tag("form li.my_class")
628
767
  output_buffer.should have_tag("form li.another_class")
@@ -631,9 +770,10 @@ describe 'SemanticFormBuilder#input' do
631
770
 
632
771
  describe 'when not provided' do
633
772
  it 'should use default id and class' do
634
- semantic_form_for(@new_post) do |builder|
773
+ form = semantic_form_for(@new_post) do |builder|
635
774
  concat(builder.input(:title))
636
775
  end
776
+ output_buffer.concat(form) if Formtastic::Util.rails3?
637
777
  output_buffer.should have_tag("form li#post_title_input")
638
778
  output_buffer.should have_tag("form li.string")
639
779
  end