formtastic-rails3 0.9.7 → 0.9.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/README.textile +23 -7
  2. data/Rakefile +27 -4
  3. data/generators/formtastic/templates/formtastic.css +9 -7
  4. data/generators/formtastic/templates/formtastic.rb +1 -1
  5. data/generators/formtastic/templates/formtastic_changes.css +4 -0
  6. data/lib/formtastic.rb +190 -142
  7. data/lib/formtastic/i18n.rb +8 -5
  8. data/lib/formtastic/railtie.rb +12 -0
  9. data/lib/formtastic/util.rb +35 -0
  10. data/lib/generators/formtastic/form/form_generator.rb +4 -3
  11. data/lib/generators/formtastic/install/install_generator.rb +2 -1
  12. data/rails/init.rb +5 -0
  13. data/spec/buttons_spec.rb +25 -8
  14. data/spec/commit_button_spec.rb +88 -64
  15. data/spec/custom_builder_spec.rb +1 -1
  16. data/spec/custom_macros.rb +67 -26
  17. data/spec/error_proc_spec.rb +1 -1
  18. data/spec/errors_spec.rb +21 -1
  19. data/spec/form_helper_spec.rb +47 -15
  20. data/spec/i18n_spec.rb +40 -19
  21. data/spec/include_blank_spec.rb +9 -5
  22. data/spec/input_spec.rb +224 -76
  23. data/spec/inputs/boolean_input_spec.rb +22 -11
  24. data/spec/inputs/check_boxes_input_spec.rb +103 -11
  25. data/spec/inputs/country_input_spec.rb +46 -8
  26. data/spec/inputs/date_input_spec.rb +80 -55
  27. data/spec/inputs/datetime_input_spec.rb +134 -83
  28. data/spec/inputs/file_input_spec.rb +4 -3
  29. data/spec/inputs/hidden_input_spec.rb +17 -3
  30. data/spec/inputs/numeric_input_spec.rb +3 -3
  31. data/spec/inputs/password_input_spec.rb +3 -3
  32. data/spec/inputs/radio_input_spec.rb +28 -11
  33. data/spec/inputs/select_input_spec.rb +122 -46
  34. data/spec/inputs/string_input_spec.rb +3 -3
  35. data/spec/inputs/text_input_spec.rb +4 -3
  36. data/spec/inputs/time_input_spec.rb +109 -53
  37. data/spec/inputs/time_zone_input_spec.rb +15 -7
  38. data/spec/inputs_spec.rb +85 -39
  39. data/spec/label_spec.rb +1 -1
  40. data/spec/layout_helper_spec.rb +5 -16
  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 +102 -36
  44. metadata +11 -14
  45. data/lib/generators/formtastic/form/templates/_form.html.erb +0 -5
  46. data/lib/generators/formtastic/form/templates/_form.html.haml +0 -4
  47. data/lib/generators/formtastic/install/templates/formtastic.css +0 -144
  48. data/lib/generators/formtastic/install/templates/formtastic.rb +0 -58
  49. data/lib/generators/formtastic/install/templates/formtastic_changes.css +0 -10
  50. data/spec/inputs/currency_input_spec.rb +0 -80
@@ -6,61 +6,69 @@ describe 'SemanticFormHelper' do
6
6
  include FormtasticSpecHelper
7
7
 
8
8
  before do
9
- @output_buffer = ActiveSupport::SafeBuffer.new
9
+ @output_buffer = ''
10
10
  mock_everything
11
11
  end
12
12
 
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,23 @@ 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|
120
+ builder.class.should == ::Formtastic::SemanticFormBuilder
121
+ end
122
+ end
123
+ end
124
+
125
+ describe '#semantic_form_remote_for' do
126
+ it 'yields an instance of SemanticFormBuilder' do
127
+ semantic_form_remote_for(@new_post, :url => '/hello') do |builder|
128
+ builder.class.should == ::Formtastic::SemanticFormBuilder
129
+ end
130
+ end
131
+ end
132
+
133
+ describe '#semantic_form_for_remote' do
134
+ it 'yields an instance of SemanticFormBuilder' do
135
+ semantic_remote_form_for(@new_post, :url => '/hello') do |builder|
104
136
  builder.class.should == ::Formtastic::SemanticFormBuilder
