formtastic 2.1.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/test.yml +61 -0
  4. data/.gitignore +4 -2
  5. data/CHANGELOG.md +52 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +105 -0
  8. data/MIT-LICENSE +1 -1
  9. data/{README.textile → README.md} +204 -219
  10. data/RELEASE_PROCESS +3 -1
  11. data/Rakefile +27 -29
  12. data/app/assets/stylesheets/formtastic.css +3 -2
  13. data/bin/appraisal +8 -0
  14. data/formtastic.gemspec +11 -14
  15. data/gemfiles/rails_5.2/Gemfile +5 -0
  16. data/gemfiles/rails_6.0/Gemfile +5 -0
  17. data/gemfiles/rails_6.1/Gemfile +5 -0
  18. data/gemfiles/rails_edge/Gemfile +13 -0
  19. data/lib/formtastic/action_class_finder.rb +18 -0
  20. data/lib/formtastic/actions/button_action.rb +55 -60
  21. data/lib/formtastic/actions/input_action.rb +59 -57
  22. data/lib/formtastic/actions/link_action.rb +68 -67
  23. data/lib/formtastic/actions.rb +6 -3
  24. data/lib/formtastic/deprecation.rb +5 -0
  25. data/lib/formtastic/engine.rb +3 -1
  26. data/lib/formtastic/form_builder.rb +35 -16
  27. data/lib/formtastic/helpers/action_helper.rb +34 -28
  28. data/lib/formtastic/helpers/enum.rb +13 -0
  29. data/lib/formtastic/helpers/errors_helper.rb +2 -2
  30. data/lib/formtastic/helpers/fieldset_wrapper.rb +16 -12
  31. data/lib/formtastic/helpers/form_helper.rb +19 -16
  32. data/lib/formtastic/helpers/input_helper.rb +69 -97
  33. data/lib/formtastic/helpers/inputs_helper.rb +35 -25
  34. data/lib/formtastic/helpers/reflection.rb +4 -4
  35. data/lib/formtastic/helpers.rb +1 -2
  36. data/lib/formtastic/html_attributes.rb +12 -1
  37. data/lib/formtastic/i18n.rb +1 -1
  38. data/lib/formtastic/input_class_finder.rb +18 -0
  39. data/lib/formtastic/inputs/base/choices.rb +2 -2
  40. data/lib/formtastic/inputs/base/collections.rb +46 -14
  41. data/lib/formtastic/inputs/base/database.rb +7 -2
  42. data/lib/formtastic/inputs/base/datetime_pickerish.rb +85 -0
  43. data/lib/formtastic/inputs/base/errors.rb +7 -7
  44. data/lib/formtastic/inputs/base/hints.rb +2 -2
  45. data/lib/formtastic/inputs/base/html.rb +10 -9
  46. data/lib/formtastic/inputs/base/labelling.rb +5 -8
  47. data/lib/formtastic/inputs/base/naming.rb +4 -4
  48. data/lib/formtastic/inputs/base/numeric.rb +1 -1
  49. data/lib/formtastic/inputs/base/options.rb +3 -4
  50. data/lib/formtastic/inputs/base/stringish.rb +10 -2
  51. data/lib/formtastic/inputs/base/timeish.rb +34 -22
  52. data/lib/formtastic/inputs/base/validations.rb +41 -13
  53. data/lib/formtastic/inputs/base/wrapping.rb +29 -26
  54. data/lib/formtastic/inputs/base.rb +22 -15
  55. data/lib/formtastic/inputs/boolean_input.rb +26 -12
  56. data/lib/formtastic/inputs/check_boxes_input.rb +39 -31
  57. data/lib/formtastic/inputs/color_input.rb +41 -0
  58. data/lib/formtastic/inputs/country_input.rb +24 -5
  59. data/lib/formtastic/inputs/datalist_input.rb +41 -0
  60. data/lib/formtastic/inputs/date_picker_input.rb +93 -0
  61. data/lib/formtastic/inputs/{date_input.rb → date_select_input.rb} +1 -1
  62. data/lib/formtastic/inputs/datetime_picker_input.rb +103 -0
  63. data/lib/formtastic/inputs/{datetime_input.rb → datetime_select_input.rb} +1 -1
  64. data/lib/formtastic/inputs/file_input.rb +2 -2
  65. data/lib/formtastic/inputs/hidden_input.rb +2 -6
  66. data/lib/formtastic/inputs/radio_input.rb +28 -22
  67. data/lib/formtastic/inputs/select_input.rb +36 -39
  68. data/lib/formtastic/inputs/time_picker_input.rb +99 -0
  69. data/lib/formtastic/inputs/{time_input.rb → time_select_input.rb} +6 -2
  70. data/lib/formtastic/inputs/time_zone_input.rb +16 -6
  71. data/lib/formtastic/inputs.rb +32 -21
  72. data/lib/formtastic/localized_string.rb +1 -1
  73. data/lib/formtastic/localizer.rb +24 -24
  74. data/lib/formtastic/namespaced_class_finder.rb +99 -0
  75. data/lib/formtastic/version.rb +1 -1
  76. data/lib/formtastic.rb +20 -10
  77. data/lib/generators/formtastic/form/form_generator.rb +10 -4
  78. data/lib/generators/formtastic/input/input_generator.rb +46 -0
  79. data/lib/generators/formtastic/install/install_generator.rb +5 -19
  80. data/lib/generators/templates/_form.html.slim +2 -2
  81. data/lib/generators/templates/formtastic.rb +46 -25
  82. data/lib/generators/templates/input.rb +19 -0
  83. data/sample/basic_inputs.html +23 -3
  84. data/script/integration-template.rb +74 -0
  85. data/script/integration.sh +19 -0
  86. data/spec/action_class_finder_spec.rb +12 -0
  87. data/spec/actions/button_action_spec.rb +8 -8
  88. data/spec/actions/generic_action_spec.rb +92 -56
  89. data/spec/actions/input_action_spec.rb +7 -7
  90. data/spec/actions/link_action_spec.rb +10 -10
  91. data/spec/builder/custom_builder_spec.rb +36 -20
  92. data/spec/builder/error_proc_spec.rb +4 -4
  93. data/spec/builder/semantic_fields_for_spec.rb +28 -29
  94. data/spec/fast_spec_helper.rb +12 -0
  95. data/spec/generators/formtastic/form/form_generator_spec.rb +45 -32
  96. data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
  97. data/spec/generators/formtastic/install/install_generator_spec.rb +9 -9
  98. data/spec/helpers/action_helper_spec.rb +75 -103
  99. data/spec/helpers/actions_helper_spec.rb +17 -17
  100. data/spec/helpers/form_helper_spec.rb +84 -33
  101. data/spec/helpers/input_helper_spec.rb +333 -285
  102. data/spec/helpers/inputs_helper_spec.rb +167 -121
  103. data/spec/helpers/reflection_helper_spec.rb +3 -3
  104. data/spec/helpers/semantic_errors_helper_spec.rb +23 -23
  105. data/spec/i18n_spec.rb +26 -26
  106. data/spec/input_class_finder_spec.rb +10 -0
  107. data/spec/inputs/base/collections_spec.rb +76 -0
  108. data/spec/inputs/base/validations_spec.rb +480 -0
  109. data/spec/inputs/boolean_input_spec.rb +100 -65
  110. data/spec/inputs/check_boxes_input_spec.rb +200 -101
  111. data/spec/inputs/color_input_spec.rb +85 -0
  112. data/spec/inputs/country_input_spec.rb +20 -20
  113. data/spec/inputs/custom_input_spec.rb +3 -4
  114. data/spec/inputs/datalist_input_spec.rb +61 -0
  115. data/spec/inputs/date_picker_input_spec.rb +449 -0
  116. data/spec/inputs/date_select_input_spec.rb +249 -0
  117. data/spec/inputs/datetime_picker_input_spec.rb +490 -0
  118. data/spec/inputs/datetime_select_input_spec.rb +209 -0
  119. data/spec/inputs/email_input_spec.rb +5 -5
  120. data/spec/inputs/file_input_spec.rb +6 -6
  121. data/spec/inputs/hidden_input_spec.rb +22 -35
  122. data/spec/inputs/include_blank_spec.rb +11 -11
  123. data/spec/inputs/label_spec.rb +62 -25
  124. data/spec/inputs/number_input_spec.rb +112 -112
  125. data/spec/inputs/password_input_spec.rb +5 -5
  126. data/spec/inputs/phone_input_spec.rb +5 -5
  127. data/spec/inputs/placeholder_spec.rb +6 -6
  128. data/spec/inputs/radio_input_spec.rb +99 -55
  129. data/spec/inputs/range_input_spec.rb +66 -66
  130. data/spec/inputs/readonly_spec.rb +50 -0
  131. data/spec/inputs/search_input_spec.rb +5 -5
  132. data/spec/inputs/select_input_spec.rb +170 -170
  133. data/spec/inputs/string_input_spec.rb +68 -16
  134. data/spec/inputs/text_input_spec.rb +16 -16
  135. data/spec/inputs/time_picker_input_spec.rb +455 -0
  136. data/spec/inputs/time_select_input_spec.rb +261 -0
  137. data/spec/inputs/time_zone_input_spec.rb +54 -28
  138. data/spec/inputs/url_input_spec.rb +5 -5
  139. data/spec/inputs/with_options_spec.rb +7 -7
  140. data/spec/localizer_spec.rb +39 -17
  141. data/spec/namespaced_class_finder_spec.rb +79 -0
  142. data/spec/schema.rb +21 -0
  143. data/spec/spec_helper.rb +254 -221
  144. data/spec/support/custom_macros.rb +128 -95
  145. data/spec/support/shared_examples.rb +12 -0
  146. data/spec/support/specialized_class_finder_shared_example.rb +27 -0
  147. data/spec/support/test_environment.rb +26 -10
  148. metadata +177 -238
  149. data/.travis.yml +0 -8
  150. data/Appraisals +0 -11
  151. data/CHANGELOG +0 -371
  152. data/gemfiles/rails-3.0.gemfile +0 -7
  153. data/gemfiles/rails-3.1.gemfile +0 -7
  154. data/gemfiles/rails-3.2.gemfile +0 -7
  155. data/lib/formtastic/helpers/buttons_helper.rb +0 -310
  156. data/lib/formtastic/inputs/base/grouped_collections.rb +0 -77
  157. data/lib/formtastic/util.rb +0 -25
  158. data/lib/tasks/verify_rcov.rb +0 -44
  159. data/spec/helpers/buttons_helper_spec.rb +0 -166
  160. data/spec/helpers/commit_button_helper_spec.rb +0 -530
  161. data/spec/inputs/date_input_spec.rb +0 -227
  162. data/spec/inputs/datetime_input_spec.rb +0 -185
  163. data/spec/inputs/time_input_spec.rb +0 -267
  164. data/spec/support/deferred_garbage_collection.rb +0 -21
