formtastic 0.9.1 → 0.9.2

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.
@@ -0,0 +1,60 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'boolean input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:allow_comments, :as => :boolean))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("boolean")
18
+ it_should_have_input_wrapper_with_id("post_allow_comments_input")
19
+ it_should_apply_error_logic_for_input_type(:boolean)
20
+
21
+ it 'should generate a label containing the input' do
22
+ output_buffer.should have_tag('form li label', :count => 1)
23
+ output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
24
+ output_buffer.should have_tag('form li label', /Allow comments/)
25
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
26
+ end
27
+
28
+ it 'should generate a checkbox input' do
29
+ output_buffer.should have_tag('form li label input')
30
+ output_buffer.should have_tag('form li label input#post_allow_comments')
31
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
32
+ output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
33
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
34
+ end
35
+
36
+ it 'should allow checked and unchecked values to be sent' do
37
+ semantic_form_for(@new_post) do |builder|
38
+ concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
39
+ end
40
+
41
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]')
42
+ output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
43
+ end
44
+
45
+ it 'should generate a label and a checkbox even if no object is given' do
46
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
47
+ concat(builder.input(:allow_comments, :as => :boolean))
48
+ end
49
+
50
+ output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
51
+ output_buffer.should have_tag('form li label', /Allow comments/)
52
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
53
+
54
+ output_buffer.should have_tag('form li label input#project_allow_comments')
55
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
56
+ output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
57
+ end
58
+
59
+ end
60
+
@@ -0,0 +1,106 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'check_boxes input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ describe 'for a has_many association' do
9
+ before do
10
+ @output_buffer = ''
11
+ mock_everything
12
+
13
+ semantic_form_for(@fred) do |builder|
14
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
15
+ end
16
+ end
17
+
18
+ it_should_have_input_wrapper_with_class("check_boxes")
19
+ it_should_have_input_wrapper_with_id("author_posts_input")
20
+ it_should_have_a_nested_fieldset
21
+ it_should_apply_error_logic_for_input_type(:check_boxes)
22
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes)
23
+ it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
24
+
25
+ it 'should generate a legend containing label text for the input' do
26
+ output_buffer.should have_tag('form li fieldset legend')
27
+ output_buffer.should have_tag('form li fieldset legend', /Posts/)
28
+ end
29
+
30
+ it 'should generate an ordered list with a list item for each choice' do
31
+ output_buffer.should have_tag('form li fieldset ol')
32
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size)
33
+ end
34
+
35
+ it 'should have one option with a "checked" attribute' do
36
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
37
+ end
38
+
39
+ it 'should generate hidden inputs with default value blank' do
40
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
41
+ end
42
+
43
+ describe "each choice" do
44
+
45
+ it 'should contain a label for the radio input with a nested input and label text' do
46
+ ::Post.find(:all).each do |post|
47
+ output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
48
+ output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
49
+ end
50
+ end
51
+
52
+ it 'should use values as li.class when value_as_class is true' do
53
+ ::Post.find(:all).each do |post|
54
+ output_buffer.should have_tag("form li fieldset ol li.#{post.id} label")
55
+ end
56
+ end
57
+
58
+ it 'should have a checkbox input for each post' do
59
+ ::Post.find(:all).each do |post|
60
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
61
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
62
+ end
63
+ end
64
+
65
+ it "should mark input as checked if it's the the existing choice" do
66
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
67
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
68
+ end
69
+ end
70
+
71
+ describe 'and no object is given' do
72
+ before(:each) do
73
+ output_buffer.replace ''
74
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
75
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
76
+ end
77
+ end
78
+
79
+ it 'should generate a fieldset with legend' do
80
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
81
+ end
82
+
83
+ it 'shold generate an li tag for each item in the collection' do
84
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
85
+ end
86
+
87
+ it 'should generate labels for each item' do
88
+ ::Author.find(:all).each do |author|
89
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
90
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
91
+ end
92
+ end
93
+
94
+ it 'should generate inputs for each item' do
95
+ ::Author.find(:all).each do |author|
96
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
97
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
98
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
99
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ end
106
+
@@ -0,0 +1,80 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../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
+ semantic_form_for(@new_post) do |builder|
29
+ builder.stub!(:country_select).and_return("<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.should have_tag('form li label')
42
+ output_buffer.should have_tag('form li label[@for="post_country"]')
43
+ output_buffer.should have_tag('form li label', /Country/)
44
+ end
45
+
46
+ it "should generate a select" do
47
+ output_buffer.should have_tag("form li select")
48
+ end
49
+
50
+ end
51
+
52
+ describe ":priority_countries option" do
53
+
54
+ it "should be passed down to the country_select helper when provided" do
55
+ priority_countries = ["Foo", "Bah"]
56
+ semantic_form_for(@new_post) do |builder|
57
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
58
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
59
+
60
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
61
+ end
62
+ end
63
+
64
+ it "should default to the @@priority_countries config when absent" do
65
+ priority_countries = Formtastic::SemanticFormBuilder.priority_countries
66
+ priority_countries.should_not be_empty
67
+ priority_countries.should_not be_nil
68
+
69
+ semantic_form_for(@new_post) do |builder|
70
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
71
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
72
+
73
+ concat(builder.input(:country, :as => :country))
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'date input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:publish_at, :as => :date))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("date")
18
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
19
+ it_should_have_a_nested_fieldset
20
+ it_should_apply_error_logic_for_input_type(:date)
21
+
22
+ it 'should have a legend containing the label text inside the fieldset' do
23
+ output_buffer.should have_tag('form li.date fieldset legend', /Publish at/)
24
+ end
25
+
26
+ it 'should have an ordered list of three items inside the fieldset' do
27
+ output_buffer.should have_tag('form li.date fieldset ol')
28
+ output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
29
+ end
30
+
31
+ it 'should have three labels for year, month and day' do
32
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
33
+ output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
34
+ output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
35
+ output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
36
+ end
37
+
38
+ it 'should have three selects for year, month and day' do
39
+ output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,155 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe 'datetime input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+
12
+ semantic_form_for(@new_post) do |builder|
13
+ concat(builder.input(:publish_at, :as => :datetime))
14
+ end
15
+ end
16
+
17
+ it_should_have_input_wrapper_with_class("datetime")
18
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
19
+ it_should_have_a_nested_fieldset
20
+ it_should_apply_error_logic_for_input_type(:datetime)
21
+
22
+ it 'should have a legend containing the label text inside the fieldset' do
23
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
24
+ end
25
+
26
+ it 'should have an ordered list of five items inside the fieldset' do
27
+ output_buffer.should have_tag('form li.datetime fieldset ol')
28
+ output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
29
+ end
30
+
31
+ it 'should have five labels for year, month, day, hour and minute' do
32
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
33
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
34
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
35
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
36
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
37
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
38
+ end
39
+
40
+ it 'should have five selects for year, month, day, hour and minute' do
41
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
42
+ end
43
+
44
+ it 'should generate a sanitized label and matching ids for attribute' do
45
+ semantic_form_for(@new_post) do |builder|
46
+ builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
47
+ concat(bob_builder.input(:created_at, :as => :datetime))
48
+ end
49
+ end
50
+
51
+ 1.upto(5) do |i|
52
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']")
53
+ output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i")
54
+ end
55
+ end
56
+
57
+ describe 'when :discard_input => true is set' do
58
+ it 'should use default hidden value equals to 1 when attribute returns nil' do
59
+ semantic_form_for(@new_post) do |builder|
60
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
61
+ end
62
+
63
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='1']")
64
+ end
65
+
66
+ it 'should use default attribute value when it is not nil' do
67
+ @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
68
+ semantic_form_for(@new_post) do |builder|
69
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
70
+ end
71
+
72
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
73
+ end
74
+ end
75
+
76
+ describe 'inputs order' do
77
+ it 'should have a default' do
78
+ semantic_form_for(@new_post) do |builder|
79
+ self.should_receive(:select_year).once.ordered.and_return('')
80
+ self.should_receive(:select_month).once.ordered.and_return('')
81
+ self.should_receive(:select_day).once.ordered.and_return('')
82
+ builder.input(:publish_at, :as => :datetime)
83
+ end
84
+ end
85
+
86
+ it 'should be specified with :order option' do
87
+ I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
88
+ semantic_form_for(@new_post) do |builder|
89
+ self.should_receive(:select_month).once.ordered.and_return('')
90
+ self.should_receive(:select_year).once.ordered.and_return('')
91
+ self.should_receive(:select_day).once.ordered.and_return('')
92
+ builder.input(:publish_at, :as => :datetime)
93
+ end
94
+ end
95
+
96
+ it 'should be changed through I18n' do
97
+ semantic_form_for(@new_post) do |builder|
98
+ self.should_receive(:select_day).once.ordered.and_return('')
99
+ self.should_receive(:select_month).once.ordered.and_return('')
100
+ self.should_receive(:select_year).once.ordered.and_return('')
101
+ builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
102
+ end
103
+ end
104
+ end
105
+
106
+ describe 'when the locale changes the label text' do
107
+ before do
108
+ I18n.backend.store_translations 'en', :datetime => {:prompts => {
109
+ :year => 'The Year', :month => 'The Month', :day => 'The Day',
110
+ :hour => 'The Hour', :minute => 'The Minute'
111
+ }}
112
+ semantic_form_for(@new_post) do |builder|
113
+ concat(builder.input(:publish_at, :as => :datetime))
114
+ end
115
+ end
116
+
117
+ after do
118
+ I18n.backend.store_translations 'en', :formtastic => {
119
+ :year => nil, :month => nil, :day => nil,
120
+ :hour => nil, :minute => nil
121
+ }
122
+ end
123
+
124
+ it 'should have translated labels for year, month, day, hour and minute' do
125
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
126
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
127
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
128
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
129
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
130
+ end
131
+ end
132
+
133
+ describe 'when no object is given' do
134
+ before(:each) do
135
+ output_buffer.replace ''
136
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
137
+ concat(builder.input(:publish_at, :as => :datetime))
138
+ end
139
+ end
140
+
141
+ it 'should have fieldset with legend' do
142
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
143
+ end
144
+
145
+ it 'should have labels for each input' do
146
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
147
+ end
148
+
149
+ it 'should have selects for each inputs' do
150
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
151
+ end
152
+ end
153
+
154
+ end
155
+