formtastic 3.1.3 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/test.yml +61 -0
  4. data/.gitignore +3 -2
  5. data/CHANGELOG.md +61 -0
  6. data/Gemfile.lock +140 -0
  7. data/MIT-LICENSE +1 -1
  8. data/{README.textile → README.md} +183 -167
  9. data/RELEASE_PROCESS +3 -1
  10. data/Rakefile +20 -1
  11. data/app/assets/stylesheets/formtastic.css +1 -1
  12. data/bin/appraisal +8 -0
  13. data/formtastic.gemspec +12 -16
  14. data/gemfiles/rails_6.0/Gemfile +5 -0
  15. data/gemfiles/rails_6.1/Gemfile +5 -0
  16. data/gemfiles/rails_7.0/Gemfile +5 -0
  17. data/gemfiles/rails_7.1/Gemfile +5 -0
  18. data/gemfiles/rails_edge/Gemfile +13 -0
  19. data/lib/formtastic/action_class_finder.rb +1 -0
  20. data/lib/formtastic/actions/base.rb +1 -0
  21. data/lib/formtastic/actions/button_action.rb +1 -0
  22. data/lib/formtastic/actions/buttonish.rb +1 -0
  23. data/lib/formtastic/actions/input_action.rb +1 -0
  24. data/lib/formtastic/actions/link_action.rb +1 -0
  25. data/lib/formtastic/actions.rb +7 -3
  26. data/lib/formtastic/deprecation.rb +2 -38
  27. data/lib/formtastic/engine.rb +4 -1
  28. data/lib/formtastic/form_builder.rb +12 -24
  29. data/lib/formtastic/helpers/action_helper.rb +2 -48
  30. data/lib/formtastic/helpers/actions_helper.rb +1 -0
  31. data/lib/formtastic/helpers/enum.rb +14 -0
  32. data/lib/formtastic/helpers/errors_helper.rb +3 -2
  33. data/lib/formtastic/helpers/fieldset_wrapper.rb +14 -9
  34. data/lib/formtastic/helpers/file_column_detection.rb +1 -0
  35. data/lib/formtastic/helpers/form_helper.rb +2 -1
  36. data/lib/formtastic/helpers/input_helper.rb +20 -76
  37. data/lib/formtastic/helpers/inputs_helper.rb +29 -23
  38. data/lib/formtastic/helpers/reflection.rb +1 -0
  39. data/lib/formtastic/helpers.rb +2 -2
  40. data/lib/formtastic/html_attributes.rb +1 -0
  41. data/lib/formtastic/i18n.rb +2 -1
  42. data/lib/formtastic/input_class_finder.rb +1 -0
  43. data/lib/formtastic/inputs/base/associations.rb +1 -0
  44. data/lib/formtastic/inputs/base/choices.rb +3 -2
  45. data/lib/formtastic/inputs/base/collections.rb +46 -10
  46. data/lib/formtastic/inputs/base/database.rb +5 -7
  47. data/lib/formtastic/inputs/base/datetime_pickerish.rb +1 -0
  48. data/lib/formtastic/inputs/base/errors.rb +7 -6
  49. data/lib/formtastic/inputs/base/fileish.rb +1 -0
  50. data/lib/formtastic/inputs/base/hints.rb +2 -1
  51. data/lib/formtastic/inputs/base/html.rb +9 -7
  52. data/lib/formtastic/inputs/base/labelling.rb +3 -2
  53. data/lib/formtastic/inputs/base/naming.rb +5 -4
  54. data/lib/formtastic/inputs/base/numeric.rb +1 -0
  55. data/lib/formtastic/inputs/base/options.rb +3 -3
  56. data/lib/formtastic/inputs/base/placeholder.rb +1 -0
  57. data/lib/formtastic/inputs/base/stringish.rb +1 -0
  58. data/lib/formtastic/inputs/base/timeish.rb +9 -4
  59. data/lib/formtastic/inputs/base/validations.rb +39 -12
  60. data/lib/formtastic/inputs/base/wrapping.rb +1 -0
  61. data/lib/formtastic/inputs/base.rb +3 -2
  62. data/lib/formtastic/inputs/boolean_input.rb +2 -1
  63. data/lib/formtastic/inputs/check_boxes_input.rb +15 -6
  64. data/lib/formtastic/inputs/color_input.rb +1 -1
  65. data/lib/formtastic/inputs/country_input.rb +4 -1
  66. data/lib/formtastic/inputs/datalist_input.rb +1 -0
  67. data/lib/formtastic/inputs/date_picker_input.rb +1 -0
  68. data/lib/formtastic/inputs/date_select_input.rb +1 -0
  69. data/lib/formtastic/inputs/datetime_picker_input.rb +1 -0
  70. data/lib/formtastic/inputs/datetime_select_input.rb +1 -0
  71. data/lib/formtastic/inputs/email_input.rb +1 -0
  72. data/lib/formtastic/inputs/file_input.rb +1 -0
  73. data/lib/formtastic/inputs/hidden_input.rb +3 -2
  74. data/lib/formtastic/inputs/number_input.rb +1 -0
  75. data/lib/formtastic/inputs/password_input.rb +1 -0
  76. data/lib/formtastic/inputs/phone_input.rb +1 -0
  77. data/lib/formtastic/inputs/radio_input.rb +21 -0
  78. data/lib/formtastic/inputs/range_input.rb +1 -0
  79. data/lib/formtastic/inputs/search_input.rb +1 -0
  80. data/lib/formtastic/inputs/select_input.rb +30 -1
  81. data/lib/formtastic/inputs/string_input.rb +1 -0
  82. data/lib/formtastic/inputs/text_input.rb +1 -0
  83. data/lib/formtastic/inputs/time_picker_input.rb +1 -0
  84. data/lib/formtastic/inputs/time_select_input.rb +1 -0
  85. data/lib/formtastic/inputs/time_zone_input.rb +17 -6
  86. data/lib/formtastic/inputs/url_input.rb +1 -0
  87. data/lib/formtastic/inputs.rb +33 -29
  88. data/lib/formtastic/localized_string.rb +1 -0
  89. data/lib/formtastic/localizer.rb +21 -22
  90. data/lib/formtastic/namespaced_class_finder.rb +8 -9
  91. data/lib/formtastic/version.rb +2 -1
  92. data/lib/formtastic.rb +10 -11
  93. data/lib/generators/formtastic/form/form_generator.rb +2 -1
  94. data/lib/generators/formtastic/input/input_generator.rb +47 -0
  95. data/lib/generators/formtastic/install/install_generator.rb +1 -0
  96. data/lib/generators/templates/formtastic.rb +15 -13
  97. data/lib/generators/templates/input.rb +19 -0
  98. data/sample/basic_inputs.html +1 -1
  99. data/script/integration-template.rb +73 -0
  100. data/script/integration.sh +19 -0
  101. data/spec/action_class_finder_spec.rb +2 -1
  102. data/spec/actions/button_action_spec.rb +21 -20
  103. data/spec/actions/generic_action_spec.rb +134 -133
  104. data/spec/actions/input_action_spec.rb +20 -19
  105. data/spec/actions/link_action_spec.rb +30 -29
  106. data/spec/builder/custom_builder_spec.rb +39 -22
  107. data/spec/builder/error_proc_spec.rb +6 -5
  108. data/spec/builder/semantic_fields_for_spec.rb +46 -45
  109. data/spec/fast_spec_helper.rb +13 -0
  110. data/spec/generators/formtastic/form/form_generator_spec.rb +33 -32
  111. data/spec/generators/formtastic/input/input_generator_spec.rb +125 -0
  112. data/spec/generators/formtastic/install/install_generator_spec.rb +10 -9
  113. data/spec/helpers/action_helper_spec.rb +329 -10
  114. data/spec/helpers/actions_helper_spec.rb +43 -42
  115. data/spec/helpers/form_helper_spec.rb +45 -38
  116. data/spec/helpers/input_helper_spec.rb +976 -2
  117. data/spec/helpers/inputs_helper_spec.rb +217 -202
  118. data/spec/helpers/reflection_helper_spec.rb +7 -6
  119. data/spec/helpers/semantic_errors_helper_spec.rb +26 -25
  120. data/spec/i18n_spec.rb +30 -29
  121. data/spec/input_class_finder_spec.rb +2 -1
  122. data/spec/inputs/base/collections_spec.rb +78 -0
  123. data/spec/inputs/base/validations_spec.rb +481 -0
  124. data/spec/inputs/boolean_input_spec.rb +73 -72
  125. data/spec/inputs/check_boxes_input_spec.rb +169 -121
  126. data/spec/inputs/color_input_spec.rb +53 -64
  127. data/spec/inputs/country_input_spec.rb +23 -22
  128. data/spec/inputs/custom_input_spec.rb +3 -6
  129. data/spec/inputs/datalist_input_spec.rb +3 -2
  130. data/spec/inputs/date_picker_input_spec.rb +114 -113
  131. data/spec/inputs/date_select_input_spec.rb +76 -61
  132. data/spec/inputs/datetime_picker_input_spec.rb +123 -122
  133. data/spec/inputs/datetime_select_input_spec.rb +85 -68
  134. data/spec/inputs/email_input_spec.rb +17 -16
  135. data/spec/inputs/file_input_spec.rb +18 -17
  136. data/spec/inputs/hidden_input_spec.rb +32 -31
  137. data/spec/inputs/include_blank_spec.rb +10 -9
  138. data/spec/inputs/label_spec.rb +26 -25
  139. data/spec/inputs/number_input_spec.rb +212 -211
  140. data/spec/inputs/password_input_spec.rb +17 -16
  141. data/spec/inputs/phone_input_spec.rb +17 -16
  142. data/spec/inputs/placeholder_spec.rb +18 -17
  143. data/spec/inputs/radio_input_spec.rb +92 -65
  144. data/spec/inputs/range_input_spec.rb +136 -135
  145. data/spec/inputs/readonly_spec.rb +51 -0
  146. data/spec/inputs/search_input_spec.rb +16 -15
  147. data/spec/inputs/select_input_spec.rb +209 -102
  148. data/spec/inputs/string_input_spec.rb +51 -50
  149. data/spec/inputs/text_input_spec.rb +34 -33
  150. data/spec/inputs/time_picker_input_spec.rb +115 -114
  151. data/spec/inputs/time_select_input_spec.rb +84 -70
  152. data/spec/inputs/time_zone_input_spec.rb +58 -31
  153. data/spec/inputs/url_input_spec.rb +17 -16
  154. data/spec/inputs/with_options_spec.rb +9 -8
  155. data/spec/localizer_spec.rb +18 -17
  156. data/spec/namespaced_class_finder_spec.rb +18 -6
  157. data/spec/schema.rb +22 -0
  158. data/spec/spec_helper.rb +172 -260
  159. data/spec/support/custom_macros.rb +74 -76
  160. data/spec/support/deprecation.rb +2 -1
  161. data/spec/support/shared_examples.rb +2 -1233
  162. data/spec/support/specialized_class_finder_shared_example.rb +1 -0
  163. data/spec/support/test_environment.rb +24 -9
  164. metadata +78 -170
  165. data/.travis.yml +0 -29
  166. data/Appraisals +0 -29
  167. data/CHANGELOG +0 -39
  168. data/DEPRECATIONS +0 -49
  169. data/gemfiles/rails_3.2.gemfile +0 -7
  170. data/gemfiles/rails_4.0.4.gemfile +0 -7
  171. data/gemfiles/rails_4.1.gemfile +0 -7
  172. data/gemfiles/rails_4.2.gemfile +0 -7
  173. data/gemfiles/rails_4.gemfile +0 -7
  174. data/gemfiles/rails_edge.gemfile +0 -10
  175. data/lib/formtastic/util.rb +0 -57
  176. data/spec/helpers/namespaced_action_helper_spec.rb +0 -43
  177. data/spec/helpers/namespaced_input_helper_spec.rb +0 -36
  178. data/spec/support/deferred_garbage_collection.rb +0 -21
  179. data/spec/util_spec.rb +0 -66
@@ -1,12 +1,13 @@
1
1
  # encoding: utf-8
2
+ # frozen_string_literal: true
2
3
  require 'spec_helper'
3
4
 
4
- describe 'range input' do
5
+ RSpec.describe 'range input' do
5
6
 
6
7
  include FormtasticSpecHelper
7
8
 
8
9
  before do
9
- @output_buffer = ''
10
+ @output_buffer = ActionView::OutputBuffer.new ''
10
11
  mock_everything
11
12
  end
12
13
 
@@ -42,11 +43,11 @@ describe 'range input' do
42
43
  it_should_have_label_and_input_with_id("context2_author_age")
43
44
 
44
45
  end
45
-
46
+
46
47
  describe "when index is provided" do
47
48
 
48
49
  before do
49
- @output_buffer = ''
50
+ @output_buffer = ActionView::OutputBuffer.new ''
50
51
  mock_everything
51
52
 
52
53
  concat(semantic_form_for(@new_post) do |builder|
@@ -55,451 +56,451 @@ describe 'range input' do
55
56
  end)
56
57
  end)
57
58
  end
58
-
59
+
59
60
  it 'should index the id of the wrapper' do
60
- output_buffer.should have_tag("li#post_author_attributes_3_name_input")
61
+ expect(output_buffer.to_str).to have_tag("li#post_author_attributes_3_name_input")
61
62
  end
62
-
63
+
63
64
  it 'should index the id of the select tag' do
64
- output_buffer.should have_tag("input#post_author_attributes_3_name")
65
+ expect(output_buffer.to_str).to have_tag("input#post_author_attributes_3_name")
65
66
  end
66
-
67
+
67
68
  it 'should index the name of the select tag' do
68
- output_buffer.should have_tag("input[@name='post[author_attributes][3][name]']")
69
+ expect(output_buffer.to_str).to have_tag("input[@name='post[author_attributes][3][name]']")
69
70
  end
70
-
71
+
71
72
  end
72
-
73
+
73
74
 
74
75
  describe "when validations require a minimum value (:greater_than)" do
75
76
  before do
76
- @new_post.class.stub(:validators_on).with(:title).and_return([
77
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
77
78
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than=>2})
78
79
  ])
79
80
  end
80
-
81
+
81
82
  it "should allow :input_html to override :min" do
82
83
  concat(semantic_form_for(@new_post) do |builder|
83
84
  builder.input(:title, :as => :range, :input_html => { :min => 5 })
84
85
  end)
85
- output_buffer.should have_tag('input[@min="5"]')
86
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
86
87
  end
87
-
88
+
88
89
  it "should allow :input_html to override :min through :in" do
89
90
  concat(semantic_form_for(@new_post) do |builder|
90
91
  builder.input(:title, :as => :range, :input_html => { :in => 5..102 })
91
92
  end)