@@ -1,10 +1,15 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- describe 'check_boxes input' do
4
+ RSpec.describe 'check_boxes input' do
5
5
 
6
6
  include FormtasticSpecHelper
7
7
 
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
8
13
  describe 'for a has_many association' do
9
14
  before do
10
15
  @output_buffer = ''
@@ -26,75 +31,75 @@ describe 'check_boxes input' do
26
31
  it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
27
32
 
28
33
  it 'should generate a legend containing a label with text for the input' do
29
- output_buffer.should have_tag('form li fieldset legend.label label')
30
- output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
34
+ expect(output_buffer).to have_tag('form li fieldset legend.label label')
35
+ expect(output_buffer).to have_tag('form li fieldset legend.label label', :text => /Posts/)
31
36
  end
32
37
 
33
38
  it 'should not link the label within the legend to any input' do
34
- output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
39
+ expect(output_buffer).not_to have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
35
40
  end
36
41
 
37
42
  it 'should generate an ordered list with an li.choice for each choice' do
38
- output_buffer.should have_tag('form li fieldset ol')
39
- output_buffer.should have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
43
+ expect(output_buffer).to have_tag('form li fieldset ol')
44
+ expect(output_buffer).to have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
40
45
  end
41
46
 
42
47
  it 'should have one option with a "checked" attribute' do
