formeze 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -1
- data/formeze.gemspec +1 -1
- data/lib/formeze.rb +11 -3
- data/spec/formeze_spec.rb +20 -0
- metadata +3 -2
data/README.md
CHANGED
@@ -93,7 +93,7 @@ well defined formats, like numbers. For example:
|
|
93
93
|
```ruby
|
94
94
|
field :number, pattern: /\A[1-9]\d*\z/
|
95
95
|
|
96
|
-
field :card_security_code, char_limit: 5,
|
96
|
+
field :card_security_code, char_limit: 5, pattern: /\A\d+\z/
|
97
97
|
```
|
98
98
|
|
99
99
|
If you want to validate that the field value belongs to a set of predefined
|
@@ -187,6 +187,24 @@ Formeze will automatically define optional "utf8" and "authenticity_token"
|
|
187
187
|
fields on every form so that you don't have to specify those manually.
|
188
188
|
|
189
189
|
|
190
|
+
Sinatra usage
|
191
|
+
-------------
|
192
|
+
|
193
|
+
Using formeze with sinatra is similar, the only difference is that there is
|
194
|
+
no raw_post method on the request object so the body has to be read directly:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
form = SomeForm.new
|
198
|
+
form.parse(request.body.read)
|
199
|
+
|
200
|
+
if form.valid?
|
201
|
+
# do something with form data
|
202
|
+
else
|
203
|
+
# display form.errors to user
|
204
|
+
end
|
205
|
+
```
|
206
|
+
|
207
|
+
|
190
208
|
Integration with I18n
|
191
209
|
---------------------
|
192
210
|
|
data/formeze.gemspec
CHANGED
data/lib/formeze.rb
CHANGED
@@ -19,9 +19,7 @@ module Formeze
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def scrub(value)
|
22
|
-
|
23
|
-
Formeze.scrub_methods.fetch(scrub_method).call(tmp)
|
24
|
-
end
|
22
|
+
Formeze.scrub(value, @options[:scrub])
|
25
23
|
end
|
26
24
|
|
27
25
|
def validate(value)
|
@@ -249,6 +247,10 @@ module Formeze
|
|
249
247
|
@errors ||= []
|
250
248
|
end
|
251
249
|
|
250
|
+
def errors?
|
251
|
+
errors.size > 0
|
252
|
+
end
|
253
|
+
|
252
254
|
def valid?
|
253
255
|
errors.empty?
|
254
256
|
end
|
@@ -264,6 +266,12 @@ module Formeze
|
|
264
266
|
}
|
265
267
|
end
|
266
268
|
|
269
|
+
def self.scrub(input, method_names)
|
270
|
+
Array(method_names).inject(input) do |tmp, method_name|
|
271
|
+
scrub_methods.fetch(method_name).call(tmp)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
267
275
|
def self.setup(form)
|
268
276
|
form.send :include, InstanceMethods
|
269
277
|
|
data/spec/formeze_spec.rb
CHANGED
@@ -59,6 +59,12 @@ describe 'FormWithField after parsing valid input' do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
describe 'errors query method' do
|
63
|
+
it 'should return false' do
|
64
|
+
@form.errors?.must_equal(false)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
62
68
|
describe 'errors method' do
|
63
69
|
it 'should return an empty array' do
|
64
70
|
@form.errors.must_be_instance_of(Array)
|
@@ -79,6 +85,12 @@ describe 'FormWithField after parsing blank input' do
|
|
79
85
|
end
|
80
86
|
end
|
81
87
|
|
88
|
+
describe 'errors query method' do
|
89
|
+
it 'should return true' do
|
90
|
+
@form.errors?.must_equal(true)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
82
94
|
describe 'errors method' do
|
83
95
|
it 'should return an array containing an error message' do
|
84
96
|
@form.errors.must_be_instance_of(Array)
|
@@ -526,3 +538,11 @@ describe 'FormWithScrubbedFields' do
|
|
526
538
|
end
|
527
539
|
end
|
528
540
|
end
|
541
|
+
|
542
|
+
describe 'Formeze' do
|
543
|
+
describe 'scrub module method' do
|
544
|
+
it 'should apply the scrub methods to the given input' do
|
545
|
+
Formeze.scrub("word\n\n", [:strip, :upcase]).must_equal('WORD')
|
546
|
+
end
|
547
|
+
end
|
548
|
+
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.5.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-08
|
12
|
+
date: 2012-10-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|
@@ -64,3 +64,4 @@ signing_key:
|
|
64
64
|
specification_version: 3
|
65
65
|
summary: See description
|
66
66
|
test_files: []
|
67
|
+
has_rdoc:
|