92
- output_buffer.should have_tag('input[@min="5"]')
93
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
93
94
  end
94
-
95
+
95
96
  it "should allow options to override :min" do
96
97
  concat(semantic_form_for(@new_post) do |builder|
97
98
  builder.input(:title, :as => :range, :min => 5)
98
99
  end)
99
- output_buffer.should have_tag('input[@min="5"]')
100
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
100
101
  end
101
-
102
+
102
103
  it "should allow options to override :min through :in" do
103
104
  concat(semantic_form_for(@new_post) do |builder|
104
105
  builder.input(:title, :as => :range, :in => 5..102)
105
106
  end)
106
- output_buffer.should have_tag('input[@min="5"]')
107
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
107
108
  end
108
-
109
+
109
110
  describe "and the column is an integer" do
110
111
  before do
111
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
112
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
112
113
  end
113
-
114
+
114
115
  it "should add a min attribute to the input one greater than the validation" do
115
116
  concat(semantic_form_for(@new_post) do |builder|
116
117
  builder.input(:title, :as => :range)
117
118
  end)
118
- output_buffer.should have_tag('input[@min="3"]')
119
+ expect(output_buffer.to_str).to have_tag('input[@min="3"]')
119
120
  end
120
121
  end
121
-
122
+
122
123
  describe "and the column is a float" do
123
124
  before do
124
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
125
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
125
126
  end
126
-
127
+
127
128
  it "should raise an error" do
128
- lambda {
129
+ expect {
129
130
  concat(semantic_form_for(@new_post) do |builder|
130
131
  builder.input(:title, :as => :range)
131
132
  end)
132
- }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
133
+ }.to raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
133
134
  end
134
135
  end
135
-
136
+
136
137
  describe "and the column is a big decimal" do
137
138
  before do
138
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
139
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
139
140
  end
140
-
141
+
141
142
  it "should raise an error" do
142
- lambda {
143
+ expect {
143
144
  concat(semantic_form_for(@new_post) do |builder|
144
145
  builder.input(:title, :as => :range)
145
146
  end)
146
- }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
147
+ }.to raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMinimumAttributeError)
147
148
  end
148
149
  end
149
-
150
+
150
151
  end
151
-
152
+
152
153
  describe "when validations require a minimum value (:greater_than_or_equal_to)" do
153
154
  before do
154
- @new_post.class.stub(:validators_on).with(:title).and_return([
155
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
155
156
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than_or_equal_to=>2})
156
157
  ])
157
158
  end
158
-
159
+
159
160
  it "should allow :input_html to override :min" do