43
- output_buffer.should have_tag('form li input[@checked]', :count => 1)
48
+ expect(output_buffer).to have_tag('form li input[@checked]', :count => 1)
44
49
  end
45
50
 
46
51
  it 'should not generate hidden inputs with default value blank' do
47
- output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
52
+ expect(output_buffer).not_to have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
48
53
  end
49
54
 
50
55
  it 'should not render hidden inputs inside the ol' do
51
- output_buffer.should_not have_tag("form li fieldset ol li input[@type='hidden']")
56
+ expect(output_buffer).not_to have_tag("form li fieldset ol li input[@type='hidden']")
52
57
  end
53
58
 
54
59
  it 'should render one hidden input for each choice outside the ol' do
55
- output_buffer.should have_tag("form li fieldset > input[@type='hidden']", :count => 1)
60
+ expect(output_buffer).to have_tag("form li fieldset > input[@type='hidden']", :count => 1)
56
61
  end
57
62
 
58
63
  describe "each choice" do
59
-
64
+
60
65
  it 'should not give the choice label the .label class' do
61
- output_buffer.should_not have_tag('li.choice label.label')
66
+ expect(output_buffer).not_to have_tag('li.choice label.label')
62
67
  end
63
-
68
+
64
69
  it 'should not be marked as required' do