105
137
  end
106
138
  end
@@ -24,20 +24,24 @@ describe 'Formtastic::I18n' do
24
24
 
25
25
  before do
26
26
  @formtastic_strings = {
27
- :required => 'Default Required',
28
27
  :yes => 'Default Yes',
29
28
  :no => 'Default No',
30
- :create => 'Default Create {{model}}',
31
- :update => 'Default Update {{model}}',
29
+ :create => 'Default Create %{model}',
30
+ :update => 'Default Update %{model}',
32
31
  :custom_scope => {
33
32
  :duck => 'Duck',
34
- :duck_pond => '{{ducks}} ducks in a pond'
33
+ :duck_pond => '%{ducks} ducks in a pond'
35
34
  }
36
35
  }
37
36
  ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
38
37
  end
38
+
39
+ after do
40
+ ::I18n.backend.reload!
41
+ end
39
42
 
40
43
  it "should translate core strings correctly" do
44
+ ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
41
45
  ::Formtastic::I18n.t(:required).should == "Default Required"
42
46
  ::Formtastic::I18n.t(:yes).should == "Default Yes"
43
47
  ::Formtastic::I18n.t(:no).should == "Default No"
@@ -54,7 +58,6 @@ describe 'Formtastic::I18n' do
54
58
  end
55
59
 
56
60
  it "should be possible to override default values" do
57
- ::I18n.backend.store_translations :en, {:formtastic => {:required => nil}}
58
61
  ::Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
59
62
  end
60
63
 
@@ -63,18 +66,12 @@ describe 'Formtastic::I18n' do
63
66
  describe "when no I18n locales are available" do
64
67
 
65
68
  before do
66
- ::I18n.backend.store_translations :en, :formtastic => {
67
- :required => nil,
68
- :yes => nil,
69
- :no => nil,
70
- :create => nil,
71
- :update => nil
72
- }
69
+ ::I18n.backend.reload!
73
70
  end
74
-
71
+
75
72
  it "should use default strings" do
76
73
  (::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
77
- ::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]
78
75
  end
79
76
  end
80
77
 
@@ -85,14 +82,15 @@ describe 'Formtastic::I18n' do
85
82
  include FormtasticSpecHelper
86
83
 
87
84
  before do
88
- @output_buffer = ActiveSupport::SafeBuffer.new
85
+ @output_buffer = ''
89
86
  mock_everything
90
87
 
91
88
  ::I18n.backend.store_translations :en, :formtastic => {
92
89
  :labels => {
93
90
  :title => "Hello world!",
94
91
  :post => {:title => "Hello post!"},
95
- :project => {:title => "Hello project!"}
92
+ :project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
93
+ :line_item => {:name => "Hello line item name!"}
96
94
  }
97
95
  }
98
96
  ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
@@ -102,7 +100,7 @@ describe 'Formtastic::I18n' do
102
100
  end
103
101
 
104
102
  after do
105
- ::I18n.backend.store_translations :en, :formtastic => nil
103
+ ::I18n.backend.reload!
106
104
  ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
107
105
  end
108
106
 
@@ -111,19 +109,42 @@ describe 'Formtastic::I18n' do
111
109
  end
112
110
 
113
111
  it "should be able to translate with namespaced object" do
114
- semantic_form_for(@new_post) do |builder|
112
+ form = semantic_form_for(@new_post) do |builder|
115
113
  concat(builder.input(:title))
116
114
  end
115
+ output_buffer.concat(form) if Formtastic::Util.rails3?
117
116
  output_buffer.should have_tag("form label", /Hello post!/)
118
117
  end
119
118
 
120
119
  it "should be able to translate without form-object" do
121
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
120
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
122
121
  concat(builder.input(:title))
123
122
  end
123
+ output_buffer.concat(form) if Formtastic::Util.rails3?
124
124
  output_buffer.should have_tag("form label", /Hello project!/)
125
125
  end
126
126
 
127
+ it 'should be able to translate nested objects with nested translations' do
128
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
129
+ builder.semantic_fields_for(:task) do |f|
130
+ concat(f.input(:name))
131
+ end
132
+ end
133
+ output_buffer.concat(form) if Formtastic::Util.rails3?
134
+ output_buffer.should have_tag("form label", /Hello task name!/)
135
+ end
136
+
137
+ it 'should be able to translated nested objects with top level translations' do
138
+ form = semantic_form_for(:order, :url => 'http://test.host') do |builder|
139
+ builder.semantic_fields_for(:line_item) do |f|
140
+ concat(f.input(:name))
141
+ end
142
+ end
143
+ output_buffer.concat(form) if Formtastic::Util.rails3?
144
+ output_buffer.should have_tag("form label", /Hello line item name!/)
145
+ end
146
+
147
+
127
148
  # TODO: Add spec for namespaced models?
128
149
 
129
150
  end
@@ -6,7 +6,7 @@ describe "*select: options[:include_blank]" do
6
6
  include FormtasticSpecHelper
7
7
 
8
8
  before do
9
- @output_buffer = ActiveSupport::SafeBuffer.new
9
+ @output_buffer = ''
10
10
  mock_everything
11
11
 
12
12
  @new_post.stub!(:author_id).and_return(nil)
@@ -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
@@ -2,11 +2,11 @@
2
2
  require File.dirname(__FILE__) + '/spec_helper'
3
3
 
4
4
  describe 'SemanticFormBuilder#input' do
5
-
5
+
6
6
  include FormtasticSpecHelper
7
-
7
+
8
8
  before do
9
- @output_buffer = ActiveSupport::SafeBuffer.new
9
+ @output_buffer = ''
10
10
  mock_everything
11
11
  end
12
12
 
@@ -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)
@@ -51,7 +51,7 @@ describe 'SemanticFormBuilder#input' do
51
51
  before do
52
52
  @old_string = ::Formtastic::SemanticFormBuilder.required_string
53
53
  @new_string = ::Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
54
- @new_post.class.should_not_receive(:validators_on)
54
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
55
55
  end
56
56
 
57
57
  after do
@@ -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
 
@@ -79,7 +81,7 @@ describe 'SemanticFormBuilder#input' do
79
81
 
80
82
  before do
81
83
  @string = ::Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
82
- @new_post.class.should_not_receive(:validators_on)
84
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
83
85
  end
84
86
 
85
87
  after do
@@ -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
 
@@ -124,25 +129,34 @@ describe 'SemanticFormBuilder#input' do
124
129
 
125
130
  describe 'and an object was given' do
126
131
 
127
- describe 'and the validation reflection is available' do
132
+ describe 'and the validation reflection plugin is available' do
128
133
 
129
134
  before do
130
- # @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(true)
135
+ ::Post.stub!(:reflect_on_validations_for).and_return([])
136
+ @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
137
+ end
138
+
139
+ unless defined?(Rspec)
140
+ after do
141
+ ::Post.unstub!(:reflect_on_validations_for)
142
+ @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(false)
143
+ end
131
144
  end
132
145
 
133
146
  describe 'and validates_presence_of was called for the method' do
134
147
  it 'should be required' do
135
- @new_post.class.should_receive(:validators_on).with(:title).and_return([
136
- mock('ActiveModel::Validations::PresenceValidator', :kind => :presence, :attributes => [:title], :options => nil)
148
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
149
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil)
137
150
  ])
138
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
139
- mock('ActiveModel::Validations::PresenceValidator', :kind => :presence, :attributes => [:body], :options => {:if => true})
151
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
152
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true})
140
153
  ])
141
154
 
142
- semantic_form_for(@new_post) do |builder|
155
+ form = semantic_form_for(@new_post) do |builder|
143
156
  concat(builder.input(:title))
