formeze 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +28 -0
- data/formeze.gemspec +1 -1
- data/lib/formeze.rb +21 -23
- data/spec/formeze_spec.rb +5 -12
- metadata +3 -3
data/README.md
CHANGED
@@ -122,6 +122,34 @@ field :colour, multiple: true, values: Colour.keys
|
|
122
122
|
Unlike all the other examples so far, reading the attribute that corresponds
|
123
123
|
to this field will return an array of strings instead of a single string.
|
124
124
|
|
125
|
+
Sometimes you'll only want the field to be defined if some condition is true.
|
126
|
+
The condition may depend on the state of other form fields, or some external
|
127
|
+
state accessible from the form object. You can do this by specifying either
|
128
|
+
the `defined_if` or `defined_unless` options with a proc. Here's an example
|
129
|
+
of using the defined_if option:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
field :business_name, defined_if: proc { @account.business? }
|
133
|
+
```
|
134
|
+
|
135
|
+
In this example the `business_name` field will only be defined and validated
|
136
|
+
for business accounts. The proc is evaluated in the context of the form object,
|
137
|
+
so has full access to instance variables and methods defined on the object.
|
138
|
+
Here's an example of using the defined_unless option:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
field :same_address, values: %w(true), key_required: false
|
142
|
+
|
143
|
+
field :billing_address_line_one, defined_unless: proc { same_address? }
|
144
|
+
|
145
|
+
def same_address?
|
146
|
+
same_address == 'true'
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
In this example, the `billing_address_line_one` field will only be defined
|
151
|
+
and validated if the `same_address` checkbox is checked.
|
152
|
+
|
125
153
|
|
126
154
|
Rails usage
|
127
155
|
-----------
|
data/formeze.gemspec
CHANGED
data/lib/formeze.rb
CHANGED
@@ -85,21 +85,21 @@ module Formeze
|
|
85
85
|
def values
|
86
86
|
@options.fetch(:values)
|
87
87
|
end
|
88
|
-
end
|
89
88
|
|
90
|
-
|
91
|
-
|
89
|
+
def defined_if?
|
90
|
+
@options.has_key?(:defined_if)
|
91
|
+
end
|
92
92
|
|
93
|
-
def
|
94
|
-
@
|
93
|
+
def defined_if
|
94
|
+
@options.fetch(:defined_if)
|
95
95
|
end
|
96
|
-
end
|
97
96
|
|
98
|
-
|
99
|
-
|
97
|
+
def defined_unless?
|
98
|
+
@options.has_key?(:defined_unless)
|
99
|
+
end
|
100
100
|
|
101
|
-
def
|
102
|
-
@
|
101
|
+
def defined_unless
|
102
|
+
@options.fetch(:defined_unless)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -159,14 +159,6 @@ module Formeze
|
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
|
-
def guard(&block)
|
163
|
-
fields << GuardCondition.new(block)
|
164
|
-
end
|
165
|
-
|
166
|
-
def halts(&block)
|
167
|
-
fields << HaltingCondition.new(block)
|
168
|
-
end
|
169
|
-
|
170
162
|
def checks
|
171
163
|
@checks ||= []
|
172
164
|
end
|
@@ -195,11 +187,7 @@ module Formeze
|
|
195
187
|
form_data = CGI.parse(encoded_form_data)
|
196
188
|
|
197
189
|
self.class.fields.each do |field|
|
198
|
-
|
199
|
-
instance_eval(&field.block) ? next : break
|
200
|
-
elsif field.is_a?(HaltingCondition)
|
201
|
-
instance_eval(&field.block) ? break : next
|
202
|
-
end
|
190
|
+
next unless field_defined?(field)
|
203
191
|
|
204
192
|
unless form_data.has_key?(field.key)
|
205
193
|
next if field.multiple? || !field.key_required?
|
@@ -229,6 +217,16 @@ module Formeze
|
|
229
217
|
end
|
230
218
|
end
|
231
219
|
|
220
|
+
def field_defined?(field)
|
221
|
+
if field.defined_if?
|
222
|
+
instance_eval(&field.defined_if)
|
223
|
+
elsif field.defined_unless?
|
224
|
+
!instance_eval(&field.defined_unless)
|
225
|
+
else
|
226
|
+
true
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
232
230
|
def errors
|
233
231
|
@errors ||= []
|
234
232
|
end
|
data/spec/formeze_spec.rb
CHANGED
@@ -313,24 +313,21 @@ class FormWithGuardCondition
|
|
313
313
|
Formeze.setup(self)
|
314
314
|
|
315
315
|
field :account_name
|
316
|
-
|
317
|
-
guard { @business_account }
|
318
|
-
|
319
|
-
field :account_vat_number
|
316
|
+
field :account_vat_number, defined_if: proc { @business_account }
|
320
317
|
|
321
318
|
def initialize(business_account)
|
322
319
|
@business_account = business_account
|
323
320
|
end
|
324
321
|
end
|
325
322
|
|
326
|
-
describe 'FormWithGuardCondition' do
|
323
|
+
describe 'FormWithGuardCondition with business_account set to false' do
|
327
324
|
before do
|
328
325
|
@form = FormWithGuardCondition.new(false)
|
329
326
|
end
|
330
327
|
|
331
328
|
describe 'parse method' do
|
332
|
-
it 'should raise an exception when
|
333
|
-
proc { @form.parse('account_name=Something&
|
329
|
+
it 'should raise an exception when the account_vat_number is present' do
|
330
|
+
proc { @form.parse('account_name=Something&account_vat_number=123456789') }.must_raise(Formeze::KeyError)
|
334
331
|
end
|
335
332
|
end
|
336
333
|
end
|
@@ -364,12 +361,8 @@ class FormWithHaltingCondition
|
|
364
361
|
Formeze.setup(self)
|
365
362
|
|
366
363
|
field :delivery_address
|
367
|
-
|
368
364
|
field :same_address, values: %w(yes no)
|
369
|
-
|
370
|
-
halts { same_address? }
|
371
|
-
|
372
|
-
field :billing_address
|
365
|
+
field :billing_address, defined_unless: :same_address?
|
373
366
|
|
374
367
|
def same_address?
|
375
368
|
same_address == 'yes'
|
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.2.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-
|
12
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A little library for handling form data/input
|
15
15
|
email:
|
@@ -43,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
43
|
version: '0'
|
44
44
|
requirements: []
|
45
45
|
rubyforge_project:
|
46
|
-
rubygems_version: 1.8.
|
46
|
+
rubygems_version: 1.8.16
|
47
47
|
signing_key:
|
48
48
|
specification_version: 3
|
49
49
|
summary: See description
|