65
- output_buffer.should_not have_tag('li.choice input[@required]')
70
+ expect(output_buffer).not_to have_tag('li.choice input[@required]')
66
71
  end
67
-
72
+
68
73
  it 'should contain a label for the radio input with a nested input and label text' do
69
74
  ::Post.all.each do |post|
70
- output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
71
- output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
75
+ expect(output_buffer).to have_tag('form li fieldset ol li label', :text => /#{post.to_label}/)
76
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
72
77
  end
73
78
  end
74
79
 
75
80
  it 'should use values as li.class when value_as_class is true' do
76
81
  ::Post.all.each do |post|
77
- output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
82
+ expect(output_buffer).to have_tag("form li fieldset ol li.post_#{post.id} label")
78
83
  end
79
84
  end
80
-
85
+
81
86
  it 'should have a checkbox input but no hidden field for each post' do
82
87
  ::Post.all.each do |post|
83
- output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
84
- output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
88
+ expect(output_buffer).to have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
89
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
85
90
  end
86
91
  end
87
92
 
88
93
  it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
89
- output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
94
+ expect(output_buffer).to have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
90
95
  end
91
96
 
92
97
  it 'the hidden field with an empty array value should be followed by the ol' do
93
- output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
98
+ expect(output_buffer).to have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
94
99
  end
95
100
 
96
101
  it 'should not have a hidden field with an empty string value for the collection' do
97
- output_buffer.should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
102
+ expect(output_buffer).not_to have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
98
103
  end
99
104
 
100
105
  it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
@@ -105,20 +110,21 @@ describe 'check_boxes input' do
105
110
  end)
106
111
 
107
112
  ::Post.all.each do |post|
108
- output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
109
- output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
113
+ expect(output_buffer).to have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
114
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
115
+ expect(output_buffer).to have_tag('form li fieldset ol li label', :text => /#{post.to_label}/)
110
116
  end
111
117
 
112
118
  end
113
119
 
114
120
  it "should mark input as checked if it's the the existing choice" do
115
- ::Post.all.include?(@fred.posts.first).should be_true
116
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
121
+ expect(::Post.all.include?(@fred.posts.first)).to be_truthy
122
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@checked='checked']")
117
123
  end
118
124
  end
119
125
 
120
126
  describe 'and no object is given' do
121
- before(:each) do
127
+ before(:example) do
122
128
  output_buffer.replace ''
123
129
  concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
124
130
  concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.all))
@@ -126,26 +132,26 @@ describe 'check_boxes input' do
126
132
  end
127
133
 
128
134
  it 'should generate a fieldset with legend' do
129
- output_buffer.should have_tag('form li fieldset legend', /Author/)
135
+ expect(output_buffer).to have_tag('form li fieldset legend', :text => /Author/)
130
136
  end
131
137
 
132
138
  it 'shold generate an li tag for each item in the collection' do
133
- output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.all.size)
139
+ expect(output_buffer).to have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.all.size)
134
140
  end
135
141
 
136
142
  it 'should generate labels for each item' do
137
143
  ::Author.all.each do |author|
138
- output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
139
- output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
144
+ expect(output_buffer).to have_tag('form li fieldset ol li label', :text => /#{author.to_label}/)
145
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
140
146
  end
141
147
  end
142
148
 
143
149
  it 'should generate inputs for each item' do
144
150
  ::Author.all.each do |author|
145
- output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
146
- output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
147
- output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
148
- output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
151
+ expect(output_buffer).to have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
152
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@type='checkbox']")
153
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='#{author.id}']")
154
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
149
155
  end
150
156
  end
151
157
 
@@ -154,8 +160,9 @@ describe 'check_boxes input' do
154
160
  concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
155
161
  end)
156
162
 
157
- output_buffer.should have_tag('form li fieldset ol li label') do |label|
158
- label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
163
+ expect(output_buffer).to have_tag('form li fieldset ol li label', text: %r{<b>Item [12]</b>}, count: 2) do |label|
164
+ expect(label).to have_text('<b>Item 1</b>', count: 1)
165
+ expect(label).to have_text('<b>Item 2</b>', count: 1)
159
166
  end
160
167
  end
161
168
  end
@@ -172,18 +179,18 @@ describe 'check_boxes input' do
172
179
 
173
180
  it 'should have a checkbox input for each post' do
174
181
  ::Post.all.each do |post|
175
- output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
176
- output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.all.length)
182
+ expect(output_buffer).to have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
183
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.all.length)
177
184
  end
178
185
  end
179
186
 
180
187
  it "should mark input as checked if it's the the existing choice" do
181
- ::Post.all.include?(@fred.posts.first).should be_true
182
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
188
+ expect(::Post.all.include?(@fred.posts.first)).to be_truthy
189
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@checked='checked']")
183
190
  end
184
191
 
185
192
  it 'should not generate empty hidden inputs' do
186
- output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
193
+ expect(output_buffer).not_to have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
187
194
  end
188
195
  end
189
196
 
@@ -194,7 +201,7 @@ describe 'check_boxes input' do
194
201
 
195
202
  describe "no disabled items" do
196
203
  before do
197
- @new_post.stub!(:author_ids).and_return(nil)
204
+ allow(@new_post).to receive(:author_ids).and_return(nil)
198
205
 
199
206
  concat(semantic_form_for(@new_post) do |builder|
200
207
  concat(builder.input(:authors, :as => :check_boxes, :disabled => nil))
@@ -202,13 +209,13 @@ describe 'check_boxes input' do
202
209
  end
203
210
 
204
211
  it 'should not have any disabled item(s)' do
205
- output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
212
+ expect(output_buffer).not_to have_tag("form li fieldset ol li label input[@disabled='disabled']")
206
213
  end
207
214
  end
208
215
 
209
216
  describe "single disabled item" do
210
217
  before do
211
- @new_post.stub!(:author_ids).and_return(nil)
218
+ allow(@new_post).to receive(:author_ids).and_return(nil)
212
219
 
213
220
  concat(semantic_form_for(@new_post) do |builder|
214
221
  concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id))
@@ -216,15 +223,15 @@ describe 'check_boxes input' do
216
223
  end
217
224
 
218
225
  it "should have one item disabled; the specified one" do
219
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
220
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
221
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
226
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
227
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", :text => /fred/i)
228
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
222
229
  end
223
230
  end
224
231
 
225
232
  describe "multiple disabled items" do
226
233
  before do
227
- @new_post.stub!(:author_ids).and_return(nil)
234
+ allow(@new_post).to receive(:author_ids).and_return(nil)
228
235
 
229
236
  concat(semantic_form_for(@new_post) do |builder|
230
237
  concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id]))
@@ -232,11 +239,11 @@ describe 'check_boxes input' do
232
239
  end
233
240
 
234
241
  it "should have multiple items disabled; the specified ones" do
235
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
236
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
237
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
238
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
239
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
242
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
243
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", :text => /bob/i)
244
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
245
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", :text => /fred/i)
246
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
240
247
  end
241
248
  end
242
249
 
@@ -247,7 +254,7 @@ describe 'check_boxes input' do
247
254
  before do
248
255
  ::I18n.backend.store_translations :en, :formtastic => { :labels => { :post => { :authors => "Translated!" }}}
249
256
  with_config :i18n_lookups_by_default, true do
250
- @new_post.stub!(:author_ids).and_return(nil)
257
+ allow(@new_post).to receive(:author_ids).and_return(nil)
251
258
  concat(semantic_form_for(@new_post) do |builder|
252
259
  concat(builder.input(:authors, :as => :check_boxes))
253
260
  end)
@@ -259,56 +266,72 @@ describe 'check_boxes input' do
259
266
  end
260
267
 
261
268
  it "should do foo" do
262
- output_buffer.should have_tag("legend.label label", /Translated/)
269
+ expect(output_buffer).to have_tag("legend.label label", :text => /Translated/)
263
270
  end
264
271
 
265
272
  end
266
273
 
267
274
  describe "when :label option is set" do
268
275
  before do
269
- @new_post.stub!(:author_ids).and_return(nil)
276
+ allow(@new_post).to receive(:author_ids).and_return(nil)
270
277
  concat(semantic_form_for(@new_post) do |builder|
271
278
  concat(builder.input(:authors, :as => :check_boxes, :label => 'The authors'))
272
279
  end)
273
280
  end
274
281
 
275
282
  it "should output the correct label title" do
276
- output_buffer.should have_tag("legend.label label", /The authors/)
283
+ expect(output_buffer).to have_tag("legend.label label", :text => /The authors/)
277
284
  end
278
285
  end
279
286
 
280
287
  describe "when :label option is false" do
281
288
  before do
282
289
  @output_buffer = ''
283
- @new_post.stub!(:author_ids).and_return(nil)
290
+ allow(@new_post).to receive(:author_ids).and_return(nil)
284
291
  concat(semantic_form_for(@new_post) do |builder|
285
292
  concat(builder.input(:authors, :as => :check_boxes, :label => false))
286
293
  end)
287
294
  end
288
295
 
289
296
  it "should not output the legend" do
290
- output_buffer.should_not have_tag("legend.label")
297
+ expect(output_buffer).not_to have_tag("legend.label")
291
298
  end
292
-
299
+
293
300
  it "should not cause escaped HTML" do
294
- output_buffer.should_not include("&gt;")
301
+ expect(output_buffer).not_to include("&gt;")
295
302
  end
296
-
303
+
297
304
  end
298
305
 
299
306
  describe "when :required option is true" do
300
307
  before do
301
- @new_post.stub!(:author_ids).and_return(nil)
308
+ allow(@new_post).to receive(:author_ids).and_return(nil)
302
309
  concat(semantic_form_for(@new_post) do |builder|
303
310
  concat(builder.input(:authors, :as => :check_boxes, :required => true))
304
311
  end)
305
312
  end
306
313
 
307
314
  it "should output the correct label title" do
308
- output_buffer.should have_tag("legend.label label abbr")
315
+ expect(output_buffer).to have_tag("legend.label label abbr")
309
316
  end
310
317
  end
318
+ end
311
319
 
320
+ describe 'for a enum column' do
321
+ before do
322
+ allow(@new_post).to receive(:status) { 'inactive' }
323
+ statuses = ActiveSupport::HashWithIndifferentAccess.new("active"=>0, "inactive"=>1)
324
+ allow(@new_post.class).to receive(:statuses) { statuses }
325
+ allow(@new_post).to receive(:defined_enums) { { "status" => statuses } }
326
+ end
327
+
328
+ it 'should have a select inside the wrapper' do
329
+ expect {
330
+ concat(semantic_form_for(@new_post) do |builder|
331
+ concat(builder.input(:status, :as => :check_boxes))
332
+ end)
333
+ }.to raise_error(Formtastic::UnsupportedEnumCollection)
334
+ end
312
335
  end
313
336
 
314
337
  describe 'for a has_and_belongs_to_many association' do
@@ -324,14 +347,39 @@ describe 'check_boxes input' do
324
347
 
325
348
  it 'should render checkboxes' do
326
349
  # I'm aware these two lines test the same thing
327
- output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
328
- output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.all.size)
350
+ expect(output_buffer).to have_tag('input[type="checkbox"]', :count => 2)
351
+ expect(output_buffer).to have_tag('input[type="checkbox"]', :count => ::Author.all.size)
329
352
  end
330
353
 
331
354
  it 'should only select checkboxes that are present in the association' do
332
355
  # I'm aware these two lines test the same thing
333
- output_buffer.should have_tag('input[checked="checked"]', :count => 1)
334
- output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
356
+ expect(output_buffer).to have_tag('input[checked="checked"]', :count => 1)
357
+ expect(output_buffer).to have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
358
+ end
359
+
360
+ end
361
+
362
+ describe ':collection for a has_and_belongs_to_many association' do
363
+
364
+ before do
365
+ @output_buffer = ''
366
+ mock_everything
367
+
368
+ concat(semantic_form_for(@freds_post) do |builder|
369
+ concat(builder.input(:authors, as: :check_boxes, collection: Author.all))
370
+ end)
371
+ end
372
+
373
+ it 'should render checkboxes' do
374
+ # I'm aware these two lines test the same thing
375
+ expect(output_buffer).to have_tag('input[type="checkbox"]', :count => 2)
376
+ expect(output_buffer).to have_tag('input[type="checkbox"]', :count => ::Author.all.size)
377
+ end
378
+
379
+ it 'should only select checkboxes that are present in the association' do
380
+ # I'm aware these two lines test the same thing
381
+ expect(output_buffer).to have_tag('input[checked="checked"]', :count => 1)
382
+ expect(output_buffer).to have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
335
383
  end
336
384
 
337
385
  end
@@ -344,33 +392,54 @@ describe 'check_boxes input' do
344
392
  end
345
393
 
346
394
  it 'to set the right input value' do