144
157
  concat(builder.input(:body))
145
158
  end
159
+ output_buffer.concat(form) if Formtastic::Util.rails3?
146
160
  output_buffer.should have_tag('form li.required')
147
161
  output_buffer.should_not have_tag('form li.optional')
148
162
  end
@@ -190,14 +204,16 @@ describe 'SemanticFormBuilder#input' do
190
204
 
191
205
  # TODO make a matcher for this?
192
206
  def should_be_required(options)
193
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
194
- mock('ActiveModel::Validations::PresenceValidator', :kind => :presence, :attributes => [:body], :options => options[:options])
207
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
208
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options])
195
209
  ])
196
210
 
197
- semantic_form_for(@new_post) do |builder|
211
+ form = semantic_form_for(@new_post) do |builder|
198
212
  concat(builder.input(:body))
199
213
  end
200
214
 
215
+ output_buffer.concat(form) if Formtastic::Util.rails3?
216
+
201
217
  if options[:required]
202
218
  output_buffer.should_not have_tag('form li.optional')
203
219
  output_buffer.should have_tag('form li.required')
@@ -209,13 +225,14 @@ describe 'SemanticFormBuilder#input' do
209
225
 
210
226
  describe 'and validates_presence_of was not called for the method' do
211
227
  before do
212
- @new_post.class.should_receive(:validators_on).with(:title).and_return([])
228
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([])
213
229
  end
214
230
 
215
231
  it 'should not be required' do
216
- semantic_form_for(@new_post) do |builder|
232
+ form = semantic_form_for(@new_post) do |builder|
217
233
  concat(builder.input(:title))
218
234
  end
235
+ output_buffer.concat(form) if Formtastic::Util.rails3?
219
236
  output_buffer.should_not have_tag('form li.required')
220
237
  output_buffer.should have_tag('form li.optional')
221
238
  end
@@ -223,15 +240,127 @@ describe 'SemanticFormBuilder#input' do
223
240
 
224
241
  end
225
242
 
243
+ unless defined?(Rspec)
244
+ describe 'and its a ActiveModel' do
245
+ before do
246
+ ::Post.stub!(:validators_on).and_return([])
247
+ @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(true)
248
+ end
249
+
250
+ after do
251
+ ::Post.unstub!(:validators_on)
252
+ @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(false)
253
+ end
254
+ describe 'and validates_presence_of was called for the method' do
255
+ it 'should be required' do
256
+
257
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([
258
+ active_model_presence_validator([:title])
259
+ ])
260
+
261
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
262
+ active_model_presence_validator([:body], {:if => true})
263
+ ])
264
+
265
+ form = semantic_form_for(@new_post) do |builder|
266
+ concat(builder.input(:title))
267
+ concat(builder.input(:body))
268
+ end
269
+ output_buffer.concat(form) if Formtastic::Util.rails3?
270
+ output_buffer.should have_tag('form li.required')
271
+ output_buffer.should_not have_tag('form li.optional')
272
+ end
273
+
274
+ it 'should be not be required if the optional :if condition is not satisifed' do
275
+ should_be_required(:required => false, :options => { :if => false })
276
+ end
277
+
278
+ it 'should not be required if the optional :if proc evaluates to false' do
279
+ should_be_required(:required => false, :options => { :if => proc { |record| false } })
280
+ end
281
+
282
+ it 'should be required if the optional :if proc evaluates to true' do
283
+ should_be_required(:required => true, :options => { :if => proc { |record| true } })
284
+ end
285
+
286
+ it 'should not be required if the optional :unless proc evaluates to true' do
287
+ should_be_required(:required => false, :options => { :unless => proc { |record| true } })
288
+ end
289
+
290
+ it 'should be required if the optional :unless proc evaluates to false' do
291
+ should_be_required(:required => true, :options => { :unless => proc { |record| false } })
292
+ end
293
+
294
+ it 'should be required if the optional :if with a method string evaluates to true' do
295
+ @new_post.should_receive(:required_condition).and_return(true)
296
+ should_be_required(:required => true, :options => { :if => :required_condition })
297
+ end
298
+
299
+ it 'should be required if the optional :if with a method string evaluates to false' do
300
+ @new_post.should_receive(:required_condition).and_return(false)
301
+ should_be_required(:required => false, :options => { :if => :required_condition })
302
+ end
303
+
304
+ it 'should not be required if the optional :unless with a method string evaluates to false' do
305
+ @new_post.should_receive(:required_condition).and_return(false)
306
+ should_be_required(:required => true, :options => { :unless => :required_condition })
307
+ end
308
+
309
+ it 'should be required if the optional :unless with a method string evaluates to true' do
310
+ @new_post.should_receive(:required_condition).and_return(true)
311
+ should_be_required(:required => false, :options => { :unless => :required_condition })
312
+ end
313
+ end
314
+
315
+ # TODO make a matcher for this?
316
+ def should_be_required(options)
317
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
318
+ active_model_presence_validator([:body], options[:options])
319
+ ])
320
+
321
+ form = semantic_form_for(@new_post) do |builder|
322
+ concat(builder.input(:body))
323
+ end
324
+
325
+ output_buffer.concat(form) if Formtastic::Util.rails3?
326
+
327
+ if options[:required]
328
+ output_buffer.should_not have_tag('form li.optional')
329
+ output_buffer.should have_tag('form li.required')
330
+ else
331
+ output_buffer.should have_tag('form li.optional')
332
+ output_buffer.should_not have_tag('form li.required')
333
+ end
334
+ end
335
+
336
+ describe 'and validates_presence_of was not called for the method' do
337
+ before do
338
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([])
339
+ end
340
+
341
+ it 'should not be required' do
342
+ form = semantic_form_for(@new_post) do |builder|
343
+ concat(builder.input(:title))
344
+ end
345
+ output_buffer.concat(form) if Formtastic::Util.rails3?
346
+ output_buffer.should_not have_tag('form li.required')
347
+ output_buffer.should have_tag('form li.optional')
348
+ end
349
+ end
350
+
351
+ end
352
+ end
353
+
226
354
  describe 'and the validation reflection plugin is not available' do
227
355
 
228
356
  it 'should use the default value' do
229
357
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
230
358
  ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
231
359
 
232
- semantic_form_for(@new_post) do |builder|
360
+ form = semantic_form_for(@new_post) do |builder|
233
361
  concat(builder.input(:title))
234
362
  end
363
+ output_buffer.concat(form) if Formtastic::Util.rails3?
235
364
  output_buffer.should_not have_tag('form li.required')
236
365
  output_buffer.should have_tag('form li.optional')
237
366
 
@@ -251,18 +380,20 @@ describe 'SemanticFormBuilder#input' do
251
380
  describe 'when not provided' do
252
381
 
253
382
  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|
383
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
255
384
  concat(builder.input(:anything))
256
385
  end
386
+ output_buffer.concat(form) if Formtastic::Util.rails3?
257
387
  output_buffer.should have_tag('form li.string')
258
388
  end
259
389
 
260
390
  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|
391
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
262
392
  concat(builder.input(:password))
263
393
  concat(builder.input(:password_confirmation))
264
394
  concat(builder.input(:confirm_password))
265
395
  end
396
+ output_buffer.concat(form) if Formtastic::Util.rails3?
266
397
  output_buffer.should have_tag('form li.password', :count => 3)
267
398
  end
268
399
 
@@ -325,15 +456,11 @@ describe 'SemanticFormBuilder#input' do
325
456
  default_input_type(:float).should == :numeric
326
457
  default_input_type(:decimal).should == :numeric
327
458
  end
328
-
459
+
329
460
  it 'should default to :country for :string columns named country' do
330
461
  default_input_type(:string, :country).should == :country
331
462
  end
332
463
 