160
161
  concat(semantic_form_for(@new_post) do |builder|
161
162
  builder.input(:title, :as => :range, :input_html => { :min => 5 })
162
163
  end)
163
- output_buffer.should have_tag('input[@min="5"]')
164
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
164
165
  end
165
-
166
+
166
167
  it "should allow options to override :min" do
167
168
  concat(semantic_form_for(@new_post) do |builder|
168
169
  builder.input(:title, :as => :range, :min => 5)
169
170
  end)
170
- output_buffer.should have_tag('input[@min="5"]')
171
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
171
172
  end
172
173
 
173
174
  it "should allow :input_html to override :min with :in" do
174
175
  concat(semantic_form_for(@new_post) do |builder|
175
176
  builder.input(:title, :as => :range, :input_html => { :in => 5..102 })
176
177
  end)
177
- output_buffer.should have_tag('input[@min="5"]')
178
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
178
179
  end
179
-
180
+
180
181
  it "should allow options to override :min with :in" do
181
182
  concat(semantic_form_for(@new_post) do |builder|
182
183
  builder.input(:title, :as => :range, :in => 5..102)
183
184
  end)
184
- output_buffer.should have_tag('input[@min="5"]')
185
+ expect(output_buffer.to_str).to have_tag('input[@min="5"]')
185
186
  end
186
-
187
+
187
188
 
188
189
  [:integer, :decimal, :float].each do |column_type|
189
190
  describe "and the column is a #{column_type}" do
190
191
  before do
191
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
192
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
192
193
  end
193
194
 
194
195
  it "should add a max attribute to the input equal to the validation" do
195
196
  concat(semantic_form_for(@new_post) do |builder|
196
197
  builder.input(:title, :as => :range)
197
198
  end)
198
- output_buffer.should have_tag('input[@min="2"]')
199
+ expect(output_buffer.to_str).to have_tag('input[@min="2"]')
199
200
  end
200
201
  end
201
202
  end
202
203
 
203
204
  describe "and there is no column" do
204
205
  before do
205
- @new_post.stub(:column_for_attribute).with(:title).and_return(nil)
206
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(nil)
206
207
  end
207
-
208
+
208
209
  it "should add a max attribute to the input equal to the validation" do
209
210
  concat(semantic_form_for(@new_post) do |builder|
210
211
  builder.input(:title, :as => :range)
211
212
  end)
212
- output_buffer.should have_tag('input[@min="2"]')
213
+ expect(output_buffer.to_str).to have_tag('input[@min="2"]')
213
214
  end
214
215
  end
215
216
  end
216
217
 
217
218
  describe "when validations do not require a minimum value" do
218
-
219
+
219
220
  it "should default to 1" do
220
221
  concat(semantic_form_for(@new_post) do |builder|
221
222
  builder.input(:title, :as => :range)
222
223
  end)
223
- output_buffer.should have_tag('input[@min="1"]')
224
+ expect(output_buffer.to_str).to have_tag('input[@min="1"]')
224
225
  end
225
-
226
+
226
227
  end
227
228
 
228
229
  describe "when validations require a maximum value (:less_than)" do
229
230
  before do
230
- @new_post.class.stub(:validators_on).with(:title).and_return([
231
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
231
232
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than=>20})
232
233
  ])
233
234
  end
234
-
235
+
235
236
  it "should allow :input_html to override :max" do
236
237
  concat(semantic_form_for(@new_post) do |builder|
237
238
  builder.input(:title, :as => :range, :input_html => { :max => 102 })
238
239
  end)
239
- output_buffer.should have_tag('input[@max="102"]')
240
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
240
241
  end
241
-
242
+
242
243
  it "should allow option to override :max" do
243
244
  concat(semantic_form_for(@new_post) do |builder|
244
245
  builder.input(:title, :as => :range, :max => 102)
245
246
  end)
246
- output_buffer.should have_tag('input[@max="102"]')
247
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
247
248
  end
248
-
249
+
249
250
  it "should allow :input_html to override :max with :in" do
250
251
  concat(semantic_form_for(@new_post) do |builder|
251
252
  builder.input(:title, :as => :range, :input_html => { :in => 1..102 })
252
253
  end)
253
- output_buffer.should have_tag('input[@max="102"]')
254
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
254
255
  end
255
256
 
256
257
  it "should allow option to override :max with :in" do