347
- item = mock('item')
348
- item.should_not_receive(:id)
349
- item.stub!(:custom_value).and_return('custom_value')
350
- item.should_receive(:custom_value).exactly(3).times
351
- @new_post.author.should_receive(:custom_value).exactly(1).times
352
- concat(semantic_form_for(@new_post) do |builder|
353
- concat(builder.input(:author, :as => :check_boxes, :member_value => :custom_value, :collection => [item, item, item]))
354
- end)
355
- output_buffer.should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
395
+ item = double('item')
396
+ expect(item).not_to receive(:id)
397
+ allow(item).to receive(:custom_value).and_return('custom_value')
398
+ expect(item).to receive(:custom_value).exactly(3).times
399
+ expect(@new_post.author).to receive(:custom_value).exactly(1).times
400
+
401
+ with_deprecation_silenced do
402
+ concat(semantic_form_for(@new_post) do |builder|
403
+ concat(builder.input(:author, :as => :check_boxes, :member_value => :custom_value, :collection => [item, item, item]))
404
+ end)
405
+ end
406
+ expect(output_buffer).to have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
356
407
  end
357
408
  end
358
409
  end
359
-
410
+
360
411
  describe 'when :collection is provided as an array of arrays' do
361
412
  before do
362
413
  @output_buffer = ''
363
414
  mock_everything
364
- @fred.stub(:genres) { ['fiction', 'biography'] }
365
-
415
+ allow(@fred).to receive(:genres) { ['fiction', 'biography'] }
416
+
366
417
  concat(semantic_form_for(@fred) do |builder|
367
418
  concat(builder.input(:genres, :as => :check_boxes, :collection => [['Fiction', 'fiction'], ['Non-fiction', 'non_fiction'], ['Biography', 'biography']]))
368
419
  end)
369
420
  end
370
-
421
+
371
422
  it 'should check the correct checkboxes' do
372
- output_buffer.should have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
373
- output_buffer.should have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
423
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
424
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
425
+ end
426
+ end
427
+
428
+ describe 'when :collection is a set' do
429
+ before do
430
+ @output_buffer = ''
431
+ mock_everything
432
+ allow(@fred).to receive(:roles) { Set.new([:reviewer, :admin]) }
433
+
434
+ concat(semantic_form_for(@fred) do |builder|
435
+ concat(builder.input(:roles, :as => :check_boxes, :collection => [['User', :user], ['Reviewer', :reviewer], ['Administrator', :admin]]))
436
+ end)
437
+ end
438
+
439
+ it 'should check the correct checkboxes' do
440
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='user']")
441
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='admin'][@checked='checked']")
442
+ expect(output_buffer).to have_tag("form li fieldset ol li label input[@value='reviewer'][@checked='checked']")
374
443
  end
375
444
  end
376
445
 
@@ -384,15 +453,15 @@ describe 'check_boxes input' do
384
453
  concat(builder.input(:posts, :as => :check_boxes))
385
454
  end)
386
455
  end
387
-
456
+
388
457
  it "should have a label for #context2_author_post_ids_19" do
389
- output_buffer.should have_tag("form li label[@for='context2_author_post_ids_19']")
458
+ expect(output_buffer).to have_tag("form li label[@for='context2_author_post_ids_19']")
390
459
  end
391
460
 
392
461
  it_should_have_input_with_id('context2_author_post_ids_19')
393
462
  it_should_have_input_wrapper_with_id("context2_author_posts_input")
394
463
  end
395
-
464
+
396
465
  describe "when index is provided" do
397
466
 
398
467
  before do
@@ -405,21 +474,21 @@ describe 'check_boxes input' do
405
474
  end)
406
475
  end)
407
476
  end
408
-
477
+
409
478
  it 'should index the id of the wrapper' do
410
- output_buffer.should have_tag("li#author_post_3_authors_input")
479
+ expect(output_buffer).to have_tag("li#author_post_3_authors_input")
411
480
  end
412
-
481
+
413
482
  it 'should index the id of the input tag' do
414
- output_buffer.should have_tag("input#author_post_3_author_ids_42")
483
+ expect(output_buffer).to have_tag("input#author_post_3_author_ids_42")
415
484
  end
416
485
 
417
486
  it 'should index the name of the checkbox input' do
418
- output_buffer.should have_tag("input[@type='checkbox'][@name='author[post][3][author_ids][]']")
487
+ expect(output_buffer).to have_tag("input[@type='checkbox'][@name='author[post][3][author_ids][]']")
419
488
  end
420
-
489
+
421
490
  end
422
-
491
+
423
492
 