333
- it 'should default to :currency for :string columns named currency' do
334
- default_input_type(:string, :currency).should == :currency
335
- end
336
-
337
464
  describe 'defaulting to file column' do
338
465
  ::Formtastic::SemanticFormBuilder.file_methods.each do |method|
339
466
  it "should default to :file for attributes that respond to ##{method}" do
@@ -341,7 +468,8 @@ describe 'SemanticFormBuilder#input' do
341
468
  column = mock('column')
342
469
 
343
470
  ::Formtastic::SemanticFormBuilder.file_methods.each do |test|
344
- column.stub!(:respond_to?).with(test).and_return(method == test)
471
+ ### TODO: Check if this is ok
472
+ column.stub!(method).with(test).and_return(method == test)
345
473
  end
346
474
 
347
475
  @new_post.should_receive(method).and_return(column)
@@ -378,60 +506,66 @@ describe 'SemanticFormBuilder#input' do
378
506
  end
379
507
 
380
508
  describe ':label option' do
381
-
509
+
382
510
  describe 'when provided' do
383
511
  it 'should be passed down to the label tag' do
384
- semantic_form_for(@new_post) do |builder|
512
+ form = semantic_form_for(@new_post) do |builder|
385
513
  concat(builder.input(:title, :label => "Kustom"))
386
514
  end
515
+
516
+ output_buffer.concat(form) if Formtastic::Util.rails3?
387
517
  output_buffer.should have_tag("form li label", /Kustom/)
388
518
  end
389
519
 
390
520
  it 'should not generate a label if false' do
391
- semantic_form_for(@new_post) do |builder|
521
+ form = semantic_form_for(@new_post) do |builder|
392
522
  concat(builder.input(:title, :label => false))
393
523
  end
524
+ output_buffer.concat(form) if Formtastic::Util.rails3?
394
525
  output_buffer.should_not have_tag("form li label")
395
526
  end
396
527
 
397
528
  it 'should be dupped if frozen' do
398
- semantic_form_for(@new_post) do |builder|
529
+ form = semantic_form_for(@new_post) do |builder|
399
530
  concat(builder.input(:title, :label => "Kustom".freeze))
400
531
  end
532
+ output_buffer.concat(form) if Formtastic::Util.rails3?
401
533
  output_buffer.should have_tag("form li label", /Kustom/)
402
534
  end
403
535
  end
404
536
 
405
537
  describe 'when not provided' do
406
- describe 'when localized label is provided' do
407
- describe 'and object is given' do
538
+ describe 'when localized label is provided' do
539
+ describe 'and object is given' do
408
540
  describe 'and label_str_method not default' do
409
541
  it 'should render a label with localized label (I18n)' do
410
542
  with_config :label_str_method, :capitalize do
411
543
  @localized_label_text = 'Localized title'
412
544
  @new_post.stub!(:meta_description)
413
545
  @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text)
414
-
415
- semantic_form_for(@new_post) do |builder|
546
+
547
+ form = semantic_form_for(@new_post) do |builder|
416
548
  concat(builder.input(:meta_description))
417
549
  end
418
-
419
- output_buffer.should have_tag('form li label', @localized_label_text)
550
+
551
+ output_buffer.concat(form) if Formtastic::Util.rails3?
552
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
420
553
  end
421
554
  end
422
555
  end
423
556
  end
424
557
  end
425
-
558
+
426
559
  describe 'when localized label is NOT provided' do
427
560
  describe 'and object is not given' do
428
561
  it 'should default the humanized method name, passing it down to the label tag' do
429
562
  ::Formtastic::SemanticFormBuilder.label_str_method = :humanize
430
563
 
431
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
564
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
432
565
  concat(builder.input(:meta_description))
433
566
  end
434
567
 
568
+ output_buffer.concat(form) if Formtastic::Util.rails3?
435
569
  output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
436
570
  end
437
571
  end
@@ -441,29 +575,31 @@ describe 'SemanticFormBuilder#input' do
441
575
  @new_post.stub!(:meta_description) # a two word method name