257
258
  concat(semantic_form_for(@new_post) do |builder|
258
259
  builder.input(:title, :as => :range, :in => 1..102)
259
260
  end)
260
- output_buffer.should have_tag('input[@max="102"]')
261
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
261
262
  end
262
-
263
+
263
264
  describe "and the column is an integer" do
264
265
  before do
265
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
266
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :integer))
266
267
  end
267
-
268
+
268
269
  it "should add a max attribute to the input one greater than the validation" do
269
270
  concat(semantic_form_for(@new_post) do |builder|
270
271
  builder.input(:title, :as => :range)
271
272
  end)
272
- output_buffer.should have_tag('input[@max="19"]')
273
+ expect(output_buffer.to_str).to have_tag('input[@max="19"]')
273
274
  end
274
275
  end
275
-
276
+
276
277
  describe "and the column is a float" do
277
278
  before do
278
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
279
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :float))
279
280
  end
280
-
281
+
281
282
  it "should raise an error" do
282
- lambda {
283
+ expect {
283
284
  concat(semantic_form_for(@new_post) do |builder|
284
285
  builder.input(:title, :as => :range)
285
286
  end)
286
- }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
287
+ }.to raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
287
288
  end
288
289
  end
289
-
290
+
290
291
  describe "and the column is a big decimal" do
291
292
  before do
292
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
293
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => :decimal))
293
294
  end
294
-
295
+
295
296
  it "should raise an error" do
296
- lambda {
297
+ expect {
297
298
  concat(semantic_form_for(@new_post) do |builder|
298
299
  builder.input(:title, :as => :range)
299
300
  end)
300
- }.should raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
301
+ }.to raise_error(Formtastic::Inputs::Base::Validations::IndeterminableMaximumAttributeError)
301
302
  end
302
303
  end
303
-
304
+
304
305
  end
305
-
306
+
306
307
  describe "when validations require a maximum value (:less_than_or_equal_to)" do
307
308
  before do
308
- @new_post.class.stub(:validators_on).with(:title).and_return([
309
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
309
310
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than_or_equal_to=>20})
310
311
  ])
311
312
  end
312
-
313
+
313
314
  it "should allow :input_html to override :max" do
314
315
  concat(semantic_form_for(@new_post) do |builder|
315
316
  builder.input(:title, :as => :range, :input_html => { :max => 102 })
316
317
  end)
317
- output_buffer.should have_tag('input[@max="102"]')
318
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
318
319
  end
319
-
320
+
320
321
  it "should allow options to override :max" do
321
322
  concat(semantic_form_for(@new_post) do |builder|
322
323
  builder.input(:title, :as => :range, :max => 102)
323
324
  end)
324
- output_buffer.should have_tag('input[@max="102"]')
325
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
325
326
  end
326
-
327
+
327
328
  it "should allow :input_html to override :max with :in" do
328
329
  concat(semantic_form_for(@new_post) do |builder|
329
330
  builder.input(:title, :as => :range, :input_html => { :in => 1..102 })
330
331
  end)
331
- output_buffer.should have_tag('input[@max="102"]')
332
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
332
333
  end
333
-
334
+
334
335
  it "should allow options to override :max with :in" do
335
336
  concat(semantic_form_for(@new_post) do |builder|
336
337
  builder.input(:title, :as => :range, :in => 1..102)
337
338
  end)
338
- output_buffer.should have_tag('input[@max="102"]')
339
+ expect(output_buffer.to_str).to have_tag('input[@max="102"]')
339
340
  end
340
341
 
341
342
  [:integer, :decimal, :float].each do |column_type|
342
343
  describe "and the column is a #{column_type}" do
343
344
  before do
344
- @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
345
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(double('column', :type => column_type))
345
346
  end
346
347
 
347
348
  it "should add a max attribute to the input equal to the validation" do
348
349
  concat(semantic_form_for(@new_post) do |builder|
349
350
  builder.input(:title, :as => :range)
350
351
  end)
351
- output_buffer.should have_tag('input[@max="20"]')
352
+ expect(output_buffer.to_str).to have_tag('input[@max="20"]')
352
353
  end
353
354
  end
354
355
  end
355
356
 
356
357
  describe "and there is no column" do
357
358
  before do
358
- @new_post.stub(:column_for_attribute).with(:title).and_return(nil)
359
+ allow(@new_post).to receive(:column_for_attribute).with(:title).and_return(nil)
359
360
  end
360
-
361
+
361
362
  it "should add a max attribute to the input equal to the validation" do
