formeze 1.8.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -1
- data/formeze.gemspec +1 -1
- data/lib/formeze.rb +14 -12
- data/spec/formeze_spec.rb +23 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -83,7 +83,7 @@ Field options
|
|
83
83
|
-------------
|
84
84
|
|
85
85
|
By default fields cannot be blank, they are limited to 64 characters,
|
86
|
-
and they cannot contain newlines. These restrictions can be
|
86
|
+
and they cannot contain newlines. These restrictions can be overridden
|
87
87
|
by setting various field options.
|
88
88
|
|
89
89
|
Defining a field without any options works well for a simple text input.
|
@@ -94,6 +94,21 @@ it by setting the `char_limit` option. For example:
|
|
94
94
|
field :title, char_limit: 200
|
95
95
|
```
|
96
96
|
|
97
|
+
Specify the `required` option to make the field optional, i.e. the value
|
98
|
+
of the field can be blank/empty. For example:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
field :title, required: false
|
102
|
+
```
|
103
|
+
|
104
|
+
To make it easy to integrate with your application you might want to return
|
105
|
+
a different value for blank fields, such as nil, zero, or a "null" object.
|
106
|
+
Use the `blank` option to specify this behaviour. For example:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
field :title, required: false, blank: nil
|
110
|
+
```
|
111
|
+
|
97
112
|
If you are dealing with textareas (i.e. multiple lines of text) then you can
|
98
113
|
set the `multiline` option to allow newlines. For example:
|
99
114
|
|
data/formeze.gemspec
CHANGED
data/lib/formeze.rb
CHANGED
@@ -18,13 +18,13 @@ module Formeze
|
|
18
18
|
@name, @options = name, options
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
Formeze.scrub(value, @options[:scrub])
|
23
|
-
end
|
21
|
+
def validate(value, form)
|
22
|
+
value = Formeze.scrub(value, @options[:scrub])
|
24
23
|
|
25
|
-
|
26
|
-
if blank?(value)
|
24
|
+
if value !~ /\S/
|
27
25
|
yield error(:required, 'is required') if required?
|
26
|
+
|
27
|
+
form.send(:"#{name}=", blank_value? ? blank_value : value)
|
28
28
|
else
|
29
29
|
yield error(:not_multiline, 'cannot contain newlines') if !multiline? && value.lines.count > 1
|
30
30
|
|
@@ -33,6 +33,8 @@ module Formeze
|
|
33
33
|
yield error(:no_match, 'is invalid') if no_match?(value)
|
34
34
|
|
35
35
|
yield error(:bad_value, 'is invalid') if values? && !values.include?(value)
|
36
|
+
|
37
|
+
form.send(:"#{name}=", value)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
@@ -80,8 +82,12 @@ module Formeze
|
|
80
82
|
@options.has_key?(:pattern) && value !~ @options[:pattern]
|
81
83
|
end
|
82
84
|
|
83
|
-
def
|
84
|
-
|
85
|
+
def blank_value?
|
86
|
+
@options.has_key?(:blank)
|
87
|
+
end
|
88
|
+
|
89
|
+
def blank_value
|
90
|
+
@options.fetch(:blank)
|
85
91
|
end
|
86
92
|
|
87
93
|
def values?
|
@@ -214,13 +220,9 @@ module Formeze
|
|
214
220
|
end
|
215
221
|
|
216
222
|
values.each do |value|
|
217
|
-
|
218
|
-
|
219
|
-
field.validate(scrubbed_value) do |error|
|
223
|
+
field.validate(value, self) do |error|
|
220
224
|
error!("#{field.label} #{error}", field.name)
|
221
225
|
end
|
222
|
-
|
223
|
-
send(:"#{field.name}=", scrubbed_value)
|
224
226
|
end
|
225
227
|
end
|
226
228
|
|
data/spec/formeze_spec.rb
CHANGED
@@ -66,6 +66,12 @@ describe 'FormWithField after parsing valid input' do
|
|
66
66
|
@form.parse('title=Untitled')
|
67
67
|
end
|
68
68
|
|
69
|
+
describe 'title method' do
|
70
|
+
it 'returns the value of the field' do
|
71
|
+
@form.title.must_equal('Untitled')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
69
75
|
describe 'valid query method' do
|
70
76
|
it 'returns true' do
|
71
77
|
@form.valid?.must_equal(true)
|
@@ -187,6 +193,23 @@ describe 'FormWithOptionalField after parsing blank input' do
|
|
187
193
|
end
|
188
194
|
end
|
189
195
|
|
196
|
+
class FormWithOptionalFieldUsingBlankOption < Formeze::Form
|
197
|
+
field :title, :required => false, :blank => 42
|
198
|
+
end
|
199
|
+
|
200
|
+
describe 'FormWithOptionalFieldUsingBlankOption after parsing blank input' do
|
201
|
+
before do
|
202
|
+
@form = FormWithOptionalFieldUsingBlankOption.new
|
203
|
+
@form.parse('title=')
|
204
|
+
end
|
205
|
+
|
206
|
+
describe 'title method' do
|
207
|
+
it 'returns the value specified by the blank option' do
|
208
|
+
@form.title.must_equal(42)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
190
213
|
class FormWithFieldThatCanHaveMultipleLines < Formeze::Form
|
191
214
|
field :description, :multiline => true
|
192
215
|
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.9.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-
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|