442
576
  @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
443
577
 
444
- semantic_form_for(@new_post) do |builder|
578
+ form = semantic_form_for(@new_post) do |builder|
445
579
  concat(builder.input(:meta_description))
446
580
  end
447
581
 
582
+ output_buffer.concat(form) if Formtastic::Util.rails3?
448
583
  output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
449
584
  end
450
585
  end
451
-
586
+
452
587
  describe 'and object is given with label_str_method set to :capitalize' do
453
588
  it 'should capitalize method name, passing it down to the label tag' do
454
589
  with_config :label_str_method, :capitalize do
455
590
  @new_post.stub!(:meta_description)
456
-
457
- semantic_form_for(@new_post) do |builder|
591
+
592
+ form = semantic_form_for(@new_post) do |builder|
458
593
  concat(builder.input(:meta_description))
459
594
  end
460
-
595
+
596
+ output_buffer.concat(form) if Formtastic::Util.rails3?
461
597
  output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
462
598
  end
463
599
  end
464
600
  end
465
601
  end
466
-
602
+
467
603
  describe 'when localized label is provided' do
468
604
  before do
469
605
  @localized_label_text = 'Localized title'
@@ -483,11 +619,12 @@ describe 'SemanticFormBuilder#input' do
483
619
  end
484
620
 
485
621
  it 'should render a label with localized label (I18n)' do
486
- semantic_form_for(@new_post) do |builder|
622
+ form = semantic_form_for(@new_post) do |builder|
487
623
  concat(builder.input(:title, :label => true))
488
624
  concat(builder.input(:published, :as => :boolean, :label => true))
489
625
  end
490
- output_buffer.should have_tag('form li label', @localized_label_text)
626
+ output_buffer.concat(form) if Formtastic::Util.rails3?
627
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
491
628
  end
492
629
 
493
630
  it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
@@ -500,15 +637,16 @@ describe 'SemanticFormBuilder#input' do
500
637
  }
501
638
  }
502
639
  }
503
- semantic_form_for(@new_post) do |builder|
640
+ form = semantic_form_for(@new_post) do |builder|
504
641
  concat(builder.input(:title, :label => true))
505
642
  concat(builder.input(:published, :as => :boolean, :label => true))
506
643
  end
507
- output_buffer.should have_tag('form li label', @default_localized_label_text)
644
+ output_buffer.concat(form) if Formtastic::Util.rails3?
645
+ output_buffer.should have_tag('form li label', Regexp.new('^' + @default_localized_label_text))
508
646
  end
509
647
  end
510
648
  end
511
-
649
+
512
650
  end
513
651
 
514
652
  describe ':hint option' do
@@ -516,9 +654,10 @@ describe 'SemanticFormBuilder#input' do
516
654
  describe 'when provided' do
517
655
  it 'should be passed down to the paragraph tag' do
518
656
  hint_text = "this is the title of the post"
519
- semantic_form_for(@new_post) do |builder|
657
+ form = semantic_form_for(@new_post) do |builder|
520
658
  concat(builder.input(:title, :hint => hint_text))
521
659
  end
660
+ output_buffer.concat(form) if Formtastic::Util.rails3?
522
661
  output_buffer.should have_tag("form li p.inline-hints", hint_text)
523
662
  end
524
663
  end
@@ -532,53 +671,58 @@ describe 'SemanticFormBuilder#input' do
532
671
  :formtastic => {
533
672
  :hints => {
534
673
  :title => @default_localized_hint_text,
535
- :post => {
536
- :title => @localized_hint_text
537
- }
538
674
  }
539
675
  }
540
676
  ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
541
677
  end
542
678
 
679
+ after do
680
+ ::I18n.backend.reload!
681
+ end
682
+
543
683
  describe 'when provided value (hint value) is set to TRUE' do
544
684
  it 'should render a hint paragraph containing a localized hint (I18n)' do
545
- semantic_form_for(@new_post) do |builder|
546
- concat(builder.input(:title, :hint => true))
547
- end
548
- output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
549
- end
550
-
551
- it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
552
685
  ::I18n.backend.store_translations :en,
553
686
  :formtastic => {
554
687
  :hints => {
555
688
  :post => {
556
- :title => nil
689
+ :title => @localized_hint_text
557
690
  }
558
691
  }
559
692
  }
560
- semantic_form_for(@new_post) do |builder|
693
+ form = semantic_form_for(@new_post) do |builder|
561
694
  concat(builder.input(:title, :hint => true))
562
695
  end
696
+ output_buffer.concat(form) if Formtastic::Util.rails3?
697
+ output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
698
+ end
699
+
700
+ it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
701
+ form = semantic_form_for(@new_post) do |builder|
702
+ concat(builder.input(:title, :hint => true))
703
+ end
704
+ output_buffer.concat(form) if Formtastic::Util.rails3?
563
705
  output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
564
706
  end
565
707
  end
566
-
708
+
567
709
  describe 'when provided value (label value) is set to FALSE' do
568
710
  it 'should not render a hint paragraph' do
569
- semantic_form_for(@new_post) do |builder|
711
+ form = semantic_form_for(@new_post) do |builder|
570
712
  concat(builder.input(:title, :hint => false))
571
713
  end
714
+ output_buffer.concat(form) if Formtastic::Util.rails3?
572
715
  output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
573
716
  end
574
717
  end
575
718
  end
576
-
719
+
577
720
  describe 'when localized hint (I18n) is not provided' do
578
721
  it 'should not render a hint paragraph' do
579
- semantic_form_for(@new_post) do |builder|
722
+ form = semantic_form_for(@new_post) do |builder|
580
723
  concat(builder.input(:title))
581
724
  end
725
+ output_buffer.concat(form) if Formtastic::Util.rails3?
582
726
  output_buffer.should_not have_tag('form li p.inline-hints')
583
727
  end
584
728
  end
@@ -590,25 +734,28 @@ describe 'SemanticFormBuilder#input' do
590
734
 
591
735
  describe 'when provided' do
592
736
  it 'should be passed down to the li tag' do
593
- semantic_form_for(@new_post) do |builder|
737
+ form = semantic_form_for(@new_post) do |builder|
594
738
  concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
595
739
  end
740
+ output_buffer.concat(form) if Formtastic::Util.rails3?
596
741
  output_buffer.should have_tag("form li#another_id")
597
742
  end
598
743
 
599
744
  it 'should append given classes to li default classes' do
600
- semantic_form_for(@new_post) do |builder|
745
+ form = semantic_form_for(@new_post) do |builder|
601
746
  concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
602
747
  end
748
+ output_buffer.concat(form) if Formtastic::Util.rails3?
603
749
  output_buffer.should have_tag("form li.string")
604
750
  output_buffer.should have_tag("form li.required")
605
751
  output_buffer.should have_tag("form li.another_class")
606
752
  end
607
753
 
608
754
  it 'should allow classes to be an array' do
609
- semantic_form_for(@new_post) do |builder|
755
+ form = semantic_form_for(@new_post) do |builder|
610
756
  concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
611
757
  end
758
+ output_buffer.concat(form) if Formtastic::Util.rails3?
612
759
  output_buffer.should have_tag("form li.string")
613
760
  output_buffer.should have_tag("form li.my_class")
614
761
  output_buffer.should have_tag("form li.another_class")
@@ -617,9 +764,10 @@ describe 'SemanticFormBuilder#input' do
617
764
 
618
765
  describe 'when not provided' do
619
766
  it 'should use default id and class' do
620
- semantic_form_for(@new_post) do |builder|
767
+ form = semantic_form_for(@new_post) do |builder|
621
768
  concat(builder.input(:title))
622
769
  end
770
+ output_buffer.concat(form) if Formtastic::Util.rails3?
623
771
  output_buffer.should have_tag("form li#post_title_input")
624
772
  output_buffer.should have_tag("form li.string")
625
773
  end