sensis-formtastic-rails3 1.d4e5326
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.textile +584 -0
- data/Rakefile +127 -0
- data/generators/form/USAGE +16 -0
- data/generators/form/form_generator.rb +120 -0
- data/generators/form/templates/view__form.html.erb +5 -0
- data/generators/form/templates/view__form.html.haml +4 -0
- data/generators/formtastic/formtastic_generator.rb +24 -0
- data/generators/formtastic/templates/formtastic.css +131 -0
- data/generators/formtastic/templates/formtastic.rb +54 -0
- data/generators/formtastic/templates/formtastic_changes.css +14 -0
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
- data/init.rb +5 -0
- data/lib/formtastic.rb +1870 -0
- data/lib/formtastic/i18n.rb +35 -0
- data/lib/formtastic/layout_helper.rb +10 -0
- data/lib/formtastic/railtie.rb +12 -0
- data/lib/formtastic/util.rb +36 -0
- data/lib/generators/formtastic/form/form_generator.rb +86 -0
- data/lib/generators/formtastic/install/install_generator.rb +24 -0
- data/lib/locale/en.yml +8 -0
- data/rails/init.rb +2 -0
- data/spec/buttons_spec.rb +166 -0
- data/spec/commit_button_spec.rb +401 -0
- data/spec/custom_builder_spec.rb +98 -0
- data/spec/defaults_spec.rb +20 -0
- data/spec/error_proc_spec.rb +27 -0
- data/spec/errors_spec.rb +105 -0
- data/spec/form_helper_spec.rb +142 -0
- data/spec/helpers/layout_helper_spec.rb +21 -0
- data/spec/i18n_spec.rb +152 -0
- data/spec/include_blank_spec.rb +74 -0
- data/spec/input_spec.rb +786 -0
- data/spec/inputs/boolean_input_spec.rb +104 -0
- data/spec/inputs/check_boxes_input_spec.rb +426 -0
- data/spec/inputs/country_input_spec.rb +118 -0
- data/spec/inputs/date_input_spec.rb +168 -0
- data/spec/inputs/datetime_input_spec.rb +310 -0
- data/spec/inputs/file_input_spec.rb +34 -0
- data/spec/inputs/hidden_input_spec.rb +78 -0
- data/spec/inputs/numeric_input_spec.rb +44 -0
- data/spec/inputs/password_input_spec.rb +46 -0
- data/spec/inputs/radio_input_spec.rb +243 -0
- data/spec/inputs/select_input_spec.rb +546 -0
- data/spec/inputs/string_input_spec.rb +64 -0
- data/spec/inputs/text_input_spec.rb +34 -0
- data/spec/inputs/time_input_spec.rb +206 -0
- data/spec/inputs/time_zone_input_spec.rb +110 -0
- data/spec/inputs_spec.rb +476 -0
- data/spec/label_spec.rb +89 -0
- data/spec/semantic_errors_spec.rb +98 -0
- data/spec/semantic_fields_for_spec.rb +45 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +289 -0
- data/spec/support/custom_macros.rb +494 -0
- data/spec/support/output_buffer.rb +4 -0
- data/spec/support/test_environment.rb +45 -0
- metadata +234 -0
@@ -0,0 +1,118 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'country input' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when country_select is not available as a helper from a plugin" do
|
14
|
+
|
15
|
+
it "should raise an error, sugesting the author installs a plugin" do
|
16
|
+
lambda {
|
17
|
+
semantic_form_for(@new_post) do |builder|
|
18
|
+
concat(builder.input(:country, :as => :country))
|
19
|
+
end
|
20
|
+
}.should raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "when country_select is available as a helper (from a plugin)" do
|
26
|
+
|
27
|
+
before do
|
28
|
+
@form = semantic_form_for(@new_post) do |builder|
|
29
|
+
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
30
|
+
concat(builder.input(:country, :as => :country))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it_should_have_input_wrapper_with_class("country")
|
35
|
+
it_should_have_input_wrapper_with_id("post_country_input")
|
36
|
+
|
37
|
+
# TODO -- needs stubbing inside the builder block, tricky!
|
38
|
+
#it_should_apply_error_logic_for_input_type(:country)
|
39
|
+
|
40
|
+
it 'should generate a label for the input' do
|
41
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
42
|
+
output_buffer.should have_tag('form li label')
|
43
|
+
output_buffer.should have_tag('form li label[@for="post_country"]')
|
44
|
+
output_buffer.should have_tag('form li label', /Country/)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should generate a select" do
|
48
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
49
|
+
output_buffer.should have_tag("form li select")
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe ":priority_countries option" do
|
55
|
+
|
56
|
+
it "should be passed down to the country_select helper when provided" do
|
57
|
+
priority_countries = ["Foo", "Bah"]
|
58
|
+
semantic_form_for(@new_post) do |builder|
|
59
|
+
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
60
|
+
builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
61
|
+
|
62
|
+
concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should default to the @@priority_countries config when absent" do
|
67
|
+
priority_countries = ::Formtastic::SemanticFormBuilder.priority_countries
|
68
|
+
priority_countries.should_not be_empty
|
69
|
+
priority_countries.should_not be_nil
|
70
|
+
|
71
|
+
semantic_form_for(@new_post) do |builder|
|
72
|
+
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
73
|
+
builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
74
|
+
|
75
|
+
concat(builder.input(:country, :as => :country))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "matching" do
|
82
|
+
|
83
|
+
describe "when the attribute is 'country'" do
|
84
|
+
|
85
|
+
before do
|
86
|
+
@form = semantic_form_for(@new_post) do |builder|
|
87
|
+
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
|
88
|
+
concat(builder.input(:country))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should render a country input" do
|
93
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
94
|
+
output_buffer.should have_tag "form li.country"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "whent the attribute is 'country_something'" do
|
99
|
+
|
100
|
+
before do
|
101
|
+
@form = semantic_form_for(@new_post) do |builder|
|
102
|
+
concat(builder.input(:country_subdivision))
|
103
|
+
concat(builder.input(:country_code))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should render a country input" do
|
108
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
109
|
+
output_buffer.should_not have_tag "form li.country"
|
110
|
+
output_buffer.should have_tag "form li.string", :count => 2
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'date input' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "general" do
|
14
|
+
|
15
|
+
before do
|
16
|
+
output_buffer.replace ''
|
17
|
+
@form = semantic_form_for(@new_post) do |builder|
|
18
|
+
concat(builder.input(:publish_at, :as => :date))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it_should_have_input_wrapper_with_class("date")
|
23
|
+
it_should_have_input_wrapper_with_id("post_publish_at_input")
|
24
|
+
it_should_have_a_nested_fieldset
|
25
|
+
it_should_apply_error_logic_for_input_type(:date)
|
26
|
+
|
27
|
+
it 'should have a legend and label with the label text inside the fieldset' do
|
28
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
29
|
+
output_buffer.should have_tag('form li.date fieldset legend.label label', /Publish at/)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should associate the legend label with the first select' do
|
33
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
34
|
+
output_buffer.should have_tag('form li.date fieldset legend.label')
|
35
|
+
output_buffer.should have_tag('form li.date fieldset legend.label label')
|
36
|
+
output_buffer.should have_tag('form li.date fieldset legend.label label[@for]')
|
37
|
+
output_buffer.should have_tag('form li.date fieldset legend.label label[@for="post_publish_at_1i"]')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should have an ordered list of three items inside the fieldset' do
|
41
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
42
|
+
output_buffer.should have_tag('form li.date fieldset ol')
|
43
|
+
output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should have three labels for year, month and day' do
|
47
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
48
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
|
49
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
|
50
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
|
51
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should have three selects for year, month and day' do
|
55
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
56
|
+
output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe ':selected option' do
|
61
|
+
|
62
|
+
describe "when the object has a value" do
|
63
|
+
it "should select the object value (ignoring :selected)" do
|
64
|
+
output_buffer.replace ''
|
65
|
+
@new_post.stub!(:created_at => Time.mktime(2012))
|
66
|
+
with_deprecation_silenced do
|
67
|
+
@form = semantic_form_for(@new_post) do |builder|
|
68
|
+
concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
72
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
73
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'when the object has no value (nil)' do
|
78
|
+
it "should select the :selected if provided as a Date" do
|
79
|
+
output_buffer.replace ''
|
80
|
+
@new_post.stub!(:created_at => nil)
|
81
|
+
with_deprecation_silenced do
|
82
|
+
@form = semantic_form_for(@new_post) do |builder|
|
83
|
+
concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999)))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
87
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
88
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should select the :selected if provided as a Time" do
|
92
|
+
output_buffer.replace ''
|
93
|
+
@new_post.stub!(:created_at => nil)
|
94
|
+
with_deprecation_silenced do
|
95
|
+
@form = semantic_form_for(@new_post) do |builder|
|
96
|
+
concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
|
97
|
+
end
|
98
|
+
end
|
99
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
100
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
101
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not select an option if the :selected is provided as nil" do
|
105
|
+
output_buffer.replace ''
|
106
|
+
@new_post.stub!(:created_at => nil)
|
107
|
+
with_deprecation_silenced do
|
108
|
+
@form = semantic_form_for(@new_post) do |builder|
|
109
|
+
concat(builder.input(:created_at, :as => :date, :selected => nil))
|
110
|
+
end
|
111
|
+
end
|
112
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
113
|
+
output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should select nothing if a :selected is not provided" do
|
117
|
+
output_buffer.replace ''
|
118
|
+
@new_post.stub!(:created_at => nil)
|
119
|
+
form = semantic_form_for(@new_post) do |builder|
|
120
|
+
concat(builder.input(:created_at, :as => :date))
|
121
|
+
end
|
122
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
123
|
+
output_buffer.should_not have_tag("form li ol li select option[@selected]")
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
describe ':labels option' do
|
131
|
+
fields = [:year, :month, :day]
|
132
|
+
fields.each do |field|
|
133
|
+
it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
|
134
|
+
output_buffer.replace ''
|
135
|
+
form = semantic_form_for(@new_post) do |builder|
|
136
|
+
concat(builder.input(:created_at, :as => :date, :labels => { field => "another #{field} label" }))
|
137
|
+
end
|
138
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
139
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length)
|
140
|
+
fields.each do |f|
|
141
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
|
146
|
+
output_buffer.replace ''
|
147
|
+
form = semantic_form_for(@new_post) do |builder|
|
148
|
+
concat(builder.input(:created_at, :as => :date, :labels => { field => "" }))
|
149
|
+
end
|
150
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
151
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length-1)
|
152
|
+
fields.each do |f|
|
153
|
+
output_buffer.should have_tag('form li.date fieldset ol li label', /#{f}/i) unless field == f
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should warn about :selected deprecation' do
|
160
|
+
with_deprecation_silenced do
|
161
|
+
::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
|
162
|
+
semantic_form_for(@new_post) do |builder|
|
163
|
+
concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999)))
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
@@ -0,0 +1,310 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'datetime input' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
|
12
|
+
@new_post.should_receive(:publish_at=).any_number_of_times
|
13
|
+
@new_post.should_receive(:created_at=).any_number_of_times
|
14
|
+
@bob.should_receive(:created_at=).any_number_of_times
|
15
|
+
@new_post.should_receive(:title=).any_number_of_times # Macro stuff forces this.
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "general" do
|
19
|
+
|
20
|
+
before do
|
21
|
+
output_buffer.replace ''
|
22
|
+
@form = semantic_form_for(@new_post) do |builder|
|
23
|
+
concat(builder.input(:publish_at, :as => :datetime))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it_should_have_input_wrapper_with_class("datetime")
|
28
|
+
it_should_have_input_wrapper_with_id("post_publish_at_input")
|
29
|
+
it_should_have_a_nested_fieldset
|
30
|
+
it_should_apply_error_logic_for_input_type(:datetime)
|
31
|
+
|
32
|
+
it 'should have a legend and label with the label text inside the fieldset' do
|
33
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
34
|
+
output_buffer.should have_tag('form li.datetime fieldset legend.label label', /Publish at/)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should associate the legend label with the first select' do
|
38
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
39
|
+
output_buffer.should have_tag('form li.datetime fieldset legend.label label[@for="post_publish_at_1i"]')
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should have an ordered list of five items inside the fieldset' do
|
43
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
44
|
+
output_buffer.should have_tag('form li.datetime fieldset ol')
|
45
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should have five labels for year, month, day, hour and minute' do
|
49
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
50
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
|
51
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
|
52
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
|
53
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
|
54
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
|
55
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should have five selects for year, month, day, hour and minute' do
|
59
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
60
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should generate a sanitized label and matching ids for attribute' do
|
64
|
+
form = semantic_form_for(@new_post) do |builder|
|
65
|
+
fields = builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
|
66
|
+
concat(bob_builder.input(:created_at, :as => :datetime))
|
67
|
+
end
|
68
|
+
concat(fields)
|
69
|
+
end
|
70
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
71
|
+
|
72
|
+
1.upto(5) do |i|
|
73
|
+
output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']")
|
74
|
+
output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'when :discard_input => true is set' do
|
80
|
+
it 'should use default attribute value when it is not nil' do
|
81
|
+
@new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
|
82
|
+
form = semantic_form_for(@new_post) do |builder|
|
83
|
+
concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
|
84
|
+
end
|
85
|
+
|
86
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
87
|
+
output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'inputs order' do
|
92
|
+
it 'should have a default' do
|
93
|
+
output_buffer.replace ''
|
94
|
+
semantic_form_for(@new_post) do |builder|
|
95
|
+
self.should_receive(:select_year).once.ordered.and_return('')
|
96
|
+
self.should_receive(:select_month).once.ordered.and_return('')
|
97
|
+
self.should_receive(:select_day).once.ordered.and_return('')
|
98
|
+
builder.input(:publish_at, :as => :datetime)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should be specified with :order option' do
|
103
|
+
::I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
|
104
|
+
output_buffer.replace ''
|
105
|
+
semantic_form_for(@new_post) do |builder|
|
106
|
+
self.should_receive(:select_month).once.ordered.and_return('')
|
107
|
+
self.should_receive(:select_year).once.ordered.and_return('')
|
108
|
+
self.should_receive(:select_day).once.ordered.and_return('')
|
109
|
+
builder.input(:publish_at, :as => :datetime)
|
110
|
+
::I18n.backend.store_translations 'en', :date => nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should be changed through I18n' do
|
115
|
+
output_buffer.replace ''
|
116
|
+
semantic_form_for(@new_post) do |builder|
|
117
|
+
self.should_receive(:select_day).once.ordered.and_return('')
|
118
|
+
self.should_receive(:select_month).once.ordered.and_return('')
|
119
|
+
self.should_receive(:select_year).once.ordered.and_return('')
|
120
|
+
builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'when the locale changes the label text' do
|
126
|
+
before do
|
127
|
+
::I18n.backend.store_translations 'en', :datetime => {:prompts => {
|
128
|
+
:year => 'The Year', :month => 'The Month', :day => 'The Day',
|
129
|
+
:hour => 'The Hour', :minute => 'The Minute'
|
130
|
+
}}
|
131
|
+
output_buffer.replace ''
|
132
|
+
@form = semantic_form_for(@new_post) do |builder|
|
133
|
+
concat(builder.input(:publish_at, :as => :datetime))
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
after do
|
138
|
+
::I18n.backend.store_translations 'en', :formtastic => {
|
139
|
+
:year => nil, :month => nil, :day => nil,
|
140
|
+
:hour => nil, :minute => nil
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should have translated labels for year, month, day, hour and minute' do
|
145
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
146
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
|
147
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
|
148
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
|
149
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
|
150
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe 'when no object is given' do
|
155
|
+
before(:each) do
|
156
|
+
output_buffer.replace ''
|
157
|
+
@form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
158
|
+
concat(builder.input(:publish_at, :as => :datetime))
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should have fieldset with legend - classified as a label' do
|
163
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
164
|
+
output_buffer.should have_tag('form li.datetime fieldset legend.label', /Publish at/)
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'should have labels for each input' do
|
168
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
169
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should have selects for each inputs' do
|
173
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
174
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
#describe 'when an object is given and the attribute value is nil' do
|
179
|
+
# before(:each) do
|
180
|
+
# @new_post.stub!(:publish_at).and_return(nil)
|
181
|
+
# output_buffer.replace ''
|
182
|
+
# semantic_form_for(@new_post) do |builder|
|
183
|
+
# concat(builder.input(:publish_at, :as => :datetime))
|
184
|
+
# end
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# it 'should not select a value' do
|
188
|
+
# output_buffer.should_not have_tag('form li.datetime option[@selected]')
|
189
|
+
# end
|
190
|
+
#
|
191
|
+
#end
|
192
|
+
|
193
|
+
describe ':selected option' do
|
194
|
+
|
195
|
+
describe "when the object has a value" do
|
196
|
+
it "should select the object value (ignoring :selected)" do
|
197
|
+
output_buffer.replace ''
|
198
|
+
@new_post.stub!(:created_at => Time.mktime(2012))
|
199
|
+
with_deprecation_silenced do
|
200
|
+
@form = semantic_form_for(@new_post) do |builder|
|
201
|
+
concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999)))
|
202
|
+
end
|
203
|
+
end
|
204
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
205
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
206
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe 'when the object has no value' do
|
211
|
+
it "should select the :selected if provided as a Date" do
|
212
|
+
output_buffer.replace ''
|
213
|
+
@new_post.stub!(:created_at => nil)
|
214
|
+
with_deprecation_silenced do
|
215
|
+
@form = semantic_form_for(@new_post) do |builder|
|
216
|
+
concat(builder.input(:created_at, :as => :datetime, :selected => Date.new(1999)))
|
217
|
+
end
|
218
|
+
end
|
219
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
220
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
221
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should select the :selected if provided as a Time" do
|
225
|
+
output_buffer.replace ''
|
226
|
+
@new_post.stub!(:created_at => nil)
|
227
|
+
with_deprecation_silenced do
|
228
|
+
@form = semantic_form_for(@new_post) do |builder|
|
229
|
+
concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999)))
|
230
|
+
end
|
231
|
+
end
|
232
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
233
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1)
|
234
|
+
output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1)
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should not select an option if the :selected is provided as nil" do
|
238
|
+
output_buffer.replace ''
|
239
|
+
@new_post.stub!(:created_at => nil)
|
240
|
+
with_deprecation_silenced do
|
241
|
+
@form = semantic_form_for(@new_post) do |builder|
|
242
|
+
concat(builder.input(:created_at, :as => :datetime, :selected => nil))
|
243
|
+
end
|
244
|
+
end
|
245
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
246
|
+
output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]")
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should select nothing if a :selected is not provided" do
|
250
|
+
output_buffer.replace ''
|
251
|
+
@new_post.stub!(:created_at => nil)
|
252
|
+
form = semantic_form_for(@new_post) do |builder|
|
253
|
+
concat(builder.input(:created_at, :as => :datetime))
|
254
|
+
end
|
255
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
256
|
+
output_buffer.should_not have_tag("form li ol li select option[@selected]")
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'should warn about :selected deprecation' do
|
261
|
+
with_deprecation_silenced do
|
262
|
+
::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
|
263
|
+
semantic_form_for(@new_post) do |builder|
|
264
|
+
concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999)))
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
describe ':labels option' do
|
272
|
+
fields = [:year, :month, :day, :hour, :minute]
|
273
|
+
fields.each do |field|
|
274
|
+
it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do
|
275
|
+
output_buffer.replace ''
|
276
|
+
form = semantic_form_for(@new_post) do |builder|
|
277
|
+
concat(builder.input(:created_at, :as => :datetime, :labels => { field => "another #{field} label" }))
|
278
|
+
end
|
279
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
280
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length)
|
281
|
+
fields.each do |f|
|
282
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do
|
287
|
+
output_buffer.replace ''
|
288
|
+
form = semantic_form_for(@new_post) do |builder|
|
289
|
+
concat(builder.input(:created_at, :as => :datetime, :labels => { field => "" }))
|
290
|
+
end
|
291
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
292
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length-1)
|
293
|
+
fields.each do |f|
|
294
|
+
output_buffer.should have_tag('form li.datetime fieldset ol li label', /#{f}/i) unless field == f
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'should warn about :selected deprecation' do
|
301
|
+
with_deprecation_silenced do
|
302
|
+
::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
|
303
|
+
semantic_form_for(@new_post) do |builder|
|
304
|
+
concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999)))
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
310
|
+
|