formeze 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -4
- data/formeze.gemspec +3 -1
- data/lib/formeze.rb +13 -6
- data/spec/formeze_spec.rb +43 -21
- metadata +18 -2
data/README.md
CHANGED
@@ -63,9 +63,9 @@ your own initialization logic).
|
|
63
63
|
Detecting errors
|
64
64
|
----------------
|
65
65
|
|
66
|
-
Formeze distinguishes between
|
67
|
-
running of your application), and key/value errors (which most likely
|
68
|
-
either developer error, or form tampering).
|
66
|
+
Formeze distinguishes between validation errors (which are expected in the
|
67
|
+
normal running of your application), and key/value errors (which most likely
|
68
|
+
indicate either developer error, or form tampering).
|
69
69
|
|
70
70
|
For the latter case, the `parse` method that formeze provides will raise a
|
71
71
|
Formeze::KeyError or a Formeze::ValueError exception if the structure of the
|
@@ -73,7 +73,10 @@ form data does not match the field definitions.
|
|
73
73
|
|
74
74
|
After calling `parse` you can check that the form is valid by calling the
|
75
75
|
`#valid?` method. If it isn't you can call the `errors` method which will
|
76
|
-
return an array of error messages to display to the user.
|
76
|
+
return an array of error messages to display to the end user.
|
77
|
+
|
78
|
+
You can also use `errors_on?` and `errors_on` to check for and select error
|
79
|
+
messages specific to a single field.
|
77
80
|
|
78
81
|
|
79
82
|
Field options
|
data/formeze.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'formeze'
|
3
|
-
s.version = '1.
|
3
|
+
s.version = '1.7.0'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.authors = ['Tim Craft']
|
6
6
|
s.email = ['mail@timcraft.com']
|
@@ -8,6 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.description = 'A little library for handling form data/input'
|
9
9
|
s.summary = 'See description'
|
10
10
|
s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md Rakefile.rb formeze.gemspec)
|
11
|
+
s.add_development_dependency('rake', ['>= 0.9.3'])
|
11
12
|
s.add_development_dependency('i18n', ['~> 0.6.0'])
|
13
|
+
s.add_development_dependency('minitest', ['>= 4.2.0']) if RUBY_VERSION == '1.8.7'
|
12
14
|
s.require_path = 'lib'
|
13
15
|
end
|
data/lib/formeze.rb
CHANGED
@@ -37,7 +37,7 @@ module Formeze
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def error(i18n_key, default)
|
40
|
-
translate(i18n_key, scope
|
40
|
+
translate(i18n_key, :scope => [:formeze, :errors], :default => default)
|
41
41
|
end
|
42
42
|
|
43
43
|
def key
|
@@ -49,7 +49,7 @@ module Formeze
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def label
|
52
|
-
@options.fetch(:label) { translate(name, scope
|
52
|
+
@options.fetch(:label) { translate(name, :scope => [:formeze, :labels], :default => Label.new(name)) }
|
53
53
|
end
|
54
54
|
|
55
55
|
def required?
|
@@ -182,7 +182,7 @@ module Formeze
|
|
182
182
|
|
183
183
|
class ValueError < StandardError; end
|
184
184
|
|
185
|
-
class
|
185
|
+
class ValidationError < StandardError; end
|
186
186
|
|
187
187
|
module InstanceMethods
|
188
188
|
def parse(encoded_form_data)
|
@@ -241,6 +241,13 @@ module Formeze
|
|
241
241
|
field_errors[field_name]
|
242
242
|
end
|
243
243
|
|
244
|
+
def to_hash
|
245
|
+
self.class.fields.inject({}) do |hash, field|
|
246
|
+
hash[field.name] = send(field.name)
|
247
|
+
hash
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
244
251
|
private
|
245
252
|
|
246
253
|
def field_defined?(field)
|
@@ -258,7 +265,7 @@ module Formeze
|
|
258
265
|
end
|
259
266
|
|
260
267
|
def error!(message, field_name = nil)
|
261
|
-
error =
|
268
|
+
error = ValidationError.new(message)
|
262
269
|
|
263
270
|
errors << error
|
264
271
|
|
@@ -288,9 +295,9 @@ module Formeze
|
|
288
295
|
form.extend ClassMethods
|
289
296
|
|
290
297
|
if on_rails?
|
291
|
-
form.field(:utf8, key_required
|
298
|
+
form.field(:utf8, :key_required => false)
|
292
299
|
|
293
|
-
form.field(:authenticity_token, key_required
|
300
|
+
form.field(:authenticity_token, :key_required => false)
|
294
301
|
end
|
295
302
|
end
|
296
303
|
|
data/spec/formeze_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
|
-
|
3
|
-
require_relative '../lib/formeze'
|
2
|
+
require 'formeze'
|
4
3
|
require 'i18n'
|
5
4
|
|
6
5
|
class FormWithField < Formeze::Form
|
@@ -83,6 +82,12 @@ describe 'FormWithField after parsing valid input' do
|
|
83
82
|
errors.must_be_empty
|
84
83
|
end
|
85
84
|
end
|
85
|
+
|
86
|
+
describe 'to_hash method' do
|
87
|
+
it 'should return a hash containing the field name and its value' do
|
88
|
+
@form.to_hash.must_equal({:title => 'Untitled'})
|
89
|
+
end
|
90
|
+
end
|
86
91
|
end
|
87
92
|
|
88
93
|
describe 'FormWithField after parsing blank input' do
|
@@ -150,7 +155,7 @@ describe 'FormWithField parse class method' do
|
|
150
155
|
end
|
151
156
|
|
152
157
|
class FormWithOptionalField < Formeze::Form
|
153
|
-
field :title, required
|
158
|
+
field :title, :required => false
|
154
159
|
end
|
155
160
|
|
156
161
|
describe 'FormWithOptionalField after parsing blank input' do
|
@@ -167,7 +172,7 @@ describe 'FormWithOptionalField after parsing blank input' do
|
|
167
172
|
end
|
168
173
|
|
169
174
|
class FormWithFieldThatCanHaveMultipleLines < Formeze::Form
|
170
|
-
field :description, multiline
|
175
|
+
field :description, :multiline => true
|
171
176
|
end
|
172
177
|
|
173
178
|
describe 'FormWithFieldThatCanHaveMultipleLines after parsing input containing newlines' do
|
@@ -184,7 +189,7 @@ describe 'FormWithFieldThatCanHaveMultipleLines after parsing input containing n
|
|
184
189
|
end
|
185
190
|
|
186
191
|
class FormWithCharacterLimitedField < Formeze::Form
|
187
|
-
field :title, char_limit
|
192
|
+
field :title, :char_limit => 16
|
188
193
|
end
|
189
194
|
|
190
195
|
describe 'FormWithCharacterLimitedField after parsing input with too many characters' do
|
@@ -201,7 +206,7 @@ describe 'FormWithCharacterLimitedField after parsing input with too many charac
|
|
201
206
|
end
|
202
207
|
|
203
208
|
class FormWithWordLimitedField < Formeze::Form
|
204
|
-
field :title, word_limit
|
209
|
+
field :title, :word_limit => 2
|
205
210
|
end
|
206
211
|
|
207
212
|
describe 'FormWithWordLimitedField after parsing input with too many words' do
|
@@ -218,7 +223,7 @@ describe 'FormWithWordLimitedField after parsing input with too many words' do
|
|
218
223
|
end
|
219
224
|
|
220
225
|
class FormWithFieldThatMustMatchPattern < Formeze::Form
|
221
|
-
field :number, pattern
|
226
|
+
field :number, :pattern => /\A\d+\z/
|
222
227
|
end
|
223
228
|
|
224
229
|
describe 'FormWithFieldThatMustMatchPattern after parsing input that matches the pattern' do
|
@@ -248,7 +253,7 @@ describe 'FormWithFieldThatMustMatchPattern after parsing input that does not ma
|
|
248
253
|
end
|
249
254
|
|
250
255
|
class FormWithFieldThatCanHaveMultipleValues < Formeze::Form
|
251
|
-
field :colour, multiple
|
256
|
+
field :colour, :multiple => true
|
252
257
|
end
|
253
258
|
|
254
259
|
describe 'FormWithFieldThatCanHaveMultipleValues' do
|
@@ -305,6 +310,12 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with multip
|
|
305
310
|
@form.valid?.must_equal(true)
|
306
311
|
end
|
307
312
|
end
|
313
|
+
|
314
|
+
describe 'to_hash method' do
|
315
|
+
it 'should return a hash containing the field name and its array value' do
|
316
|
+
@form.to_hash.must_equal({:colour => %w(black white)})
|
317
|
+
end
|
318
|
+
end
|
308
319
|
end
|
309
320
|
|
310
321
|
describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with no values' do
|
@@ -328,7 +339,7 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with no val
|
|
328
339
|
end
|
329
340
|
|
330
341
|
class FormWithFieldThatCanOnlyHaveSpecifiedValues < Formeze::Form
|
331
|
-
field :answer, values
|
342
|
+
field :answer, :values => %w(yes no)
|
332
343
|
end
|
333
344
|
|
334
345
|
describe 'FormWithFieldThatCanOnlyHaveSpecifiedValues after parsing input with an invalid value' do
|
@@ -346,7 +357,7 @@ end
|
|
346
357
|
|
347
358
|
class FormWithGuardCondition < Formeze::Form
|
348
359
|
field :account_name
|
349
|
-
field :account_vat_number, defined_if
|
360
|
+
field :account_vat_number, :defined_if => proc { @business_account }
|
350
361
|
|
351
362
|
def initialize(business_account)
|
352
363
|
@business_account = business_account
|
@@ -392,8 +403,8 @@ end
|
|
392
403
|
|
393
404
|
class FormWithHaltingCondition < Formeze::Form
|
394
405
|
field :delivery_address
|
395
|
-
field :same_address, values
|
396
|
-
field :billing_address, defined_unless
|
406
|
+
field :same_address, :values => %w(yes no)
|
407
|
+
field :billing_address, :defined_unless => :same_address?
|
397
408
|
|
398
409
|
def same_address?
|
399
410
|
same_address == 'yes'
|
@@ -446,7 +457,7 @@ describe 'FormWithCustomValidation after parsing invalid input' do
|
|
446
457
|
end
|
447
458
|
|
448
459
|
class FormWithOptionalKey < Formeze::Form
|
449
|
-
field :accept_terms, values
|
460
|
+
field :accept_terms, :values => %w(true), :key_required => false
|
450
461
|
end
|
451
462
|
|
452
463
|
describe 'FormWithOptionalKey after parsing input without the key' do
|
@@ -463,7 +474,7 @@ describe 'FormWithOptionalKey after parsing input without the key' do
|
|
463
474
|
end
|
464
475
|
|
465
476
|
class FormWithOptionalFieldThatCanOnlyHaveSpecifiedValues < Formeze::Form
|
466
|
-
field :size, required
|
477
|
+
field :size, :required => false, :values => %w(S M L XL)
|
467
478
|
end
|
468
479
|
|
469
480
|
describe 'FormWithOptionalFieldThatCanOnlyHaveSpecifiedValues after parsing blank input' do
|
@@ -514,7 +525,7 @@ describe 'I18n integration' do
|
|
514
525
|
end
|
515
526
|
|
516
527
|
it 'should be possible to override the default error messages' do
|
517
|
-
I18n.backend.store_translations :en, {formeze
|
528
|
+
I18n.backend.store_translations :en, {:formeze => {:errors => {:required => 'cannot be blank'}}}
|
518
529
|
|
519
530
|
form = FormWithField.new
|
520
531
|
form.parse('title=')
|
@@ -522,7 +533,7 @@ describe 'I18n integration' do
|
|
522
533
|
end
|
523
534
|
|
524
535
|
it 'should be possible to set labels globally' do
|
525
|
-
I18n.backend.store_translations :en, {formeze
|
536
|
+
I18n.backend.store_translations :en, {:formeze => {:labels => {:title => 'TITLE'}}}
|
526
537
|
|
527
538
|
form = FormWithField.new
|
528
539
|
form.parse('title=')
|
@@ -531,8 +542,8 @@ describe 'I18n integration' do
|
|
531
542
|
end
|
532
543
|
|
533
544
|
class FormWithScrubbedFields < Formeze::Form
|
534
|
-
field :postcode, scrub
|
535
|
-
field :bio, scrub
|
545
|
+
field :postcode, :scrub => [:strip, :squeeze, :upcase], :pattern => /\A[A-Z0-9]{2,4} [A-Z0-9]{3}\z/
|
546
|
+
field :bio, :scrub => [:strip, :squeeze_lines], :multiline => true
|
536
547
|
end
|
537
548
|
|
538
549
|
describe 'FormWithScrubbedFields' do
|
@@ -541,7 +552,7 @@ describe 'FormWithScrubbedFields' do
|
|
541
552
|
form = FormWithScrubbedFields.new
|
542
553
|
form.parse('postcode=++sw1a+++1aa&bio=My+name+is+Cookie+Monster.%0A%0A%0A%0AI+LOVE+COOKIES!!!!%0A%0A%0A%0A')
|
543
554
|
form.postcode.must_equal('SW1A 1AA')
|
544
|
-
form.bio.count(
|
555
|
+
form.bio.count("\n").must_equal(2)
|
545
556
|
form.valid?.must_equal(true)
|
546
557
|
end
|
547
558
|
end
|
@@ -560,8 +571,19 @@ class FormClassWithExplicitSetupCall
|
|
560
571
|
end
|
561
572
|
|
562
573
|
describe 'FormClassWithExplicitSetupCall' do
|
574
|
+
before do
|
575
|
+
@form_class = FormClassWithExplicitSetupCall
|
576
|
+
end
|
577
|
+
|
563
578
|
it 'includes the formeze class methods and instance methods' do
|
564
|
-
|
565
|
-
|
579
|
+
singleton_class = if @form.respond_to?(:singleton_class)
|
580
|
+
@form_class.singleton_class
|
581
|
+
else
|
582
|
+
(class << @form_class; self; end)
|
583
|
+
end
|
584
|
+
|
585
|
+
singleton_class.must_include(Formeze::ClassMethods)
|
586
|
+
|
587
|
+
@form_class.must_include(Formeze::InstanceMethods)
|
566
588
|
end
|
567
589
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formeze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.3
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.3
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: i18n
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|