also_validates 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -11
- data/also_validates.gemspec +2 -2
- data/lib/also_validates/version.rb +1 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# AlsoValidates
|
2
2
|
|
3
|
-
An ActiveModel validator that validates associated models,
|
3
|
+
An ActiveModel validator that validates associated models, copying any errors from composed models up to their parent.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem '
|
9
|
+
gem 'also_validates'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -14,36 +14,59 @@ And then execute:
|
|
14
14
|
|
15
15
|
Or install it yourself as:
|
16
16
|
|
17
|
-
$ gem install
|
17
|
+
$ gem install also_validates
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
Given existing models with their own validations:
|
22
|
+
|
21
23
|
class Beer
|
22
24
|
include ActiveModel::Validations
|
25
|
+
attr_accessor :hops
|
26
|
+
validates_presence_of :hops
|
23
27
|
|
24
|
-
|
28
|
+
def initialize(attrs = {})
|
29
|
+
@hops = attrs[:hops]
|
30
|
+
end
|
25
31
|
end
|
26
32
|
|
27
33
|
class Belly
|
28
34
|
include ActiveModel::Validations
|
35
|
+
attr_accessor :button
|
36
|
+
validates_presence_of :button
|
29
37
|
|
30
|
-
|
38
|
+
def initialize(attrs = {})
|
39
|
+
@button = attrs[:button]
|
40
|
+
end
|
31
41
|
end
|
32
42
|
|
43
|
+
And you need a model that composes those models:
|
44
|
+
|
33
45
|
class MonsterTruckRally
|
34
46
|
include ActiveModel::Validations
|
35
47
|
|
36
|
-
attr_accessor :beer, :belly
|
48
|
+
attr_accessor :beer, :belly, :truck_count
|
37
49
|
|
38
|
-
|
39
|
-
|
50
|
+
validates_presence_of :truck_count
|
51
|
+
also_validates :beer, :belly
|
40
52
|
|
41
|
-
def initialize(
|
42
|
-
@beer
|
43
|
-
@belly
|
53
|
+
def initialize(attrs = {})
|
54
|
+
@beer = attrs[:beer]
|
55
|
+
@belly = attrs[:belly]
|
56
|
+
@truck_count = attrs[:truck_count]
|
44
57
|
end
|
45
58
|
end
|
46
59
|
|
60
|
+
Calling `valid?` on an instance of monster truck rally, will run validations on both the monster truck rally as well as any models specified by `also_validates`. If there are any validation errors on the composed model, they are copied up (to the "base") of the parent model (in this case a MonsterTruckRally).
|
61
|
+
|
62
|
+
Note: Currently does not pass the attribute information (the field of the composed model) along to its parent. Errors are added to "base".
|
63
|
+
|
64
|
+
Only supported option currently is `allow_nil: true`:
|
65
|
+
|
66
|
+
also_validates :beer, :belly, allow_nil: true
|
67
|
+
|
68
|
+
Which, as the option states, allows nil values and will not attempt to validate those models.
|
69
|
+
|
47
70
|
## Contributing
|
48
71
|
|
49
72
|
1. Fork it
|
data/also_validates.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/also_validates/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Noah Davis"]
|
6
6
|
gem.email = ["noahd1@yahoo.com"]
|
7
|
-
gem.description = %q{
|
8
|
-
gem.summary = %q{
|
7
|
+
gem.description = %q{An ActiveModel validator that validates associated models, copying any errors from composed models up to their parent.}
|
8
|
+
gem.summary = %q{An ActiveModel validator that validates associated models, copying any errors from composed models up to their parent.}
|
9
9
|
gem.homepage = ""
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: also_validates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,8 +27,8 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.0.0
|
30
|
-
description:
|
31
|
-
|
30
|
+
description: An ActiveModel validator that validates associated models, copying any
|
31
|
+
errors from composed models up to their parent.
|
32
32
|
email:
|
33
33
|
- noahd1@yahoo.com
|
34
34
|
executables: []
|
@@ -71,7 +71,8 @@ rubyforge_project:
|
|
71
71
|
rubygems_version: 1.8.23
|
72
72
|
signing_key:
|
73
73
|
specification_version: 3
|
74
|
-
summary:
|
74
|
+
summary: An ActiveModel validator that validates associated models, copying any errors
|
75
|
+
from composed models up to their parent.
|
75
76
|
test_files:
|
76
77
|
- spec/fixtures/object_fixtures.rb
|
77
78
|
- spec/lib/also_validates_spec.rb
|