formeze 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -206,8 +206,8 @@ else
206
206
  end
207
207
  ```
208
208
 
209
- Formeze will automatically define optional "utf8" and "authenticity_token"
210
- fields on every form so that you don't have to specify those manually.
209
+ Formeze will automatically ignore the "utf8" and "authenticity_token"
210
+ parameters that Rails uses, so you don't have to handle those manually.
211
211
 
212
212
 
213
213
  Sinatra usage
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'formeze'
3
- s.version = '1.7.0'
3
+ s.version = '1.8.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -185,6 +185,16 @@ module Formeze
185
185
  class ValidationError < StandardError; end
186
186
 
187
187
  module InstanceMethods
188
+ def fill(object)
189
+ self.class.fields.each do |field|
190
+ if Hash === object && object.has_key?(field.name)
191
+ send(:"#{field.name}=", object[field.name])
192
+ elsif object.respond_to?(field.name)
193
+ send(:"#{field.name}=", object.send(field.name))
194
+ end
195
+ end
196
+ end
197
+
188
198
  def parse(encoded_form_data)
189
199
  form_data = CGI.parse(encoded_form_data)
190
200
 
@@ -214,6 +224,10 @@ module Formeze
214
224
  end
215
225
  end
216
226
 
227
+ if defined?(Rails)
228
+ %w(utf8 authenticity_token).each { |field_key| form_data.delete(field_key) }
229
+ end
230
+
217
231
  raise KeyError unless form_data.empty?
218
232
 
219
233
  self.class.checks.zip(self.class.errors) do |check, message|
@@ -293,16 +307,6 @@ module Formeze
293
307
  form.send :include, InstanceMethods
294
308
 
295
309
  form.extend ClassMethods
296
-
297
- if on_rails?
298
- form.field(:utf8, :key_required => false)
299
-
300
- form.field(:authenticity_token, :key_required => false)
301
- end
302
- end
303
-
304
- def self.on_rails?
305
- defined?(Rails)
306
310
  end
307
311
 
308
312
  class Form
@@ -12,36 +12,52 @@ describe 'FormWithField' do
12
12
  end
13
13
 
14
14
  describe 'title method' do
15
- it 'should return nil' do
15
+ it 'returns nil' do
16
16
  @form.title.must_be_nil
17
17
  end
18
18
  end
19
19
 
20
20
  describe 'title equals method' do
21
- it 'should set the value of the title attribute' do
21
+ it 'sets the value of the title attribute' do
22
22
  @form.title = 'Untitled'
23
23
  @form.title.must_equal('Untitled')
24
24
  end
25
25
  end
26
26
 
27
27
  describe 'parse method' do
28
- it 'should set the value of the title attribute' do
28
+ it 'sets the value of the title attribute' do
29
29
  @form.parse('title=Untitled')
30
30
  @form.title.must_equal('Untitled')
31
31
  end
32
32
 
33
- it 'should raise an exception when the key is missing' do
33
+ it 'raises an exception when the key is missing' do
34
34
  proc { @form.parse('') }.must_raise(Formeze::KeyError)
35
35
  end
36
36
 
37
- it 'should raise an exception when there are multiple values for the key' do
37
+ it 'raises an exception when there are multiple values for the key' do
38
38
  proc { @form.parse('title=foo&title=bar') }.must_raise(Formeze::ValueError)
39
39
  end
40
40
 
41
- it 'should raise an exception when there is an unexpected key' do
41
+ it 'raises an exception when there is an unexpected key' do
42
42
  proc { @form.parse('title=Untitled&foo=bar') }.must_raise(Formeze::KeyError)
43
43
  end
44
44
  end
45
+
46
+ describe 'fill method' do
47
+ it 'sets the value of the title attribute when given a hash with symbol keys' do
48
+ @form.fill({:title => 'Untitled'})
49
+ @form.title.must_equal('Untitled')
50
+ end
51
+
52
+ it 'sets the value of the title attribute when given an object with a title attribute' do
53
+ object = Object.new
54
+
55
+ def object.title; 'Untitled' end
56
+
57
+ @form.fill(object)
58
+ @form.title.must_equal('Untitled')
59
+ end
60
+ end
45
61
  end
46
62
 
47
63
  describe 'FormWithField after parsing valid input' do
@@ -51,32 +67,32 @@ describe 'FormWithField after parsing valid input' do
51
67
  end
52
68
 
53
69
  describe 'valid query method' do
54
- it 'should return true' do
70
+ it 'returns true' do
55
71
  @form.valid?.must_equal(true)
56
72
  end
57
73
  end
58
74
 
59
75
  describe 'errors query method' do
60
- it 'should return false' do
76
+ it 'returns false' do
61
77
  @form.errors?.must_equal(false)
62
78
  end
63
79
  end
64
80
 
65
81
  describe 'errors method' do
66
- it 'should return an empty array' do
82
+ it 'returns an empty array' do
67
83
  @form.errors.must_be_instance_of(Array)
68
84
  @form.errors.must_be_empty
69
85
  end
70
86
  end
71
87
 
72
88
  describe 'errors_on query method' do
73
- it 'should return false when given the title field name' do
89
+ it 'returns false when given the title field name' do
74
90
  @form.errors_on?(:title).must_equal(false)
75
91
  end
76
92
  end
77
93
 
78
94
  describe 'errors_on method' do
79
- it 'should return an empty array when given the title field name' do
95
+ it 'returns an empty array when given the title field name' do
80
96
  errors = @form.errors_on(:title)
81
97
  errors.must_be_instance_of(Array)
82
98
  errors.must_be_empty
@@ -84,7 +100,7 @@ describe 'FormWithField after parsing valid input' do
84
100
  end
85
101
 
86
102
  describe 'to_hash method' do
87
- it 'should return a hash containing the field name and its value' do
103
+ it 'returns a hash containing the field name and its value' do
88
104
  @form.to_hash.must_equal({:title => 'Untitled'})
89
105
  end
90
106
  end
@@ -97,19 +113,19 @@ describe 'FormWithField after parsing blank input' do
97
113
  end
98
114
 
99
115
  describe 'valid query method' do
100
- it 'should return false' do
116
+ it 'returns false' do
101
117
  @form.valid?.must_equal(false)
102
118
  end
103
119
  end
104
120
 
105
121
  describe 'errors query method' do
106
- it 'should return true' do
122
+ it 'returns true' do
107
123
  @form.errors?.must_equal(true)
108
124
  end
109
125
  end
110
126
 
111
127
  describe 'errors method' do
112
- it 'should return an array containing a single error message' do
128
+ it 'returns an array containing a single error message' do
113
129
  @form.errors.must_be_instance_of(Array)
114
130
  @form.errors.length.must_equal(1)
115
131
  @form.errors.first.to_s.must_equal('Title is required')
@@ -117,13 +133,13 @@ describe 'FormWithField after parsing blank input' do
117
133
  end
118
134
 
119
135
  describe 'errors_on query method' do
120
- it 'should return true when given the title field name' do
136
+ it 'returns true when given the title field name' do
121
137
  @form.errors_on?(:title).must_equal(true)
122
138
  end
123
139
  end
124
140
 
125
141
  describe 'errors_on method' do
126
- it 'should return an array containing a single error message when given the title field name' do
142
+ it 'returns an array containing a single error message when given the title field name' do
127
143
  errors = @form.errors_on(:title)
128
144
  errors.must_be_instance_of(Array)
129
145
  errors.length.must_equal(1)
@@ -139,14 +155,14 @@ describe 'FormWithField after parsing input containing newlines' do
139
155
  end
140
156
 
141
157
  describe 'valid query method' do
142
- it 'should return false' do
158
+ it 'returns false' do
143
159
  @form.valid?.must_equal(false)
144
160
  end
145
161
  end
146
162
  end
147
163
 
148
164
  describe 'FormWithField parse class method' do
149
- it 'should create a new instance of the class and call the parse instance method' do
165
+ it 'creates a new instance of the class and calls the parse instance method' do
150
166
  form = FormWithField.parse('title=Untitled')
151
167
  form.must_be_instance_of(FormWithField)
152
168
  form.valid?.must_equal(true)
@@ -165,7 +181,7 @@ describe 'FormWithOptionalField after parsing blank input' do
165
181
  end
166
182
 
167
183
  describe 'valid query method' do
168
- it 'should return true' do
184
+ it 'returns true' do
169
185
  @form.valid?.must_equal(true)
170
186
  end
171
187
  end
@@ -182,7 +198,7 @@ describe 'FormWithFieldThatCanHaveMultipleLines after parsing input containing n
182
198
  end
183
199
 
184
200
  describe 'valid query method' do
185
- it 'should return true' do
201
+ it 'returns true' do
186
202
  @form.valid?.must_equal(true)
187
203
  end
188
204
  end
@@ -199,7 +215,7 @@ describe 'FormWithCharacterLimitedField after parsing input with too many charac
199
215
  end
200
216
 
201
217
  describe 'valid query method' do
202
- it 'should return false' do
218
+ it 'returns false' do
203
219
  @form.valid?.must_equal(false)
204
220
  end
205
221
  end
@@ -216,7 +232,7 @@ describe 'FormWithWordLimitedField after parsing input with too many words' do
216
232
  end
217
233
 
218
234
  describe 'valid query method' do
219
- it 'should return false' do
235
+ it 'returns false' do
220
236
  @form.valid?.must_equal(false)
221
237
  end
222
238
  end
@@ -233,7 +249,7 @@ describe 'FormWithFieldThatMustMatchPattern after parsing input that matches the
233
249
  end
234
250
 
235
251
  describe 'valid query method' do
236
- it 'should return true' do
252
+ it 'returns true' do
237
253
  @form.valid?.must_equal(true)
238
254
  end
239
255
  end
@@ -246,7 +262,7 @@ describe 'FormWithFieldThatMustMatchPattern after parsing input that does not ma
246
262
  end
247
263
 
248
264
  describe 'valid query method' do
249
- it 'should return false' do
265
+ it 'returns false' do
250
266
  @form.valid?.must_equal(false)
251
267
  end
252
268
  end
@@ -262,30 +278,30 @@ describe 'FormWithFieldThatCanHaveMultipleValues' do
262
278
  end
263
279
 
264
280
  describe 'colour method' do
265
- it 'should return an empty array' do
281
+ it 'returns an empty array' do
266
282
  @form.colour.must_be_instance_of(Array)
267
283
  @form.colour.must_be_empty
268
284
  end
269
285
  end
270
286
 
271
287
  describe 'colour equals method' do
272
- it 'should add the argument to the colour attribute array' do
288
+ it 'adds the argument to the colour attribute array' do
273
289
  @form.colour = 'black'
274
290
  @form.colour.must_include('black')
275
291
  end
276
292
  end
277
293
 
278
294
  describe 'parse method' do
279
- it 'should add the value to the colour attribute array' do
295
+ it 'adds the value to the colour attribute array' do
280
296
  @form.parse('colour=black')
281
297
  @form.colour.must_include('black')
282
298
  end
283
299
 
284
- it 'should not raise an exception when there are multiple values for the key' do
300
+ it 'does not raise an exception when there are multiple values for the key' do
285
301
  @form.parse('colour=black&colour=white')
286
302
  end
287
303
 
288
- it 'should not raise an exception when the key is missing' do
304
+ it 'does not raise an exception when the key is missing' do
289
305
  @form.parse('')
290
306
  end
291
307
  end
@@ -298,7 +314,7 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with multip
298
314
  end
299
315
 
300
316
  describe 'colour method' do
301
- it 'should return an array containing the values' do
317
+ it 'returns an array containing the values' do
302
318
  @form.colour.must_be_instance_of(Array)
303
319
  @form.colour.must_include('black')
304
320
  @form.colour.must_include('white')
@@ -306,13 +322,13 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with multip
306
322
  end
307
323
 
308
324
  describe 'valid query method' do
309
- it 'should return true' do
325
+ it 'returns true' do
310
326
  @form.valid?.must_equal(true)
311
327
  end
312
328
  end
313
329
 
314
330
  describe 'to_hash method' do
315
- it 'should return a hash containing the field name and its array value' do
331
+ it 'returns a hash containing the field name and its array value' do
316
332
  @form.to_hash.must_equal({:colour => %w(black white)})
317
333
  end
318
334
  end
@@ -325,14 +341,14 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with no val
325
341
  end
326
342
 
327
343
  describe 'colour method' do
328
- it 'should return an empty array' do
344
+ it 'returns an empty array' do
329
345
  @form.colour.must_be_instance_of(Array)
330
346
  @form.colour.must_be_empty
331
347
  end
332
348
  end
333
349
 
334
350
  describe 'valid query method' do
335
- it 'should return true' do
351
+ it 'returns true' do
336
352
  @form.valid?.must_equal(true)
337
353
  end
338
354
  end
@@ -349,7 +365,7 @@ describe 'FormWithFieldThatCanOnlyHaveSpecifiedValues after parsing input with a
349
365
  end
350
366
 
351
367
  describe 'valid query method' do
352
- it 'should return false' do
368
+ it 'returns false' do
353
369
  @form.valid?.must_equal(false)
354
370
  end
355
371
  end
@@ -370,7 +386,7 @@ describe 'FormWithGuardCondition with business_account set to false' do
370
386
  end
371
387
 
372
388
  describe 'parse method' do
373
- it 'should raise an exception when the account_vat_number is present' do
389
+ it 'raises an exception when the account_vat_number key is present' do
374
390
  proc { @form.parse('account_name=Something&account_vat_number=123456789') }.must_raise(Formeze::KeyError)
375
391
  end
376
392
  end
@@ -382,7 +398,7 @@ describe 'FormWithGuardCondition with business_account set to true' do
382
398
  end
383
399
 
384
400
  describe 'parse method' do
385
- it 'should raise an exception when the account_vat_number key is missing' do
401
+ it 'raises an exception when the account_vat_number key is missing' do
386
402
  proc { @form.parse('account_name=Something') }.must_raise(Formeze::KeyError)
387
403
  end
388
404
  end
@@ -395,7 +411,7 @@ describe 'FormWithGuardCondition with business_account set to false after parsin
395
411
  end
396
412
 
397
413
  describe 'valid query method' do
398
- it 'should return true' do
414
+ it 'returns true' do
399
415
  @form.valid?.must_equal(true)
400
416
  end
401
417
  end
@@ -417,7 +433,7 @@ describe 'FormWithHaltingCondition' do
417
433
  end
418
434
 
419
435
  describe 'parse method' do
420
- it 'should raise an exception when there is an unexpected key' do
436
+ it 'raises an exception when there is an unexpected key' do
421
437
  proc { @form.parse('delivery_address=123+Main+St&same_address=yes&foo=bar') }.must_raise(Formeze::KeyError)
422
438
  end
423
439
  end
@@ -430,7 +446,7 @@ describe 'FormWithHaltingCondition after parsing input with same_address set and
430
446
  end
431
447
 
432
448
  describe 'valid query method' do
433
- it 'should return true' do
449
+ it 'returns true' do
434
450
  @form.valid?.must_equal(true)
435
451
  end
436
452
  end
@@ -450,7 +466,7 @@ describe 'FormWithCustomValidation after parsing invalid input' do
450
466
  end
451
467
 
452
468
  describe 'valid query method' do
453
- it 'should return false' do
469
+ it 'returns false' do
454
470
  @form.valid?.must_equal(false)
455
471
  end
456
472
  end
@@ -467,7 +483,7 @@ describe 'FormWithOptionalKey after parsing input without the key' do
467
483
  end
468
484
 
469
485
  describe 'valid query method' do
470
- it 'should return true' do
486
+ it 'returns true' do
471
487
  @form.valid?.must_equal(true)
472
488
  end
473
489
  end
@@ -484,33 +500,29 @@ describe 'FormWithOptionalFieldThatCanOnlyHaveSpecifiedValues after parsing blan
484
500
  end
485
501
 
486
502
  describe 'valid query method' do
487
- it 'should return true' do
503
+ it 'returns true' do
488
504
  @form.valid?.must_equal(true)
489
505
  end
490
506
  end
491
507
  end
492
508
 
493
- Rails = Object.new
509
+ describe 'FormWithField on Rails' do
510
+ before do
511
+ @form = FormWithField.new
494
512
 
495
- class RailsForm < Formeze::Form
496
- field :title
497
- end
513
+ Object.const_set(:Rails, Object.new)
514
+ end
498
515
 
499
- describe 'RailsForm' do
500
- before do
501
- @form = RailsForm.new
516
+ after do
517
+ Object.send(:remove_const, :Rails)
502
518
  end
503
519
 
504
520
  describe 'parse method' do
505
- it 'should automatically process the utf8 and authenticity_token parameters' do
521
+ it 'silently ignores the utf8 and authenticity_token parameters' do
506
522
  @form.parse('utf8=%E2%9C%93&authenticity_token=5RMc3sPZdR%2BZz4onNS8NfK&title=Test')
507
- @form.authenticity_token.wont_be_empty
508
- @form.utf8.wont_be_empty
509
- end
510
-
511
- it 'should not complain if the utf8 or authenticity_token parameters are missing' do
512
- @form.parse('utf8=%E2%9C%93&title=Test')
513
- @form.parse('authenticity_token=5RMc3sPZdR%2BZz4onNS8NfK&title=Test')
523
+ @form.wont_respond_to(:utf8)
524
+ @form.wont_respond_to(:authenticity_token)
525
+ @form.to_hash.must_equal({:title => 'Test'})
514
526
  end
515
527
  end
516
528
  end
@@ -524,7 +536,7 @@ describe 'I18n integration' do
524
536
  I18n.backend = I18n::Backend::Simple.new
525
537
  end
526
538
 
527
- it 'should be possible to override the default error messages' do
539
+ it 'provides i18n support for overriding the default error messages' do
528
540
  I18n.backend.store_translations :en, {:formeze => {:errors => {:required => 'cannot be blank'}}}
529
541
 
530
542
  form = FormWithField.new
@@ -532,7 +544,7 @@ describe 'I18n integration' do
532
544
  form.errors.first.to_s.must_equal('Title cannot be blank')
533
545
  end
534
546
 
535
- it 'should be possible to set labels globally' do
547
+ it 'provides i18n support for specifying field labels globally' do
536
548
  I18n.backend.store_translations :en, {:formeze => {:labels => {:title => 'TITLE'}}}
537
549
 
538
550
  form = FormWithField.new
@@ -548,7 +560,7 @@ end
548
560
 
549
561
  describe 'FormWithScrubbedFields' do
550
562
  describe 'parse method' do
551
- it 'should apply the scrub methods to the input before validation' do
563
+ it 'applies the scrub methods to the input before validation' do
552
564
  form = FormWithScrubbedFields.new
553
565
  form.parse('postcode=++sw1a+++1aa&bio=My+name+is+Cookie+Monster.%0A%0A%0A%0AI+LOVE+COOKIES!!!!%0A%0A%0A%0A')
554
566
  form.postcode.must_equal('SW1A 1AA')
@@ -560,7 +572,7 @@ end
560
572
 
561
573
  describe 'Formeze' do
562
574
  describe 'scrub module method' do
563
- it 'should apply the scrub methods to the given input' do
575
+ it 'applies the scrub methods to the given input' do
564
576
  Formeze.scrub("word\n\n", [:strip, :upcase]).must_equal('WORD')
565
577
  end
566
578
  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.7.0
4
+ version: 1.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake