formtastic 2.1.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +1 -0
- data/.github/workflows/test.yml +61 -0
- data/.gitignore +4 -2
- data/CHANGELOG.md +52 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +105 -0
- data/MIT-LICENSE +1 -1
- data/{README.textile → README.md} +204 -219
- data/RELEASE_PROCESS +3 -1
- data/Rakefile +27 -29
- data/app/assets/stylesheets/formtastic.css +3 -2
- data/bin/appraisal +8 -0
- data/formtastic.gemspec +11 -14
- data/gemfiles/rails_5.2/Gemfile +5 -0
- data/gemfiles/rails_6.0/Gemfile +5 -0
- data/gemfiles/rails_6.1/Gemfile +5 -0
- data/gemfiles/rails_edge/Gemfile +13 -0
- data/lib/formtastic/action_class_finder.rb +18 -0
- data/lib/formtastic/actions/button_action.rb +55 -60
- data/lib/formtastic/actions/input_action.rb +59 -57
- data/lib/formtastic/actions/link_action.rb +68 -67
- data/lib/formtastic/actions.rb +6 -3
- data/lib/formtastic/deprecation.rb +5 -0
- data/lib/formtastic/engine.rb +3 -1
- data/lib/formtastic/form_builder.rb +35 -16
- data/lib/formtastic/helpers/action_helper.rb +34 -28
- data/lib/formtastic/helpers/enum.rb +13 -0
- data/lib/formtastic/helpers/errors_helper.rb +2 -2
- data/lib/formtastic/helpers/fieldset_wrapper.rb +16 -12
- data/lib/formtastic/helpers/form_helper.rb +19 -16
- data/lib/formtastic/helpers/input_helper.rb +69 -97
- data/lib/formtastic/helpers/inputs_helper.rb +35 -25
- data/lib/formtastic/helpers/reflection.rb +4 -4
- data/lib/formtastic/helpers.rb +1 -2
- data/lib/formtastic/html_attributes.rb +12 -1
- data/lib/formtastic/i18n.rb +1 -1
- data/lib/formtastic/input_class_finder.rb +18 -0
- data/lib/formtastic/inputs/base/choices.rb +2 -2
- data/lib/formtastic/inputs/base/collections.rb +46 -14
- data/lib/formtastic/inputs/base/database.rb +7 -2
- data/lib/formtastic/inputs/base/datetime_pickerish.rb +85 -0
- data/lib/formtastic/inputs/base/errors.rb +7 -7
- data/lib/formtastic/inputs/base/hints.rb +2 -2
- data/lib/formtastic/inputs/base/html.rb +10 -9
- data/lib/formtastic/inputs/base/labelling.rb +5 -8
- data/lib/formtastic/inputs/base/naming.rb +4 -4
- data/lib/formtastic/inputs/base/numeric.rb +1 -1
- data/lib/formtastic/inputs/base/options.rb +3 -4
- data/lib/formtastic/inputs/base/stringish.rb +10 -2
- data/lib/formtastic/inputs/base/timeish.rb +34 -22
- data/lib/formtastic/inputs/base/validations.rb +41 -13
- data/lib/formtastic/inputs/base/wrapping.rb +29 -26
- data/lib/formtastic/inputs/base.rb +22 -15
- data/lib/formtastic/inputs/boolean_input.rb +26 -12
- data/lib/formtastic/inputs/check_boxes_input.rb +39 -31
- data/lib/formtastic/inputs/color_input.rb +41 -0
- data/lib/formtastic/inputs/country_input.rb +24 -5
- data/lib/formtastic/inputs/datalist_input.rb +41 -0
- data/lib/formtastic/inputs/date_picker_input.rb +93 -0
- data/lib/formtastic/inputs/{date_input.rb → date_select_input.rb} +1 -1
- data/lib/formtastic/inputs/datetime_picker_input.rb +103 -0
- data/lib/formtastic/inputs/{datetime_input.rb → datetime_select_input.rb} +1 -1
- data/lib/formtastic/inputs/file_input.rb +2 -2
- data/lib/formtastic/inputs/hidden_input.rb +2 -6
- data/lib/formtastic/inputs/radio_input.rb +28 -22
- data/lib/formtastic/inputs/select_input.rb +36 -39
- data/lib/formtastic/inputs/time_picker_input.rb +99 -0
- data/lib/formtastic/inputs/{time_input.rb → time_select_input.rb} +6 -2
- data/lib/formtastic/inputs/time_zone_input.rb +16 -6
- data/lib/formtastic/inputs.rb +32 -21
- data/lib/formtastic/localized_string.rb +1 -1
- data/lib/formtastic/localizer.rb +24 -24
- data/lib/formtastic/namespaced_class_finder.rb +99 -0
- data/lib/formtastic/version.rb +1 -1
- data/lib/formtastic.rb +20 -10
- data/lib/generators/formtastic/form/form_generator.rb +10 -4
- data/lib/generators/formtastic/input/input_generator.rb +46 -0
- data/lib/generators/formtastic/install/install_generator.rb +5 -19
- data/lib/generators/templates/_form.html.slim +2 -2
- data/lib/generators/templates/formtastic.rb +46 -25
- data/lib/generators/templates/input.rb +19 -0
- data/sample/basic_inputs.html +23 -3
- data/script/integration-template.rb +74 -0
- data/script/integration.sh +19 -0
- data/spec/action_class_finder_spec.rb +12 -0
- data/spec/actions/button_action_spec.rb +8 -8
- data/spec/actions/generic_action_spec.rb +92 -56
- data/spec/actions/input_action_spec.rb +7 -7
- data/spec/actions/link_action_spec.rb +10 -10
- data/spec/builder/custom_builder_spec.rb +36 -20
- data/spec/builder/error_proc_spec.rb +4 -4
- data/spec/builder/semantic_fields_for_spec.rb +28 -29
- data/spec/fast_spec_helper.rb +12 -0
- data/spec/generators/formtastic/form/form_generator_spec.rb +45 -32
- data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
- data/spec/generators/formtastic/install/install_generator_spec.rb +9 -9
- data/spec/helpers/action_helper_spec.rb +75 -103
- data/spec/helpers/actions_helper_spec.rb +17 -17
- data/spec/helpers/form_helper_spec.rb +84 -33
- data/spec/helpers/input_helper_spec.rb +333 -285
- data/spec/helpers/inputs_helper_spec.rb +167 -121
- data/spec/helpers/reflection_helper_spec.rb +3 -3
- data/spec/helpers/semantic_errors_helper_spec.rb +23 -23
- data/spec/i18n_spec.rb +26 -26
- data/spec/input_class_finder_spec.rb +10 -0
- data/spec/inputs/base/collections_spec.rb +76 -0
- data/spec/inputs/base/validations_spec.rb +480 -0
- data/spec/inputs/boolean_input_spec.rb +100 -65
- data/spec/inputs/check_boxes_input_spec.rb +200 -101
- data/spec/inputs/color_input_spec.rb +85 -0
- data/spec/inputs/country_input_spec.rb +20 -20
- data/spec/inputs/custom_input_spec.rb +3 -4
- data/spec/inputs/datalist_input_spec.rb +61 -0
- data/spec/inputs/date_picker_input_spec.rb +449 -0
- data/spec/inputs/date_select_input_spec.rb +249 -0
- data/spec/inputs/datetime_picker_input_spec.rb +490 -0
- data/spec/inputs/datetime_select_input_spec.rb +209 -0
- data/spec/inputs/email_input_spec.rb +5 -5
- data/spec/inputs/file_input_spec.rb +6 -6
- data/spec/inputs/hidden_input_spec.rb +22 -35
- data/spec/inputs/include_blank_spec.rb +11 -11
- data/spec/inputs/label_spec.rb +62 -25
- data/spec/inputs/number_input_spec.rb +112 -112
- data/spec/inputs/password_input_spec.rb +5 -5
- data/spec/inputs/phone_input_spec.rb +5 -5
- data/spec/inputs/placeholder_spec.rb +6 -6
- data/spec/inputs/radio_input_spec.rb +99 -55
- data/spec/inputs/range_input_spec.rb +66 -66
- data/spec/inputs/readonly_spec.rb +50 -0
- data/spec/inputs/search_input_spec.rb +5 -5
- data/spec/inputs/select_input_spec.rb +170 -170
- data/spec/inputs/string_input_spec.rb +68 -16
- data/spec/inputs/text_input_spec.rb +16 -16
- data/spec/inputs/time_picker_input_spec.rb +455 -0
- data/spec/inputs/time_select_input_spec.rb +261 -0
- data/spec/inputs/time_zone_input_spec.rb +54 -28
- data/spec/inputs/url_input_spec.rb +5 -5
- data/spec/inputs/with_options_spec.rb +7 -7
- data/spec/localizer_spec.rb +39 -17
- data/spec/namespaced_class_finder_spec.rb +79 -0
- data/spec/schema.rb +21 -0
- data/spec/spec_helper.rb +254 -221
- data/spec/support/custom_macros.rb +128 -95
- data/spec/support/shared_examples.rb +12 -0
- data/spec/support/specialized_class_finder_shared_example.rb +27 -0
- data/spec/support/test_environment.rb +26 -10
- metadata +177 -238
- data/.travis.yml +0 -8
- data/Appraisals +0 -11
- data/CHANGELOG +0 -371
- data/gemfiles/rails-3.0.gemfile +0 -7
- data/gemfiles/rails-3.1.gemfile +0 -7
- data/gemfiles/rails-3.2.gemfile +0 -7
- data/lib/formtastic/helpers/buttons_helper.rb +0 -310
- data/lib/formtastic/inputs/base/grouped_collections.rb +0 -77
- data/lib/formtastic/util.rb +0 -25
- data/lib/tasks/verify_rcov.rb +0 -44
- data/spec/helpers/buttons_helper_spec.rb +0 -166
- data/spec/helpers/commit_button_helper_spec.rb +0 -530
- data/spec/inputs/date_input_spec.rb +0 -227
- data/spec/inputs/datetime_input_spec.rb +0 -185
- data/spec/inputs/time_input_spec.rb +0 -267
- data/spec/support/deferred_garbage_collection.rb +0 -21
@@ -0,0 +1,480 @@
|
|
1
|
+
require 'fast_spec_helper'
|
2
|
+
require 'active_model'
|
3
|
+
require 'inputs/base/validations'
|
4
|
+
|
5
|
+
class MyInput
|
6
|
+
attr_accessor :validations
|
7
|
+
include Formtastic::Inputs::Base::Validations
|
8
|
+
|
9
|
+
def validations?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec.describe MyInput do
|
15
|
+
let(:builder) { double }
|
16
|
+
let(:template) { double }
|
17
|
+
let(:model_class) { double }
|
18
|
+
let(:model) { double(:class => model_class) }
|
19
|
+
let(:model_name) { "post" }
|
20
|
+
let(:method) { double }
|
21
|
+
let(:options) { Hash.new }
|
22
|
+
let(:validator) { double }
|
23
|
+
let(:instance) do
|
24
|
+
MyInput.new(builder, template, model, model_name, method, options).tap do |my_input|
|
25
|
+
my_input.validations = validations
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#required?' do
|
30
|
+
context 'with a single validator' do
|
31
|
+
let(:validations) { [validator] }
|
32
|
+
|
33
|
+
context 'with options[:required] being true' do
|
34
|
+
let(:options) { {required: true} }
|
35
|
+
|
36
|
+
it 'is required' do
|
37
|
+
expect(instance.required?).to be_truthy
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with options[:required] being false' do
|
42
|
+
let(:options) { {required: false} }
|
43
|
+
|
44
|
+
it 'is not required' do
|
45
|
+
expect(instance.required?).to be_falsey
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with negated validation' do
|
50
|
+
it 'is not required' do
|
51
|
+
instance.not_required_through_negated_validation!
|
52
|
+
expect(instance.required?).to be_falsey
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with presence validator' do
|
57
|
+
let (:validator) { double(options: {}, kind: :presence) }
|
58
|
+
|
59
|
+
it 'is required' do
|
60
|
+
expect(instance.required?).to be_truthy
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with options[:on] as symbol' do
|
64
|
+
context 'with save context' do
|
65
|
+
let (:validator) { double(options: {on: :save}, kind: :presence) }
|
66
|
+
|
67
|
+
it 'is required' do
|
68
|
+
expect(instance.required?).to be_truthy
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with create context' do
|
73
|
+
let (:validator) { double(options: {on: :create}, kind: :presence) }
|
74
|
+
|
75
|
+
it 'is required for new records' do
|
76
|
+
allow(model).to receive(:new_record?).and_return(true)
|
77
|
+
expect(instance.required?).to be_truthy
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'is not required for existing records' do
|
81
|
+
allow(model).to receive(:new_record?).and_return(false)
|
82
|
+
expect(instance.required?).to be_falsey
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with update context' do
|
87
|
+
let (:validator) { double(options: {on: :update}, kind: :presence) }
|
88
|
+
|
89
|
+
it 'is not required for new records' do
|
90
|
+
allow(model).to receive(:new_record?).and_return(true)
|
91
|
+
expect(instance.required?).to be_falsey
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'is required for existing records' do
|
95
|
+
allow(model).to receive(:new_record?).and_return(false)
|
96
|
+
expect(instance.required?).to be_truthy
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'with options[:on] as array' do
|
102
|
+
context 'with save context' do
|
103
|
+
let (:validator) { double(options: {on: [:save]}, kind: :presence) }
|
104
|
+
|
105
|
+
it 'is required' do
|
106
|
+
expect(instance.required?).to be_truthy
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'with create context' do
|
111
|
+
let (:validator) { double(options: {on: [:create]}, kind: :presence) }
|
112
|
+
|
113
|
+
it 'is required for new records' do
|
114
|
+
allow(model).to receive(:new_record?).and_return(true)
|
115
|
+
expect(instance.required?).to be_truthy
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'is not required for existing records' do
|
119
|
+
allow(model).to receive(:new_record?).and_return(false)
|
120
|
+
expect(instance.required?).to be_falsey
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'with update context' do
|
125
|
+
let (:validator) { double(options: {on: [:update]}, kind: :presence) }
|
126
|
+
|
127
|
+
it 'is not required for new records' do
|
128
|
+
allow(model).to receive(:new_record?).and_return(true)
|
129
|
+
expect(instance.required?).to be_falsey
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'is required for existing records' do
|
133
|
+
allow(model).to receive(:new_record?).and_return(false)
|
134
|
+
expect(instance.required?).to be_truthy
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'with save and create context' do
|
139
|
+
let (:validator) { double(options: {on: [:save, :create]}, kind: :presence) }
|
140
|
+
|
141
|
+
it 'is required for new records' do
|
142
|
+
allow(model).to receive(:new_record?).and_return(true)
|
143
|
+
expect(instance.required?).to be_truthy
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'is required for existing records' do
|
147
|
+
allow(model).to receive(:new_record?).and_return(false)
|
148
|
+
expect(instance.required?).to be_truthy
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'with save and update context' do
|
153
|
+
let (:validator) { double(options: {on: [:save, :create]}, kind: :presence) }
|
154
|
+
|
155
|
+
it 'is required for new records' do
|
156
|
+
allow(model).to receive(:new_record?).and_return(true)
|
157
|
+
expect(instance.required?).to be_truthy
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'is required for existing records' do
|
161
|
+
allow(model).to receive(:new_record?).and_return(false)
|
162
|
+
expect(instance.required?).to be_truthy
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context 'with create and update context' do
|
167
|
+
let (:validator) { double(options: {on: [:create, :update]}, kind: :presence) }
|
168
|
+
|
169
|
+
it 'is required for new records' do
|
170
|
+
allow(model).to receive(:new_record?).and_return(true)
|
171
|
+
expect(instance.required?).to be_truthy
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'is required for existing records' do
|
175
|
+
allow(model).to receive(:new_record?).and_return(false)
|
176
|
+
expect(instance.required?).to be_truthy
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'with save and other context' do
|
181
|
+
let (:validator) { double(options: {on: [:save, :foo]}, kind: :presence) }
|
182
|
+
|
183
|
+
it 'is required for new records' do
|
184
|
+
allow(model).to receive(:new_record?).and_return(true)
|
185
|
+
expect(instance.required?).to be_truthy
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'is required for existing records' do
|
189
|
+
allow(model).to receive(:new_record?).and_return(false)
|
190
|
+
expect(instance.required?).to be_truthy
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'with create and other context' do
|
195
|
+
let (:validator) { double(options: {on: [:create, :foo]}, kind: :presence) }
|
196
|
+
|
197
|
+
it 'is required for new records' do
|
198
|
+
allow(model).to receive(:new_record?).and_return(true)
|
199
|
+
expect(instance.required?).to be_truthy
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'is not required for existing records' do
|
203
|
+
allow(model).to receive(:new_record?).and_return(false)
|
204
|
+
expect(instance.required?).to be_falsey
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
context 'with update and other context' do
|
209
|
+
let (:validator) { double(options: {on: [:update, :foo]}, kind: :presence) }
|
210
|
+
|
211
|
+
it 'is not required for new records' do
|
212
|
+
allow(model).to receive(:new_record?).and_return(true)
|
213
|
+
expect(instance.required?).to be_falsey
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'is required for existing records' do
|
217
|
+
allow(model).to receive(:new_record?).and_return(false)
|
218
|
+
expect(instance.required?).to be_truthy
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
context 'with inclusion validator' do
|
225
|
+
context 'with allow blank' do
|
226
|
+
let (:validator) { double(options: {allow_blank: true}, kind: :inclusion) }
|
227
|
+
|
228
|
+
it 'is not required' do
|
229
|
+
expect(instance.required?).to be_falsey
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context 'without allow blank' do
|
234
|
+
let (:validator) { double(options: {allow_blank: false}, kind: :inclusion) }
|
235
|
+
|
236
|
+
it 'is required' do
|
237
|
+
expect(instance.required?).to be_truthy
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context 'with a length validator' do
|
243
|
+
context 'with allow blank' do
|
244
|
+
let (:validator) { double(options: {allow_blank: true}, kind: :length) }
|
245
|
+
|
246
|
+
it 'is not required' do
|
247
|
+
expect(instance.required?).to be_falsey
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
context 'without allow blank' do
|
252
|
+
let (:validator) { double(options: {allow_blank: false}, kind: :length) }
|
253
|
+
|
254
|
+
it 'is not required' do
|
255
|
+
expect(instance.required?).to be_falsey
|
256
|
+
end
|
257
|
+
|
258
|
+
context 'with a minimum > 0' do
|
259
|
+
let (:validator) { double(options: {allow_blank: false, minimum: 1}, kind: :length) }
|
260
|
+
|
261
|
+
it 'is required' do
|
262
|
+
expect(instance.required?).to be_truthy
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'with a minimum <= 0' do
|
267
|
+
let (:validator) { double(options: {allow_blank: false, minimum: 0}, kind: :length) }
|
268
|
+
|
269
|
+
it 'is not required' do
|
270
|
+
expect(instance.required?).to be_falsey
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context 'with a defined range starting with > 0' do
|
275
|
+
let (:validator) { double(options: {allow_blank: false, within: 1..5}, kind: :length) }
|
276
|
+
|
277
|
+
it 'is required' do
|
278
|
+
expect(instance.required?).to be_truthy
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
context 'with a defined range starting with <= 0' do
|
283
|
+
let (:validator) { double(options: {allow_blank: false, within: 0..5}, kind: :length) }
|
284
|
+
|
285
|
+
it 'is not required' do
|
286
|
+
expect(instance.required?).to be_falsey
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
context 'with another validator' do
|
293
|
+
let (:validator) { double(options: {allow_blank: true}, kind: :foo) }
|
294
|
+
|
295
|
+
it 'is not required' do
|
296
|
+
expect(instance.required?).to be_falsey
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
context 'with multiple validators' do
|
302
|
+
let(:validations) { [validator1, validator2] }
|
303
|
+
|
304
|
+
context 'with a on create presence validator and a on update presence validator' do
|
305
|
+
let(:validator1) { double(options: {on: :create}, kind: :presence) }
|
306
|
+
let(:validator2) { double(options: {}, kind: :presence) }
|
307
|
+
|
308
|
+
before :example do
|
309
|
+
allow(model).to receive(:new_record?).and_return(false)
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'is required' do
|
313
|
+
expect(instance.required?).to be_truthy
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
context 'with a on create presence validator and a presence validator' do
|
318
|
+
let (:validator1) { double(options: {on: :create}, kind: :presence) }
|
319
|
+
let (:validator2) { double(options: {}, kind: :presence) }
|
320
|
+
|
321
|
+
before :example do
|
322
|
+
allow(model).to receive(:new_record?).and_return(false)
|
323
|
+
end
|
324
|
+
|
325
|
+
it 'is required' do
|
326
|
+
expect(instance.required?).to be_truthy
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
context 'with a on create presence validator and a allow blank inclusion validator' do
|
331
|
+
let(:validator1) { double(options: {on: :create}, kind: :presence) }
|
332
|
+
let(:validator2) { double(options: {allow_blank: true}, kind: :inclusion) }
|
333
|
+
|
334
|
+
before :example do
|
335
|
+
allow(model).to receive(:new_record?).and_return(false)
|
336
|
+
end
|
337
|
+
|
338
|
+
it 'is required' do
|
339
|
+
expect(instance.required?).to be_falsey
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
describe '#validation_min' do
|
346
|
+
let(:validations) { [validator] }
|
347
|
+
|
348
|
+
context 'with a greater_than numericality validator' do
|
349
|
+
let(:validator) { double(options: { greater_than: option_value }, kind: :numericality) }
|
350
|
+
|
351
|
+
context 'with a symbol' do
|
352
|
+
let(:option_value) { :a_symbol }
|
353
|
+
|
354
|
+
it 'returns one greater' do
|
355
|
+
allow(model).to receive(:send).with(option_value).and_return(14)
|
356
|
+
expect(instance.validation_min).to eq 15
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
context 'with a proc' do
|
361
|
+
let(:option_value) { Proc.new { 10 } }
|
362
|
+
|
363
|
+
it 'returns one greater' do
|
364
|
+
expect(instance.validation_min).to eq 11
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
context 'with a number' do
|
369
|
+
let(:option_value) { 8 }
|
370
|
+
|
371
|
+
it 'returns one greater' do
|
372
|
+
expect(instance.validation_min).to eq 9
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
context 'with a greater_than_or_equal_to numericality validator' do
|
378
|
+
let(:validator) do
|
379
|
+
double(
|
380
|
+
options: { greater_than_or_equal_to: option_value },
|
381
|
+
kind: :numericality
|
382
|
+
)
|
383
|
+
end
|
384
|
+
|
385
|
+
context 'with a symbol' do
|
386
|
+
let(:option_value) { :a_symbol }
|
387
|
+
|
388
|
+
it 'returns the instance method amount' do
|
389
|
+
allow(model).to receive(:send).with(option_value).and_return(14)
|
390
|
+
expect(instance.validation_min).to eq 14
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
context 'with a proc' do
|
395
|
+
let(:option_value) { Proc.new { 10 } }
|
396
|
+
|
397
|
+
it 'returns the proc amount' do
|
398
|
+
expect(instance.validation_min).to eq 10
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
context 'with a number' do
|
403
|
+
let(:option_value) { 8 }
|
404
|
+
|
405
|
+
it 'returns the number' do
|
406
|
+
expect(instance.validation_min).to eq 8
|
407
|
+
end
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
describe '#validation_max' do
|
413
|
+
let(:validations) do
|
414
|
+
[
|
415
|
+
ActiveModel::Validations::NumericalityValidator.new(
|
416
|
+
validator_options.merge(attributes: :an_attribute)
|
417
|
+
)
|
418
|
+
]
|
419
|
+
end
|
420
|
+
|
421
|
+
context 'with a less_than numericality validator' do
|
422
|
+
let(:validator_options) { { less_than: option_value } }
|
423
|
+
|
424
|
+
context 'with a symbol' do
|
425
|
+
let(:option_value) { :a_symbol }
|
426
|
+
|
427
|
+
it 'returns one less' do
|
428
|
+
allow(model).to receive(:send).with(option_value).and_return(14)
|
429
|
+
expect(instance.validation_max).to eq 13
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
context 'with a proc' do
|
434
|
+
let(:option_value) { proc { 10 } }
|
435
|
+
|
436
|
+
it 'returns one less' do
|
437
|
+
expect(instance.validation_max).to eq 9
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
context 'with a number' do
|
442
|
+
let(:option_value) { 8 }
|
443
|
+
|
444
|
+
it 'returns one less' do
|
445
|
+
expect(instance.validation_max).to eq 7
|
446
|
+
end
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
context 'with a less_than_or_equal_to numericality validator' do
|
451
|
+
let(:validator_options) { { less_than_or_equal_to: option_value } }
|
452
|
+
|
453
|
+
context 'with a symbol' do
|
454
|
+
let(:option_value) { :a_symbol }
|
455
|
+
|
456
|
+
it 'returns the instance method amount' do
|
457
|
+
allow(model).to receive(:send).with(option_value).and_return(14)
|
458
|
+
expect(instance.validation_max).to eq 14
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
context 'with a proc' do
|
463
|
+
let(:option_value) { proc { 10 } }
|
464
|
+
|
465
|
+
it 'returns the proc amount' do
|
466
|
+
expect(instance.validation_max).to eq 10
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
context 'with a number' do
|
471
|
+
let(:option_value) { 8 }
|
472
|
+
|
473
|
+
it 'returns the number' do
|
474
|
+
expect(instance.validation_max).to eq 8
|
475
|
+
end
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|