362
363
  concat(semantic_form_for(@new_post) do |builder|
363
364
  builder.input(:title, :as => :range)
364
365
  end)
365
- output_buffer.should have_tag('input[@max="20"]')
366
+ expect(output_buffer.to_str).to have_tag('input[@max="20"]')
366
367
  end
367
368
  end
368
369
  end
369
-
370
+
370
371
  describe "when validations do not require a maximum value" do
371
-
372
+
372
373
  it "should default to 1" do
373
374
  concat(semantic_form_for(@new_post) do |builder|
374
375
  builder.input(:title, :as => :range)
375
376
  end)
376
- output_buffer.should have_tag('input[@max="100"]')
377
+ expect(output_buffer.to_str).to have_tag('input[@max="100"]')
377
378
  end
378
-
379
+
379
380
  end
380
-
381
+
381
382
  describe "when validations require conflicting minimum values (:greater_than, :greater_than_or_equal_to)" do
382
383
  before do
383
- @new_post.class.stub(:validators_on).with(:title).and_return([
384
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
384
385
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :greater_than => 20, :greater_than_or_equal_to=>2})
385
386
  ])
386
387
  end
387
-
388
+
388
389
  it "should add a max attribute to the input equal to the :greater_than_or_equal_to validation" do
389
390
  concat(semantic_form_for(@new_post) do |builder|
390
391
  builder.input(:title, :as => :range)
391
392
  end)
392
- output_buffer.should have_tag('input[@min="2"]')
393
+ expect(output_buffer.to_str).to have_tag('input[@min="2"]')
393
394
  end
394
395
  end
395
-
396
+
396
397
  describe "when validations require conflicting maximum values (:less_than, :less_than_or_equal_to)" do
397
398
  before do
398
- @new_post.class.stub(:validators_on).with(:title).and_return([
399
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
399
400
  active_model_numericality_validator([:title], {:only_integer=>false, :allow_nil=>false, :less_than => 20, :less_than_or_equal_to=>2})
400
401
  ])
401
402
  end
402
-
403
+
403
404
  it "should add a max attribute to the input equal to the :greater_than_or_equal_to validation" do
404
405
  concat(semantic_form_for(@new_post) do |builder|
405
406
  builder.input(:title, :as => :range)
406
407
  end)
407
- output_buffer.should have_tag('input[@max="2"]')
408
+ expect(output_buffer.to_str).to have_tag('input[@max="2"]')
408
409
  end
409
410
  end
410
-
411
+
411
412
  describe "when validations require only an integer (:only_integer)" do
412
-
413
+
413
414
  before do
414
- @new_post.class.stub(:validators_on).with(:title).and_return([
415
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
415
416
  active_model_numericality_validator([:title], {:allow_nil=>false, :only_integer=>true})
416
417
  ])
417
418
  end
418
-
419
+
419
420
  it "should add a step=1 attribute to the input to signify that only whole numbers are allowed" do
420
421
  concat(semantic_form_for(@new_post) do |builder|
421
422
  builder.input(:title, :as => :range)
422
423
  end)
423
- output_buffer.should have_tag('input[@step="1"]')
424
+ expect(output_buffer.to_str).to have_tag('input[@step="1"]')
424
425
  end
425
-
426
+
426
427
  it "should let input_html override :step" do
427
428
  concat(semantic_form_for(@new_post) do |builder|
428
429
  builder.input(:title, :as => :range, :input_html => { :step => 3 })
429
430
  end)
430
- output_buffer.should have_tag('input[@step="3"]')
431
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
431
432
  end
432
-
433
+
433
434
  it "should let options override :step" do
434
435
  concat(semantic_form_for(@new_post) do |builder|
435
436
  builder.input(:title, :as => :range, :step => 3)
436
437
  end)
437
- output_buffer.should have_tag('input[@step="3"]')
438
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
438
439
  end
439
-
440
+
440
441
  end
441
-
442
+
442
443
  describe "when validations require a :step (non standard)" do
443
-
444
+
444
445
  before do
445
- @new_post.class.stub(:validators_on).with(:title).and_return([
446
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
446
447
  active_model_numericality_validator([:title], {:allow_nil=>false, :only_integer=>true, :step=>2})
447
448
  ])
448
449
  end
449
-
450
+
450
451
  it "should add a step=1 attribute to the input to signify that only whole numbers are allowed" do
