Skip to content

Commit dd77495

Browse files
committed
Add options to Form#valid? to allow skipping of nested and array forms
1 parent 5354ef1 commit dd77495

File tree

6 files changed

+96
-11
lines changed

6 files changed

+96
-11
lines changed

Diff for: Gemfile.lock

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GIT
88
PATH
99
remote: .
1010
specs:
11-
rectify (0.9.1)
11+
rectify (0.10.0)
1212
activemodel (>= 4.1.0)
1313
activerecord (>= 4.1.0)
1414
activesupport (>= 4.1.0)
@@ -45,6 +45,7 @@ GEM
4545
thread_safe (~> 0.3, >= 0.3.4)
4646
tzinfo (~> 1.1)
4747
arel (6.0.3)
48+
ast (2.2.0)
4849
awesome_print (1.6.1)
4950
axiom-types (0.1.1)
5051
descendants_tracker (~> 0.0.4)
@@ -69,6 +70,9 @@ GEM
6970
minitest (5.8.4)
7071
nokogiri (1.6.7.2)
7172
mini_portile2 (~> 2.0.0.rc2)
73+
parser (2.3.0.6)
74+
ast (~> 2.2)
75+
powerpack (0.1.1)
7276
pry (0.10.3)
7377
coderay (~> 1.1.0)
7478
method_source (~> 0.8.1)
@@ -84,6 +88,7 @@ GEM
8488
rails-deprecated_sanitizer (>= 1.0.1)
8589
rails-html-sanitizer (1.0.3)
8690
loofah (~> 2.0)
91+
rainbow (2.1.0)
8792
rake (11.1.2)
8893
rspec (3.4.0)
8994
rspec-core (~> 3.4.0)
@@ -100,11 +105,19 @@ GEM
100105
diff-lcs (>= 1.2.0, < 2.0)
101106
rspec-support (~> 3.4.0)
102107
rspec-support (3.4.1)
108+
rubocop (0.37.2)
109+
parser (>= 2.3.0.4, < 3.0)
110+
powerpack (~> 0.1)
111+
rainbow (>= 1.99.1, < 3.0)
112+
ruby-progressbar (~> 1.7)
113+
unicode-display_width (~> 0.3)
114+
ruby-progressbar (1.7.5)
103115
slop (3.6.0)
104116
sqlite3 (1.3.11)
105117
thread_safe (0.3.5)
106118
tzinfo (1.2.2)
107119
thread_safe (~> 0.1)
120+
unicode-display_width (0.3.1)
108121
virtus (1.0.5)
109122
axiom-types (~> 0.1)
110123
coercible (~> 1.0)
@@ -123,6 +136,7 @@ DEPENDENCIES
123136
rectify!
124137
rspec (~> 3.4)
125138
rspec-collection_matchers (~> 1.1)
139+
rubocop
126140
sqlite3
127141
wisper-rspec!
128142

Diff for: lib/rectify/form.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,21 @@ def persisted?
5858
id.present? && id.to_i > 0
5959
end
6060

61-
def valid?(context = nil)
61+
def valid?(options = {})
6262
before_validation
6363

64-
[super, form_attributes_valid?, arrays_attributes_valid?].all?
64+
options = {} if options.blank?
65+
context = options[:context]
66+
validations = [super(context)]
67+
68+
validations << form_attributes_valid? unless options[:exclude_nested]
69+
validations << array_attributes_valid? unless options[:exclude_arrays]
70+
71+
validations.all?
72+
end
73+
74+
def invalid?(options = {})
75+
!valid?(options)
6576
end
6677

6778
def to_key
@@ -119,7 +130,7 @@ def form_attributes_valid?
119130
.all?
120131
end
121132

122-
def arrays_attributes_valid?
133+
def array_attributes_valid?
123134
array_attributes_that_respond_to(:valid?)
124135
.map(&:valid?)
125136
.all?

Diff for: lib/rectify/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Rectify
2-
VERSION = "0.9.1".freeze
2+
VERSION = "0.10.0".freeze
33
end

Diff for: readme.md

+42-5
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,43 @@ your form as well as any (deeply) nested form objects and array attributes that
371371
contain form objects. There is also an `#invalid?` method that returns the
372372
opposite of `#valid?`.
373373

374+
The `#valid?` and `#invalid?` methods also take a set of options. These options allow
375+
you to not validate nested form objects or array attributes that contain form objects.
376+
For example:
377+
378+
```ruby
379+
class UserForm < Rectify::Form
380+
attribute :name, String
381+
attribute :address, AddressForm
382+
attribute :contacts, Array[ContactForm]
383+
384+
validates :name, :presence => true
385+
end
386+
387+
class AddressForm < Rectify::Form
388+
attribute :street, String
389+
attribute :town, String
390+
attribute :city, String
391+
attribute :post_code, String
392+
393+
validates :street, :post_code, :presence => true
394+
end
395+
396+
class ContactForm < Rectify::Form
397+
attribute :name, String
398+
attribute :number, String
399+
400+
validates :name, :presence => true
401+
end
402+
403+
form = UserForm.from_params(params)
404+
405+
form.valid?(:exclude_nested => true, :exclude_arrays => true)
406+
```
407+
408+
In this case, the `UserForm` attributes will be validated (`name` in the example above)
409+
but the `address` and `contacts` will not be validated.
410+
374411
### Deep Context
375412

376413
It's sometimes useful to have some context within your form objects when performing
@@ -1054,11 +1091,11 @@ application. Something like the following:
10541091
└── app
10551092
├── controllers
10561093
├── core
1057-
   ├── billing
1058-
   ├── fulfillment
1059-
   ├── ordering
1060-
   ├── reporting
1061-
   └── security
1094+
├── billing
1095+
├── fulfillment
1096+
├── ordering
1097+
├── reporting
1098+
└── security
10621099
├── models
10631100
└── views
10641101
```

Diff for: rectify.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
2525
s.add_development_dependency "rspec-collection_matchers", "~> 1.1"
2626
s.add_development_dependency "sqlite3"
2727
s.add_development_dependency "rake"
28+
s.add_development_dependency "rubocop"
2829
end

Diff for: spec/lib/rectify/form_spec.rb

+23-1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@
418418
expect(form.head).not_to be_valid
419419
end
420420
end
421+
422+
context "when ignoring nested forms with invalid values" do
423+
it "returns true" do
424+
form = SchoolForm.new(:head => TeacherForm.new(:name => ""))
425+
426+
expect(form).to be_valid(:exclude_nested => true)
427+
end
428+
end
421429
end
422430

423431
describe "validating array attributes containing forms" do
@@ -435,12 +443,26 @@
435443

436444
context "when the array of forms has invalid values" do
437445
it "returns false" do
438-
form = UserForm.new(:contacts => [ContactForm.new(:name => "")])
446+
form = UserForm.new(
447+
:first_name => "Andy",
448+
:contacts => [ContactForm.new(:name => "")]
449+
)
439450

440451
expect(form).not_to be_valid
441452
expect(form.contacts.first).not_to be_valid
442453
end
443454
end
455+
456+
context "when ignoring narray of forms with invalid values" do
457+
it "returns true" do
458+
form = UserForm.new(
459+
:first_name => "Andy",
460+
:contacts => [ContactForm.new(:name => "")]
461+
)
462+
463+
expect(form).to be_valid(:exclude_arrays => true)
464+
end
465+
end
444466
end
445467
end
446468

0 commit comments

Comments
 (0)