formtastic 1.2.5 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.yardopts +1 -0
- data/CHANGELOG +279 -0
- data/Gemfile +3 -0
- data/README.textile +155 -172
- data/RELEASE_PROCESS +7 -0
- data/Rakefile +52 -0
- data/app/assets/stylesheets/formtastic.css +275 -0
- data/app/assets/stylesheets/formtastic_ie6.css +27 -0
- data/app/assets/stylesheets/formtastic_ie7.css +17 -0
- data/formtastic.gemspec +51 -0
- data/lib/formtastic.rb +21 -1960
- data/lib/formtastic/engine.rb +7 -0
- data/lib/formtastic/form_builder.rb +83 -0
- data/lib/formtastic/helpers.rb +16 -0
- data/lib/formtastic/helpers/buttons_helper.rb +277 -0
- data/lib/formtastic/helpers/errors_helper.rb +113 -0
- data/lib/formtastic/helpers/fieldset_wrapper.rb +75 -0
- data/lib/formtastic/helpers/file_column_detection.rb +16 -0
- data/lib/formtastic/helpers/form_helper.rb +198 -0
- data/lib/formtastic/helpers/input_helper.rb +366 -0
- data/lib/formtastic/helpers/inputs_helper.rb +392 -0
- data/lib/formtastic/helpers/reflection.rb +33 -0
- data/lib/formtastic/helpers/semantic_form_helper.rb +11 -0
- data/lib/formtastic/html_attributes.rb +21 -0
- data/lib/formtastic/i18n.rb +1 -0
- data/lib/formtastic/inputs.rb +31 -0
- data/lib/formtastic/inputs/base.rb +61 -0
- data/lib/formtastic/inputs/base/associations.rb +31 -0
- data/lib/formtastic/inputs/base/choices.rb +103 -0
- data/lib/formtastic/inputs/base/collections.rb +94 -0
- data/lib/formtastic/inputs/base/database.rb +17 -0
- data/lib/formtastic/inputs/base/errors.rb +58 -0
- data/lib/formtastic/inputs/base/fileish.rb +23 -0
- data/lib/formtastic/inputs/base/grouped_collections.rb +77 -0
- data/lib/formtastic/inputs/base/hints.rb +31 -0
- data/lib/formtastic/inputs/base/html.rb +52 -0
- data/lib/formtastic/inputs/base/labelling.rb +55 -0
- data/lib/formtastic/inputs/base/naming.rb +42 -0
- data/lib/formtastic/inputs/base/options.rb +18 -0
- data/lib/formtastic/inputs/base/stringish.rb +35 -0
- data/lib/formtastic/inputs/base/timeish.rb +128 -0
- data/lib/formtastic/inputs/base/validations.rb +166 -0
- data/lib/formtastic/inputs/base/wrapping.rb +40 -0
- data/lib/formtastic/inputs/boolean_input.rb +96 -0
- data/lib/formtastic/inputs/check_boxes_input.rb +179 -0
- data/lib/formtastic/inputs/country_input.rb +66 -0
- data/lib/formtastic/inputs/date_input.rb +14 -0
- data/lib/formtastic/inputs/datetime_input.rb +9 -0
- data/lib/formtastic/inputs/email_input.rb +40 -0
- data/lib/formtastic/inputs/file_input.rb +42 -0
- data/lib/formtastic/inputs/hidden_input.rb +66 -0
- data/lib/formtastic/inputs/number_input.rb +118 -0
- data/lib/formtastic/inputs/numeric_input.rb +21 -0
- data/lib/formtastic/inputs/password_input.rb +40 -0
- data/lib/formtastic/inputs/phone_input.rb +41 -0
- data/lib/formtastic/inputs/radio_input.rb +157 -0
- data/lib/formtastic/inputs/range_input.rb +119 -0
- data/lib/formtastic/inputs/search_input.rb +40 -0
- data/lib/formtastic/inputs/select_input.rb +210 -0
- data/lib/formtastic/inputs/string_input.rb +34 -0
- data/lib/formtastic/inputs/text_input.rb +47 -0
- data/lib/formtastic/inputs/time_input.rb +14 -0
- data/lib/formtastic/inputs/time_zone_input.rb +48 -0
- data/lib/formtastic/inputs/url_input.rb +40 -0
- data/lib/formtastic/localized_string.rb +105 -0
- data/lib/formtastic/railtie.rb +5 -7
- data/lib/formtastic/semantic_form_builder.rb +11 -0
- data/lib/formtastic/util.rb +6 -19
- data/lib/formtastic/version.rb +3 -0
- data/lib/generators/formtastic/install/install_generator.rb +28 -6
- data/lib/generators/templates/_form.html.erb +10 -4
- data/lib/generators/templates/_form.html.haml +8 -4
- data/lib/generators/templates/formtastic.rb +25 -32
- data/lib/locale/en.yml +1 -0
- data/lib/tasks/verify_rcov.rb +44 -0
- data/sample/basic_inputs.html +182 -0
- data/sample/config.ru +69 -0
- data/sample/index.html +14 -0
- data/spec/builder/custom_builder_spec.rb +109 -0
- data/spec/builder/error_proc_spec.rb +27 -0
- data/spec/builder/errors_spec.rb +193 -0
- data/spec/builder/semantic_fields_for_spec.rb +88 -0
- data/spec/helpers/buttons_helper_spec.rb +150 -0
- data/spec/helpers/commit_button_helper_spec.rb +470 -0
- data/spec/helpers/form_helper_spec.rb +135 -0
- data/spec/helpers/input_helper_spec.rb +837 -0
- data/spec/helpers/inputs_helper_spec.rb +562 -0
- data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
- data/spec/i18n_spec.rb +199 -0
- data/spec/inputs/boolean_input_spec.rb +184 -0
- data/spec/inputs/check_boxes_input_spec.rb +375 -0
- data/spec/inputs/country_input_spec.rb +133 -0
- data/spec/inputs/custom_input_spec.rb +52 -0
- data/spec/inputs/date_input_spec.rb +110 -0
- data/spec/inputs/datetime_input_spec.rb +115 -0
- data/spec/inputs/email_input_spec.rb +55 -0
- data/spec/inputs/file_input_spec.rb +59 -0
- data/spec/inputs/hidden_input_spec.rb +120 -0
- data/spec/inputs/include_blank_spec.rb +70 -0
- data/spec/inputs/label_spec.rb +104 -0
- data/spec/inputs/number_input_spec.rb +487 -0
- data/spec/inputs/numeric_input_spec.rb +41 -0
- data/spec/inputs/password_input_spec.rb +69 -0
- data/spec/inputs/phone_input_spec.rb +55 -0
- data/spec/inputs/placeholder_spec.rb +71 -0
- data/spec/inputs/radio_input_spec.rb +234 -0
- data/spec/inputs/range_input_spec.rb +477 -0
- data/spec/inputs/search_input_spec.rb +55 -0
- data/spec/inputs/select_input_spec.rb +545 -0
- data/spec/inputs/string_input_spec.rb +163 -0
- data/spec/inputs/text_input_spec.rb +158 -0
- data/spec/inputs/time_input_spec.rb +155 -0
- data/spec/inputs/time_zone_input_spec.rb +87 -0
- data/spec/inputs/url_input_spec.rb +55 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +361 -0
- data/spec/support/custom_macros.rb +656 -0
- data/spec/support/deferred_garbage_collection.rb +21 -0
- data/spec/support/deprecation.rb +6 -0
- data/spec/support/test_environment.rb +30 -0
- metadata +306 -88
- data/generators/form/USAGE +0 -16
- data/generators/form/form_generator.rb +0 -111
- data/generators/formtastic/formtastic_generator.rb +0 -26
- data/init.rb +0 -5
- data/lib/formtastic/layout_helper.rb +0 -12
- data/lib/generators/formtastic/form/form_generator.rb +0 -84
- data/lib/generators/templates/formtastic.css +0 -145
- data/lib/generators/templates/formtastic_changes.css +0 -14
- data/lib/generators/templates/rails2/_form.html.erb +0 -5
- data/lib/generators/templates/rails2/_form.html.haml +0 -4
- data/rails/init.rb +0 -2
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'FormHelper' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#semantic_form_for' do
|
14
|
+
|
15
|
+
it 'yields an instance of Formtastic::FormBuilder' do
|
16
|
+
semantic_form_for(@new_post, :url => '/hello') do |builder|
|
17
|
+
builder.class.should == Formtastic::FormBuilder
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'adds a class of "formtastic" to the generated form' do
|
22
|
+
concat(semantic_form_for(@new_post, :url => '/hello') do |builder|
|
23
|
+
end)
|
24
|
+
output_buffer.should have_tag("form.formtastic")
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'adds a class of "xyz" to the generated form' do
|
28
|
+
Formtastic::Helpers::FormHelper.default_form_class = 'xyz'
|
29
|
+
concat(semantic_form_for(::Post.new, :as => :post, :url => '/hello') do |builder|
|
30
|
+
end)
|
31
|
+
output_buffer.should have_tag("form.xyz")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'adds class matching the object name to the generated form when a symbol is provided' do
|
35
|
+
concat(semantic_form_for(@new_post, :url => '/hello') do |builder|
|
36
|
+
end)
|
37
|
+
output_buffer.should have_tag("form.post")
|
38
|
+
|
39
|
+
concat(semantic_form_for(:project, :url => '/hello') do |builder|
|
40
|
+
end)
|
41
|
+
output_buffer.should have_tag("form.project")
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'adds class matching the :as option when provided' do
|
45
|
+
concat(semantic_form_for(@new_post, :as => :message, :url => '/hello') do |builder|
|
46
|
+
end)
|
47
|
+
output_buffer.should have_tag("form.message")
|
48
|
+
|
49
|
+
concat(semantic_form_for([:admins, @new_post], :as => :message, :url => '/hello') do |builder|
|
50
|
+
end)
|
51
|
+
output_buffer.should have_tag("form.message")
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'adds class matching the object\'s class to the generated form when an object is provided' do
|
55
|
+
concat(semantic_form_for(@new_post) do |builder|
|
56
|
+
end)
|
57
|
+
output_buffer.should have_tag("form.post")
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'adds a namespaced class to the generated form' do
|
61
|
+
concat(semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder|
|
62
|
+
end)
|
63
|
+
output_buffer.should have_tag("form.namespaced_post")
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'allows :html options' do
|
67
|
+
before(:each) do
|
68
|
+
concat(semantic_form_for(@new_post, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
|
69
|
+
end)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'to add a id of "something-special" to generated form' do
|
73
|
+
output_buffer.should have_tag("form#something-special")
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'to add a class of "something-extra" to generated form' do
|
77
|
+
output_buffer.should have_tag("form.something-extra")
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'to add enctype="multipart/form-data"' do
|
81
|
+
output_buffer.should have_tag('form[@enctype="multipart/form-data"]')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'can be called with a resource-oriented style' do
|
86
|
+
semantic_form_for(@new_post) do |builder|
|
87
|
+
builder.object.class.should == ::Post
|
88
|
+
builder.object_name.should == "post"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'can be called with a generic style and instance variable' do
|
93
|
+
semantic_form_for(@new_post, :as => :post, :url => new_post_path) do |builder|
|
94
|
+
builder.object.class.should == ::Post
|
95
|
+
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'can be called with a generic style and inline object' do
|
100
|
+
semantic_form_for(@new_post, :url => new_post_path) do |builder|
|
101
|
+
builder.object.class.should == ::Post
|
102
|
+
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "with :builder option" do
|
107
|
+
it "yields an instance of the given builder" do
|
108
|
+
class MyAwesomeCustomBuilder < Formtastic::FormBuilder
|
109
|
+
end
|
110
|
+
semantic_form_for(@new_post, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
|
111
|
+
builder.class.should == MyAwesomeCustomBuilder
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe 'with :namespace option' do
|
117
|
+
it "should set the custom_namespace" do
|
118
|
+
semantic_form_for(@new_post, :namespace => 'context2') do |builder|
|
119
|
+
builder.custom_namespace == 'context2'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
describe '#semantic_fields_for' do
|
127
|
+
it 'yields an instance of Formtastic::FormBuilder' do
|
128
|
+
semantic_fields_for(@new_post) do |builder|
|
129
|
+
builder.class.should.kind_of?(Formtastic::FormBuilder)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
@@ -0,0 +1,837 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Formtastic::FormBuilder#input' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
|
12
|
+
@errors = mock('errors')
|
13
|
+
@errors.stub!(:[]).and_return([])
|
14
|
+
@new_post.stub!(:errors).and_return(@errors)
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
::I18n.backend.reload!
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'arguments and options' do
|
22
|
+
|
23
|
+
it 'should require the first argument (the method on form\'s object)' do
|
24
|
+
lambda {
|
25
|
+
concat(semantic_form_for(@new_post) do |builder|
|
26
|
+
concat(builder.input()) # no args passed in at all
|
27
|
+
end)
|
28
|
+
}.should raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ':required option' do
|
32
|
+
|
33
|
+
describe 'when true' do
|
34
|
+
|
35
|
+
it 'should set a "required" class' do
|
36
|
+
with_config :required_string, " required yo!" do
|
37
|
+
concat(semantic_form_for(@new_post) do |builder|
|
38
|
+
concat(builder.input(:title, :required => true))
|
39
|
+
end)
|
40
|
+
output_buffer.should_not have_tag('form li.optional')
|
41
|
+
output_buffer.should have_tag('form li.required')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should append the "required" string to the label' do
|
46
|
+
with_config :required_string, " required yo!" do
|
47
|
+
concat(semantic_form_for(@new_post) do |builder|
|
48
|
+
concat(builder.input(:title, :required => true))
|
49
|
+
end)
|
50
|
+
output_buffer.should have_tag('form li.required label', /required yo/)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'when false' do
|
56
|
+
|
57
|
+
before do
|
58
|
+
@string = Formtastic::FormBuilder.optional_string = " optional yo!" # ensure there's something in the string
|
59
|
+
@new_post.class.should_not_receive(:reflect_on_all_validations)
|
60
|
+
end
|
61
|
+
|
62
|
+
after do
|
63
|
+
Formtastic::FormBuilder.optional_string = ''
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should set an "optional" class' do
|
67
|
+
concat(semantic_form_for(@new_post) do |builder|
|
68
|
+
concat(builder.input(:title, :required => false))
|
69
|
+
end)
|
70
|
+
output_buffer.should_not have_tag('form li.required')
|
71
|
+
output_buffer.should have_tag('form li.optional')
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should set and "optional" class also when there is presence validator' do
|
75
|
+
@new_post.class.should_receive(:validators_on).with(:title).any_number_of_times.and_return([
|
76
|
+
active_model_presence_validator([:title])
|
77
|
+
])
|
78
|
+
concat(semantic_form_for(@new_post) do |builder|
|
79
|
+
concat(builder.input(:title, :required => false))
|
80
|
+
end)
|
81
|
+
output_buffer.should_not have_tag('form li.required')
|
82
|
+
output_buffer.should have_tag('form li.optional')
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should append the "optional" string to the label' do
|
86
|
+
concat(semantic_form_for(@new_post) do |builder|
|
87
|
+
concat(builder.input(:title, :required => false))
|
88
|
+
end)
|
89
|
+
output_buffer.should have_tag('form li.optional label', /#{@string}$/)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
describe 'when not provided' do
|
95
|
+
|
96
|
+
describe 'and an object was not given' do
|
97
|
+
|
98
|
+
it 'should use the default value' do
|
99
|
+
Formtastic::FormBuilder.all_fields_required_by_default.should == true
|
100
|
+
Formtastic::FormBuilder.all_fields_required_by_default = false
|
101
|
+
|
102
|
+
concat(semantic_form_for(:project, :url => 'http://test.host/') do |builder|
|
103
|
+
concat(builder.input(:title))
|
104
|
+
end)
|
105
|
+
output_buffer.should_not have_tag('form li.required')
|
106
|
+
output_buffer.should have_tag('form li.optional')
|
107
|
+
|
108
|
+
Formtastic::FormBuilder.all_fields_required_by_default = true
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'and an object with :validators_on was given (ActiveModel, Active Resource)' do
|
114
|
+
before do
|
115
|
+
@new_post.stub!(:class).and_return(::PostModel)
|
116
|
+
end
|
117
|
+
|
118
|
+
after do
|
119
|
+
@new_post.stub!(:class).and_return(::Post)
|
120
|
+
end
|
121
|
+
describe 'and validates_presence_of was called for the method' do
|
122
|
+
it 'should be required' do
|
123
|
+
|
124
|
+
@new_post.class.should_receive(:validators_on).with(:title).any_number_of_times.and_return([
|
125
|
+
active_model_presence_validator([:title])
|
126
|
+
])
|
127
|
+
|
128
|
+
@new_post.class.should_receive(:validators_on).with(:body).any_number_of_times.and_return([
|
129
|
+
active_model_presence_validator([:body], {:if => true})
|
130
|
+
])
|
131
|
+
|
132
|
+
concat(semantic_form_for(@new_post) do |builder|
|
133
|
+
concat(builder.input(:title))
|
134
|
+
concat(builder.input(:body))
|
135
|
+
end)
|
136
|
+
output_buffer.should have_tag('form li.required')
|
137
|
+
output_buffer.should_not have_tag('form li.optional')
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should be not be required if the optional :if condition is not satisifed' do
|
141
|
+
should_be_required(:required => false, :options => { :if => false })
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should not be required if the optional :if proc evaluates to false' do
|
145
|
+
should_be_required(:required => false, :options => { :if => proc { |record| false } })
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should be required if the optional :if proc evaluates to true' do
|
149
|
+
should_be_required(:required => true, :options => { :if => proc { |record| true } })
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should not be required if the optional :unless proc evaluates to true' do
|
153
|
+
should_be_required(:required => false, :options => { :unless => proc { |record| true } })
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'should be required if the optional :unless proc evaluates to false' do
|
157
|
+
should_be_required(:required => true, :options => { :unless => proc { |record| false } })
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'should be required if the optional :if with a method string evaluates to true' do
|
161
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
162
|
+
should_be_required(:required => true, :options => { :if => :required_condition })
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should be required if the optional :if with a method string evaluates to false' do
|
166
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
167
|
+
should_be_required(:required => false, :options => { :if => :required_condition })
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should not be required if the optional :unless with a method string evaluates to false' do
|
171
|
+
@new_post.should_receive(:required_condition).and_return(false)
|
172
|
+
should_be_required(:required => true, :options => { :unless => :required_condition })
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'should be required if the optional :unless with a method string evaluates to true' do
|
176
|
+
@new_post.should_receive(:required_condition).and_return(true)
|
177
|
+
should_be_required(:required => false, :options => { :unless => :required_condition })
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe 'and validates_inclusion_of was called for the method' do
|
182
|
+
it 'should be required' do
|
183
|
+
@new_post.class.should_receive(:validators_on).with(:published).any_number_of_times.and_return([
|
184
|
+
active_model_inclusion_validator([:published], {:in => [false, true]})
|
185
|
+
])
|
186
|
+
|
187
|
+
concat(semantic_form_for(@new_post) do |builder|
|
188
|
+
concat(builder.input(:published))
|
189
|
+
end)
|
190
|
+
output_buffer.should have_tag('form li.required')
|
191
|
+
output_buffer.should_not have_tag('form li.optional')
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'should not be required if allow_blank is true' do
|
195
|
+
@new_post.class.should_receive(:validators_on).with(:published).any_number_of_times.and_return([
|
196
|
+
active_model_inclusion_validator([:published], {:in => [false, true], :allow_blank => true})
|
197
|
+
])
|
198
|
+
|
199
|
+
concat(semantic_form_for(@new_post) do |builder|
|
200
|
+
concat(builder.input(:published))
|
201
|
+
end)
|
202
|
+
output_buffer.should_not have_tag('form li.required')
|
203
|
+
output_buffer.should have_tag('form li.optional')
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# TODO make a matcher for this?
|
208
|
+
def should_be_required(options)
|
209
|
+
@new_post.class.stub!(:validators_on).with(:body).and_return([
|
210
|
+
active_model_presence_validator([:body], options[:options])
|
211
|
+
])
|
212
|
+
|
213
|
+
concat(semantic_form_for(@new_post) do |builder|
|
214
|
+
concat(builder.input(:body))
|
215
|
+
end)
|
216
|
+
|
217
|
+
if options[:required]
|
218
|
+
output_buffer.should_not have_tag('form li.optional')
|
219
|
+
output_buffer.should have_tag('form li.required')
|
220
|
+
else
|
221
|
+
output_buffer.should have_tag('form li.optional')
|
222
|
+
output_buffer.should_not have_tag('form li.required')
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# TODO JF reversed this during refactor, need to make sure
|
227
|
+
describe 'and there are no requirement validations on the method' do
|
228
|
+
before do
|
229
|
+
@new_post.class.should_receive(:validators_on).with(:title).and_return([])
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'should use the default value' do
|
233
|
+
concat(semantic_form_for(@new_post) do |builder|
|
234
|
+
concat(builder.input(:title))
|
235
|
+
end)
|
236
|
+
output_buffer.should have_tag('form li.required')
|
237
|
+
output_buffer.should_not have_tag('form li.optional')
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
describe 'and an object without :validators_on' do
|
244
|
+
|
245
|
+
it 'should use the default value' do
|
246
|
+
Formtastic::FormBuilder.all_fields_required_by_default.should == true
|
247
|
+
Formtastic::FormBuilder.all_fields_required_by_default = false
|
248
|
+
|
249
|
+
concat(semantic_form_for(@new_post) do |builder|
|
250
|
+
concat(builder.input(:title))
|
251
|
+
end)
|
252
|
+
output_buffer.should_not have_tag('form li.required')
|
253
|
+
output_buffer.should have_tag('form li.optional')
|
254
|
+
|
255
|
+
Formtastic::FormBuilder.all_fields_required_by_default = true
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
describe ':as option' do
|
265
|
+
|
266
|
+
describe 'when not provided' do
|
267
|
+
|
268
|
+
it 'should default to a string for forms without objects unless column is password' do
|
269
|
+
concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
270
|
+
concat(builder.input(:anything))
|
271
|
+
end)
|
272
|
+
output_buffer.should have_tag('form li.string')
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'should default to password for forms without objects if column is password' do
|
276
|
+
concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
277
|
+
concat(builder.input(:password))
|
278
|
+
concat(builder.input(:password_confirmation))
|
279
|
+
concat(builder.input(:confirm_password))
|
280
|
+
end)
|
281
|
+
output_buffer.should have_tag('form li.password', :count => 3)
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do
|
285
|
+
@new_post.stub!(:method_without_a_database_column)
|
286
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
287
|
+
default_input_type(nil, :method_without_a_database_column).should == :string
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do
|
291
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
292
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
293
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'should default to :password for methods on objects that don\'t respond to "column_for_attribute" but "password" is in the method name' do
|
297
|
+
@new_post.stub!(:password_method_without_a_database_column)
|
298
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
299
|
+
default_input_type(nil, :password_method_without_a_database_column).should == :password
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'should default to :number for "integer" column with name ending in "_id"' do
|
303
|
+
@new_post.stub!(:aws_instance_id)
|
304
|
+
@new_post.stub!(:column_for_attribute).with(:aws_instance_id).and_return(mock('column', :type => :integer))
|
305
|
+
default_input_type(:integer, :aws_instance_id).should == :number
|
306
|
+
end
|
307
|
+
|
308
|
+
it 'should default to :select for associations' do
|
309
|
+
@new_post.class.stub!(:reflect_on_association).with(:user_id).and_return(mock('ActiveRecord::Reflection::AssociationReflection'))
|
310
|
+
@new_post.class.stub!(:reflect_on_association).with(:section_id).and_return(mock('ActiveRecord::Reflection::AssociationReflection'))
|
311
|
+
default_input_type(:integer, :user_id).should == :select
|
312
|
+
default_input_type(:integer, :section_id).should == :select
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'should default to :password for :string column types with "password" in the method name' do
|
316
|
+
default_input_type(:string, :password).should == :password
|
317
|
+
default_input_type(:string, :hashed_password).should == :password
|
318
|
+
default_input_type(:string, :password_hash).should == :password
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'should default to :text for :text column types' do
|
322
|
+
default_input_type(:text).should == :text
|
323
|
+
end
|
324
|
+
|
325
|
+
it 'should default to :date for :date column types' do
|
326
|
+
default_input_type(:date).should == :date
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'should default to :datetime for :datetime and :timestamp column types' do
|
330
|
+
default_input_type(:datetime).should == :datetime
|
331
|
+
default_input_type(:timestamp).should == :datetime
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'should default to :time for :time column types' do
|
335
|
+
default_input_type(:time).should == :time
|
336
|
+
end
|
337
|
+
|
338
|
+
it 'should default to :boolean for :boolean column types' do
|
339
|
+
default_input_type(:boolean).should == :boolean
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'should default to :string for :string column types' do
|
343
|
+
default_input_type(:string).should == :string
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'should default to :number for :integer, :float and :decimal column types' do
|
347
|
+
default_input_type(:integer).should == :number
|
348
|
+
default_input_type(:float).should == :number
|
349
|
+
default_input_type(:decimal).should == :number
|
350
|
+
end
|
351
|
+
|
352
|
+
it 'should default to :country for :string columns named country' do
|
353
|
+
default_input_type(:string, :country).should == :country
|
354
|
+
end
|
355
|
+
|
356
|
+
it 'should default to :email for :string columns matching email' do
|
357
|
+
default_input_type(:string, :email).should == :email
|
358
|
+
default_input_type(:string, :customer_email).should == :email
|
359
|
+
default_input_type(:string, :email_work).should == :email
|
360
|
+
end
|
361
|
+
|
362
|
+
it 'should default to :url for :string columns named url or website' do
|
363
|
+
default_input_type(:string, :url).should == :url
|
364
|
+
default_input_type(:string, :website).should == :url
|
365
|
+
default_input_type(:string, :my_url).should == :url
|
366
|
+
default_input_type(:string, :hurl).should_not == :url
|
367
|
+
end
|
368
|
+
|
369
|
+
it 'should default to :phone for :string columns named phone or fax' do
|
370
|
+
default_input_type(:string, :phone).should == :phone
|
371
|
+
default_input_type(:string, :fax).should == :phone
|
372
|
+
end
|
373
|
+
|
374
|
+
it 'should default to :search for :string columns named search' do
|
375
|
+
default_input_type(:string, :search).should == :search
|
376
|
+
end
|
377
|
+
|
378
|
+
describe 'defaulting to file column' do
|
379
|
+
Formtastic::FormBuilder.file_methods.each do |method|
|
380
|
+
it "should default to :file for attributes that respond to ##{method}" do
|
381
|
+
@new_post.stub!(:column_for_attribute).and_return(nil)
|
382
|
+
column = mock('column')
|
383
|
+
|
384
|
+
Formtastic::FormBuilder.file_methods.each do |test|
|
385
|
+
### TODO: Check if this is ok
|
386
|
+
column.stub!(method).with(test).and_return(method == test)
|
387
|
+
end
|
388
|
+
|
389
|
+
@new_post.should_receive(method).and_return(column)
|
390
|
+
|
391
|
+
semantic_form_for(@new_post) do |builder|
|
392
|
+
builder.send(:default_input_type, method).should == :file
|
393
|
+
end
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'should call the corresponding input class with .to_html' do
|
401
|
+
[:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden, :string, :password, :number, :text, :file].each do |input_style|
|
402
|
+
@new_post.stub!(:generic_column_name)
|
403
|
+
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
|
404
|
+
semantic_form_for(@new_post) do |builder|
|
405
|
+
input_instance = mock('Input instance')
|
406
|
+
input_class = "#{input_style.to_s}_input".classify
|
407
|
+
input_constant = "Formtastic::Inputs::#{input_class}".constantize
|
408
|
+
|
409
|
+
input_constant.should_receive(:new).and_return(input_instance)
|
410
|
+
input_instance.should_receive(:to_html).and_return("some HTML")
|
411
|
+
|
412
|
+
concat(builder.input(:generic_column_name, :as => input_style))
|
413
|
+
end
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
describe ':label option' do
|
420
|
+
|
421
|
+
describe 'when provided' do
|
422
|
+
it 'should be passed down to the label tag' do
|
423
|
+
concat(semantic_form_for(@new_post) do |builder|
|
424
|
+
concat(builder.input(:title, :label => "Kustom"))
|
425
|
+
end)
|
426
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
427
|
+
end
|
428
|
+
|
429
|
+
it 'should not generate a label if false' do
|
430
|
+
concat(semantic_form_for(@new_post) do |builder|
|
431
|
+
concat(builder.input(:title, :label => false))
|
432
|
+
end)
|
433
|
+
output_buffer.should_not have_tag("form li label")
|
434
|
+
end
|
435
|
+
|
436
|
+
it 'should be dupped if frozen' do
|
437
|
+
concat(semantic_form_for(@new_post) do |builder|
|
438
|
+
concat(builder.input(:title, :label => "Kustom".freeze))
|
439
|
+
end)
|
440
|
+
output_buffer.should have_tag("form li label", /Kustom/)
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
describe 'when not provided' do
|
445
|
+
describe 'when localized label is provided' do
|
446
|
+
describe 'and object is given' do
|
447
|
+
describe 'and label_str_method not :humanize' do
|
448
|
+
it 'should render a label with localized text and not apply the label_str_method' do
|
449
|
+
with_config :label_str_method, :reverse do
|
450
|
+
@localized_label_text = 'Localized title'
|
451
|
+
@new_post.stub!(:meta_description)
|
452
|
+
::I18n.backend.store_translations :en,
|
453
|
+
:formtastic => {
|
454
|
+
:labels => {
|
455
|
+
:meta_description => @localized_label_text
|
456
|
+
}
|
457
|
+
}
|
458
|
+
|
459
|
+
concat(semantic_form_for(@new_post) do |builder|
|
460
|
+
concat(builder.input(:meta_description))
|
461
|
+
end)
|
462
|
+
output_buffer.should have_tag('form li label', /Localized title/)
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
describe 'when localized label is NOT provided' do
|
470
|
+
describe 'and object is not given' do
|
471
|
+
it 'should default the humanized method name, passing it down to the label tag' do
|
472
|
+
::I18n.backend.store_translations :en, :formtastic => {}
|
473
|
+
with_config :label_str_method, :humanize do
|
474
|
+
concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
475
|
+
concat(builder.input(:meta_description))
|
476
|
+
end)
|
477
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
478
|
+
end
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
describe 'and object is given' do
|
483
|
+
it 'should delegate the label logic to class human attribute name and pass it down to the label tag' do
|
484
|
+
@new_post.stub!(:meta_description) # a two word method name
|
485
|
+
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
|
486
|
+
|
487
|
+
concat(semantic_form_for(@new_post) do |builder|
|
488
|
+
concat(builder.input(:meta_description))
|
489
|
+
end)
|
490
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
describe 'and object is given with label_str_method set to :capitalize' do
|
495
|
+
it 'should capitalize method name, passing it down to the label tag' do
|
496
|
+
with_config :label_str_method, :capitalize do
|
497
|
+
@new_post.stub!(:meta_description)
|
498
|
+
|
499
|
+
concat(semantic_form_for(@new_post) do |builder|
|
500
|
+
concat(builder.input(:meta_description))
|
501
|
+
end)
|
502
|
+
output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
describe 'when localized label is provided' do
|
509
|
+
before do
|
510
|
+
@localized_label_text = 'Localized title'
|
511
|
+
@default_localized_label_text = 'Default localized title'
|
512
|
+
::I18n.backend.store_translations :en,
|
513
|
+
:formtastic => {
|
514
|
+
:labels => {
|
515
|
+
:title => @default_localized_label_text,
|
516
|
+
:published => @default_localized_label_text,
|
517
|
+
:post => {
|
518
|
+
:title => @localized_label_text,
|
519
|
+
:published => @default_localized_label_text
|
520
|
+
}
|
521
|
+
}
|
522
|
+
}
|
523
|
+
end
|
524
|
+
|
525
|
+
it 'should render a label with localized label (I18n)' do
|
526
|
+
with_config :i18n_lookups_by_default, false do
|
527
|
+
concat(semantic_form_for(@new_post) do |builder|
|
528
|
+
concat(builder.input(:title, :label => true))
|
529
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
530
|
+
end)
|
531
|
+
output_buffer.should have_tag('form li label', Regexp.new('^' + @localized_label_text))
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
|
536
|
+
with_config :i18n_lookups_by_default, false do
|
537
|
+
::I18n.backend.store_translations :en,
|
538
|
+
:formtastic => {
|
539
|
+
:labels => {
|
540
|
+
:post => {
|
541
|
+
:title => nil,
|
542
|
+
:published => nil
|
543
|
+
}
|
544
|
+
}
|
545
|
+
}
|
546
|
+
concat(semantic_form_for(@new_post) do |builder|
|
547
|
+
concat(builder.input(:title, :label => true))
|
548
|
+
concat(builder.input(:published, :as => :boolean, :label => true))
|
549
|
+
end)
|
550
|
+
output_buffer.should have_tag('form li label', Regexp.new('^' + @default_localized_label_text))
|
551
|
+
end
|
552
|
+
end
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
end
|
557
|
+
|
558
|
+
describe ':hint option' do
|
559
|
+
|
560
|
+
describe 'when provided' do
|
561
|
+
|
562
|
+
after do
|
563
|
+
Formtastic::FormBuilder.default_hint_class = "inline-hints"
|
564
|
+
end
|
565
|
+
|
566
|
+
it 'should be passed down to the paragraph tag' do
|
567
|
+
hint_text = "this is the title of the post"
|
568
|
+
concat(semantic_form_for(@new_post) do |builder|
|
569
|
+
concat(builder.input(:title, :hint => hint_text))
|
570
|
+
end)
|
571
|
+
output_buffer.should have_tag("form li p.inline-hints", hint_text)
|
572
|
+
end
|
573
|
+
|
574
|
+
it 'should have a custom hint class if I ask for one' do
|
575
|
+
hint_text = "this is the title of the post"
|
576
|
+
concat(semantic_form_for(@new_post) do |builder|
|
577
|
+
concat(builder.input(:title, :hint => hint_text, :hint_class => 'custom-hint-class'))
|
578
|
+
end)
|
579
|
+
output_buffer.should have_tag("form li p.custom-hint-class", hint_text)
|
580
|
+
end
|
581
|
+
|
582
|
+
it 'should have a custom hint class defaulted for all forms' do
|
583
|
+
hint_text = "this is the title of the post"
|
584
|
+
Formtastic::FormBuilder.default_hint_class = "custom-hint-class"
|
585
|
+
concat(semantic_form_for(@new_post) do |builder|
|
586
|
+
concat(builder.input(:title, :hint => hint_text))
|
587
|
+
end)
|
588
|
+
output_buffer.should have_tag("form li p.custom-hint-class", hint_text)
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
describe 'when not provided' do
|
593
|
+
describe 'when localized hint (I18n) is provided' do
|
594
|
+
before do
|
595
|
+
@localized_hint_text = "This is the localized hint."
|
596
|
+
@default_localized_hint_text = "This is the default localized hint."
|
597
|
+
::I18n.backend.store_translations :en,
|
598
|
+
:formtastic => {
|
599
|
+
:hints => {
|
600
|
+
:title => @default_localized_hint_text,
|
601
|
+
}
|
602
|
+
}
|
603
|
+
end
|
604
|
+
|
605
|
+
after do
|
606
|
+
::I18n.backend.reload!
|
607
|
+
end
|
608
|
+
|
609
|
+
describe 'when provided value (hint value) is set to TRUE' do
|
610
|
+
it 'should render a hint paragraph containing a localized hint (I18n)' do
|
611
|
+
with_config :i18n_lookups_by_default, false do
|
612
|
+
::I18n.backend.store_translations :en,
|
613
|
+
:formtastic => {
|
614
|
+
:hints => {
|
615
|
+
:post => {
|
616
|
+
:title => @localized_hint_text
|
617
|
+
}
|
618
|
+
}
|
619
|
+
}
|
620
|
+
concat(semantic_form_for(@new_post) do |builder|
|
621
|
+
concat(builder.input(:title, :hint => true))
|
622
|
+
end)
|
623
|
+
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
|
624
|
+
end
|
625
|
+
end
|
626
|
+
|
627
|
+
it 'should render a hint paragraph containing a localized hint (I18n) with a custom hint class if i ask for one' do
|
628
|
+
with_config :i18n_lookups_by_default, false do
|
629
|
+
::I18n.backend.store_translations :en,
|
630
|
+
:formtastic => {
|
631
|
+
:hints => {
|
632
|
+
:post => {
|
633
|
+
:title => @localized_hint_text
|
634
|
+
}
|
635
|
+
}
|
636
|
+
}
|
637
|
+
concat(semantic_form_for(@new_post) do |builder|
|
638
|
+
concat(builder.input(:title, :hint => true, :hint_class => 'custom-hint-class'))
|
639
|
+
end)
|
640
|
+
output_buffer.should have_tag('form li p.custom-hint-class', @localized_hint_text)
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
|
645
|
+
with_config :i18n_lookups_by_default, false do
|
646
|
+
concat(semantic_form_for(@new_post) do |builder|
|
647
|
+
concat(builder.input(:title, :hint => true))
|
648
|
+
end)
|
649
|
+
output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
|
650
|
+
end
|
651
|
+
end
|
652
|
+
end
|
653
|
+
|
654
|
+
describe 'when provided value (label value) is set to FALSE' do
|
655
|
+
it 'should not render a hint paragraph' do
|
656
|
+
with_config :i18n_lookups_by_default, false do
|
657
|
+
concat(semantic_form_for(@new_post) do |builder|
|
658
|
+
concat(builder.input(:title, :hint => false))
|
659
|
+
end)
|
660
|
+
output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
|
661
|
+
end
|
662
|
+
end
|
663
|
+
end
|
664
|
+
end
|
665
|
+
|
666
|
+
describe 'when localized hint (I18n) is a model with attribute hints' do
|
667
|
+
it "should see the provided hash as a blank entry" do
|
668
|
+
with_config :i18n_lookups_by_default, false do
|
669
|
+
::I18n.backend.store_translations :en,
|
670
|
+
:formtastic => {
|
671
|
+
:hints => {
|
672
|
+
:title => { # movie title
|
673
|
+
:summary => @localized_hint_text # summary of movie
|
674
|
+
}
|
675
|
+
}
|
676
|
+
}
|
677
|
+
semantic_form_for(@new_post) do |builder|
|
678
|
+
concat(builder.input(:title, :hint => true))
|
679
|
+
end
|
680
|
+
output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
|
681
|
+
end
|
682
|
+
end
|
683
|
+
end
|
684
|
+
|
685
|
+
describe 'when localized hint (I18n) is not provided' do
|
686
|
+
it 'should not render a hint paragraph' do
|
687
|
+
with_config :i18n_lookups_by_default, false do
|
688
|
+
concat(semantic_form_for(@new_post) do |builder|
|
689
|
+
concat(builder.input(:title))
|
690
|
+
end)
|
691
|
+
output_buffer.should_not have_tag('form li p.inline-hints')
|
692
|
+
end
|
693
|
+
end
|
694
|
+
end
|
695
|
+
end
|
696
|
+
|
697
|
+
end
|
698
|
+
|
699
|
+
describe ':wrapper_html option' do
|
700
|
+
|
701
|
+
describe 'when provided' do
|
702
|
+
it 'should be passed down to the li tag' do
|
703
|
+
concat(semantic_form_for(@new_post) do |builder|
|
704
|
+
concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
|
705
|
+
end)
|
706
|
+
output_buffer.should have_tag("form li#another_id")
|
707
|
+
end
|
708
|
+
|
709
|
+
it 'should append given classes to li default classes' do
|
710
|
+
concat(semantic_form_for(@new_post) do |builder|
|
711
|
+
concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
|
712
|
+
end)
|
713
|
+
output_buffer.should have_tag("form li.string")
|
714
|
+
output_buffer.should have_tag("form li.required")
|
715
|
+
output_buffer.should have_tag("form li.another_class")
|
716
|
+
end
|
717
|
+
|
718
|
+
it 'should allow classes to be an array' do
|
719
|
+
concat(semantic_form_for(@new_post) do |builder|
|
720
|
+
concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
|
721
|
+
end)
|
722
|
+
output_buffer.should have_tag("form li.string")
|
723
|
+
output_buffer.should have_tag("form li.my_class")
|
724
|
+
output_buffer.should have_tag("form li.another_class")
|
725
|
+
end
|
726
|
+
end
|
727
|
+
|
728
|
+
describe 'when not provided' do
|
729
|
+
it 'should use default id and class' do
|
730
|
+
concat(semantic_form_for(@new_post) do |builder|
|
731
|
+
concat(builder.input(:title))
|
732
|
+
end)
|
733
|
+
output_buffer.should have_tag("form li#post_title_input")
|
734
|
+
output_buffer.should have_tag("form li.string")
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
end
|
739
|
+
|
740
|
+
describe ':collection option' do
|
741
|
+
|
742
|
+
it "should be required on polymorphic associations" do
|
743
|
+
@new_post.stub!(:commentable)
|
744
|
+
@new_post.class.stub!(:reflections).and_return({
|
745
|
+
:commentable => mock('macro_reflection', :options => { :polymorphic => true }, :macro => :belongs_to)
|
746
|
+
})
|
747
|
+
@new_post.stub!(:column_for_attribute).with(:commentable).and_return(
|
748
|
+
mock('column', :type => :integer)
|
749
|
+
)
|
750
|
+
@new_post.class.stub!(:reflect_on_association).with(:commentable).and_return(
|
751
|
+
mock('reflection', :macro => :belongs_to, :options => { :polymorphic => true })
|
752
|
+
)
|
753
|
+
expect {
|
754
|
+
concat(semantic_form_for(@new_post) do |builder|
|
755
|
+
concat(builder.inputs do
|
756
|
+
concat(builder.input :commentable)
|
757
|
+
end)
|
758
|
+
end)
|
759
|
+
}.to raise_error(Formtastic::PolymorphicInputWithoutCollectionError)
|
760
|
+
end
|
761
|
+
|
762
|
+
end
|
763
|
+
|
764
|
+
end
|
765
|
+
|
766
|
+
describe 'options re-use' do
|
767
|
+
|
768
|
+
it 'should retain :as option when re-using the same options hash' do
|
769
|
+
my_options = { :as => :string }
|
770
|
+
output = ''
|
771
|
+
|
772
|
+
concat(semantic_form_for(@new_post) do |builder|
|
773
|
+
concat(builder.input(:title, my_options))
|
774
|
+
concat(builder.input(:publish_at, my_options))
|
775
|
+
end)
|
776
|
+
output_buffer.should have_tag 'li.string', :count => 2
|
777
|
+
end
|
778
|
+
|
779
|
+
|
780
|
+
end
|
781
|
+
|
782
|
+
describe 'instantiating an input class' do
|
783
|
+
|
784
|
+
context 'when a class does not exist' do
|
785
|
+
it "should raise an error" do
|
786
|
+
lambda {
|
787
|
+
concat(semantic_form_for(@new_post) do |builder|
|
788
|
+
builder.input(:title, :as => :non_existant)
|
789
|
+
end)
|
790
|
+
}.should raise_error(Formtastic::UnknownInputError)
|
791
|
+
end
|
792
|
+
end
|
793
|
+
|
794
|
+
context 'when a customized top-level class does not exist' do
|
795
|
+
|
796
|
+
it 'should instantiate the Formtastic input' do
|
797
|
+
input = mock('input', :to_html => 'some HTML')
|
798
|
+
Formtastic::Inputs::StringInput.should_receive(:new).and_return(input)
|
799
|
+
concat(semantic_form_for(@new_post) do |builder|
|
800
|
+
builder.input(:title, :as => :string)
|
801
|
+
end)
|
802
|
+
end
|
803
|
+
|
804
|
+
end
|
805
|
+
|
806
|
+
describe 'when a top-level input class exists' do
|
807
|
+
it "should instantiate the top-level input instead of the Formtastic one" do
|
808
|
+
class ::StringInput < Formtastic::Inputs::StringInput
|
809
|
+
end
|
810
|
+
|
811
|
+
input = mock('input', :to_html => 'some HTML')
|
812
|
+
Formtastic::Inputs::StringInput.should_not_receive(:new).and_return(input)
|
813
|
+
::StringInput.should_receive(:new).and_return(input)
|
814
|
+
|
815
|
+
concat(semantic_form_for(@new_post) do |builder|
|
816
|
+
builder.input(:title, :as => :string)
|
817
|
+
end)
|
818
|
+
end
|
819
|
+
end
|
820
|
+
|
821
|
+
describe 'when instantiated multiple times with the same input type' do
|
822
|
+
|
823
|
+
it "should be cached (not calling the internal methods)" do
|
824
|
+
# TODO this is really tied to the underlying implementation
|
825
|
+
concat(semantic_form_for(@new_post) do |builder|
|
826
|
+
builder.should_receive(:custom_input_class_name).with(:string).once.and_return(::Formtastic::Inputs::StringInput)
|
827
|
+
builder.input(:title, :as => :string)
|
828
|
+
builder.input(:title, :as => :string)
|
829
|
+
end)
|
830
|
+
end
|
831
|
+
|
832
|
+
end
|
833
|
+
|
834
|
+
end
|
835
|
+
|
836
|
+
end
|
837
|
+
|