451
452
  concat(semantic_form_for(@new_post) do |builder|
452
453
  builder.input(:title, :as => :range)
453
454
  end)
454
- output_buffer.should have_tag('input[@step="2"]')
455
+ expect(output_buffer.to_str).to have_tag('input[@step="2"]')
455
456
  end
456
-
457
+
457
458
  it "should let input_html override :step" do
458
459
  concat(semantic_form_for(@new_post) do |builder|
459
460
  builder.input(:title, :as => :range, :input_html => { :step => 3 })
460
461
  end)
461
- output_buffer.should have_tag('input[@step="3"]')
462
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
462
463
  end
463
-
464
+
464
465
  it "should let options override :step" do
465
466
  concat(semantic_form_for(@new_post) do |builder|
466
467
  builder.input(:title, :as => :range, :step => 3)
467
468
  end)
468
- output_buffer.should have_tag('input[@step="3"]')
469
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
469
470
  end
470
-
471
+
471
472
  end
472
-
473
+
473
474
  describe "when validations do not specify :step (non standard) or :only_integer" do
474
-
475
+
475
476
  before do
476
- @new_post.class.stub(:validators_on).with(:title).and_return([
477
+ allow(@new_post.class).to receive(:validators_on).with(:title).and_return([
477
478
  active_model_numericality_validator([:title], {:allow_nil=>false})
478
479
  ])
479
480
  end
480
-
481
+
481
482
  it "should default step to 1" do
482
483
  concat(semantic_form_for(@new_post) do |builder|
483
484
  builder.input(:title, :as => :range)
484
485
  end)
485
- output_buffer.should have_tag('input[@step="1"]')
486
+ expect(output_buffer.to_str).to have_tag('input[@step="1"]')
486
487
  end
487
-
488
+
488
489
  it "should let input_html set :step" do
489
490
  concat(semantic_form_for(@new_post) do |builder|
490
491
  builder.input(:title, :as => :range, :input_html => { :step => 3 })
491
492
  end)
492
- output_buffer.should have_tag('input[@step="3"]')
493
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
493
494
  end
494
-
495
+
495
496
  it "should let options set :step" do
496
497
  concat(semantic_form_for(@new_post) do |builder|
497
498
  builder.input(:title, :as => :range, :step => 3)
498
499
  end)
499
- output_buffer.should have_tag('input[@step="3"]')
500
+ expect(output_buffer.to_str).to have_tag('input[@step="3"]')
500
501
  end
501
-
502
+
502
503
  end
503
-
504
+
504
505
  end
505
506
 
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe 'readonly option' do
6
+
7
+ include FormtasticSpecHelper
8
+
9
+ before do
10
+ @output_buffer = ActionView::OutputBuffer.new ''
11
+ mock_everything
12
+ end
13
+
14
+ describe "placeholder text" do
15
+ [:email, :number, :password, :phone, :search, :string, :url, :text, :date_picker, :time_picker, :datetime_picker].each do |type|
16
+
17
+ describe "for #{type} inputs" do
18
+
19
+ describe "when readonly is found in input_html" do
20
+ it "sets readonly attribute" do
21
+ concat(semantic_form_for(@new_post) do |builder|
22
+ concat(builder.input(:title, :as => type, input_html: {readonly: true}))
23
+ end)
24
+ expect(output_buffer.to_str).to have_tag((type == :text ? 'textarea' : 'input') + '[@readonly]')
25
+ end
26
+ end
27
+
28
+ describe "when readonly not found in input_html" do
29
+ describe "when column is not readonly attribute" do
30
+ it "doesn't set readonly attribute" do
31
+ concat(semantic_form_for(@new_post) do |builder|
32
+ concat(builder.input(:title, :as => type))
33
+ end)
34
+ expect(output_buffer.to_str).not_to have_tag((type == :text ? 'textarea' : 'input') + '[@readonly]')
35
+ end
36
+ end
37
+ describe "when column is readonly attribute" do
38
+ it "sets readonly attribute" do
39
+ input_class = "Formtastic::Inputs::#{type.to_s.camelize}Input".constantize
40
+ expect_any_instance_of(input_class).to receive(:readonly_attribute?).at_least(1).and_return(true)
41
+ concat(semantic_form_for(@new_post) do |builder|
42
+ concat(builder.input(:title, :as => type))
43
+ end)
44
+ expect(output_buffer.to_str).to have_tag((type == :text ? 'textarea' : 'input') + '[@readonly]')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end