424
493
  describe "when collection is an array" do
425
494
  before do
@@ -434,15 +503,45 @@ describe 'check_boxes input' do
434
503
 
435
504
  it "should use array items for labels and values" do
436
505
  @_collection.each do |post|
437
- output_buffer.should have_tag('form li fieldset ol li label', /#{post.first}/)
438
- output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
506
+ expect(output_buffer).to have_tag('form li fieldset ol li label', :text => /#{post.first}/)
507
+ expect(output_buffer).to have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
439
508
  end
440
509
  end
441
510
 
442
511
  it "should not check any items" do
443
- output_buffer.should have_tag('form li input[@checked]', :count => 0)
444
- end
512
+ expect(output_buffer).to have_tag('form li input[@checked]', :count => 0)
513
+ end
514
+
515
+ describe "and the attribute has values" do
516
+ before do
517
+ allow(@fred).to receive(:posts) { [1] }
518
+
519
+ concat(semantic_form_for(@fred) do |builder|
520
+ concat(builder.input(:posts, :as => :check_boxes, :collection => @_collection))
521
+ end)
522
+ end
523
+
524
+ it "should check the appropriate items" do
525
+ expect(output_buffer).to have_tag("form li input[@value='1'][@checked]")
526
+ end
527
+ end
528
+
529
+ describe "and the collection includes html options" do
530
+ before do
531
+ @_collection = [["First", 1, {'data-test' => 'test-data'}], ["Second", 2, {'data-test2' => 'test-data2'}]]
532
+
533
+ concat(semantic_form_for(@fred) do |builder|
534
+ concat(builder.input(:posts, :as => :check_boxes, :collection => @_collection))
535
+ end)
536
+ end
537
+
538
+ it "should have injected the html attributes" do
539
+ @_collection.each do |v|
540
+ expect(output_buffer).to have_tag("form li input[@value='#{v[1]}'][@#{v[2].keys[0]}='#{v[2].values[0]}']")
541
+ end
542
+ end
543
+ end
445
544
  end
446
-
545
+
447
546
  end
448
547
 
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ RSpec.describe 'color input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe "when object is provided" do
14
+ before do
15
+ concat(semantic_form_for(@new_post) do |builder|
16
+ concat(builder.input(:color))
17
+ end)
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class(:color)
21
+ it_should_have_input_wrapper_with_class(:input)
22
+ it_should_have_input_wrapper_with_class(:stringish)
23
+ it_should_have_input_wrapper_with_id("post_color_input")
24
+ it_should_have_label_with_text(/Color/)
25
+ it_should_have_label_for("post_color")
26
+ it_should_have_input_with_id("post_color")
27
+ it_should_have_input_with_type(:color)
28
+ it_should_have_input_with_name("post[color]")
29
+
30
+ end
31
+
32
+ describe "when namespace is provided" do
33
+
34
+ before do
35
+ concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
36
+ concat(builder.input(:color))
37
+ end)
38
+ end
39
+
40
+ it_should_have_input_wrapper_with_id("context2_post_color_input")
41
+ it_should_have_label_and_input_with_id("context2_post_color")
42
+
43
+ end
44
+
45
+ describe "when index is provided" do
46
+
47
+ before do
48
+ @output_buffer = ''
49
+ mock_everything
50
+
51
+ concat(semantic_form_for(@new_post) do |builder|
52
+ concat(builder.fields_for(:author, :index => 3) do |author|
53
+ concat(author.input(:name, :as => :color))
54
+ end)
55
+ end)
56
+ end
57
+
58
+ it 'should index the id of the wrapper' do
59
+ expect(output_buffer).to have_tag("li#post_author_attributes_3_name_input")
60
+ end
61
+
62
+ it 'should index the id of the select tag' do
63
+ expect(output_buffer).to have_tag("input#post_author_attributes_3_name")
64
+ end
65
+
66
+ it 'should index the name of the select tag' do
67
+ expect(output_buffer).to have_tag("input[@name='post[author_attributes][3][name]']")
68
+ end
69
+
70
+ end
71
+
72
+
73
+ describe "when required" do
74
+ it "should add the required attribute to the input's html options" do
75
+ with_config :use_required_attribute, true do
76
+ concat(semantic_form_for(@new_post) do |builder|
77
+ concat(builder.input(:title, :as => :color, :required => true))
78
+ end)
79
+ expect(output_buffer).to have_tag("input[@required]")
80
+ end
81
+ end
82
+ end
83
+
84
+ end
85
+