grouped_validations 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +14 -0
- data/lib/grouped_validations/version.rb +1 -1
- data/lib/grouped_validations.rb +19 -15
- data/spec/grouped_validations_spec.rb +37 -2
- metadata +17 -5
data/README.rdoc
CHANGED
@@ -48,6 +48,20 @@ You can also check validation for multiple groups
|
|
48
48
|
|
49
49
|
p.groups_valid?(:group1, :group2)
|
50
50
|
|
51
|
+
|
52
|
+
To define validation blocks just use the respective group validation method, like so
|
53
|
+
|
54
|
+
class Person < ActiveRecord::Base
|
55
|
+
validation_group :name do
|
56
|
+
validates_presence_of :first_name
|
57
|
+
validates_presence_of :last_name
|
58
|
+
end
|
59
|
+
|
60
|
+
validate_name {|r| # something custom on save }
|
61
|
+
validate_name_on_create {|r| # something custom on create }
|
62
|
+
validate_name_on_update {|r| # something custom on update }
|
63
|
+
end
|
64
|
+
|
51
65
|
== Credits
|
52
66
|
|
53
67
|
* Adam Meehan (http://github.com/adzap)
|
data/lib/grouped_validations.rb
CHANGED
@@ -5,6 +5,10 @@ module GroupedValidations
|
|
5
5
|
def validation_group(group, &block)
|
6
6
|
raise "The validation_group method requires a block" unless block_given?
|
7
7
|
|
8
|
+
unless self.include?(GroupedValidations::InstanceMethods)
|
9
|
+
include GroupedValidations::InstanceMethods
|
10
|
+
end
|
11
|
+
|
8
12
|
self.validation_groups ||= []
|
9
13
|
self.validation_groups << group
|
10
14
|
|
@@ -18,6 +22,18 @@ module GroupedValidations
|
|
18
22
|
end
|
19
23
|
|
20
24
|
module InstanceMethods
|
25
|
+
def self.included(base)
|
26
|
+
base.alias_method_chain :valid?, :groups
|
27
|
+
base.class_eval do
|
28
|
+
class << self
|
29
|
+
if ActiveRecord::VERSION::MAJOR < 3
|
30
|
+
alias_method_chain :validation_method, :groups
|
31
|
+
else
|
32
|
+
alias_method_chain :validate, :groups
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
21
37
|
|
22
38
|
def group_valid?(group)
|
23
39
|
raise "Validation group '#{group}' not defined" unless validation_groups.include?(group)
|
@@ -50,23 +66,11 @@ end
|
|
50
66
|
|
51
67
|
ActiveRecord::Base.class_eval do
|
52
68
|
extend GroupedValidations::ClassMethods
|
53
|
-
include GroupedValidations::InstanceMethods
|
54
69
|
class_inheritable_accessor :validation_groups
|
55
|
-
alias_method_chain :valid?, :groups
|
56
70
|
end
|
57
71
|
|
58
|
-
if ActiveRecord::VERSION::MAJOR
|
59
|
-
require 'grouped_validations/active_model'
|
60
|
-
ActiveRecord::Base.class_eval do
|
61
|
-
class << self
|
62
|
-
alias_method_chain :validate, :groups
|
63
|
-
end
|
64
|
-
end
|
65
|
-
else
|
72
|
+
if ActiveRecord::VERSION::MAJOR < 3
|
66
73
|
require 'grouped_validations/active_record'
|
67
|
-
|
68
|
-
|
69
|
-
alias_method_chain :validation_method, :groups
|
70
|
-
end
|
71
|
-
end
|
74
|
+
else
|
75
|
+
require 'grouped_validations/active_model'
|
72
76
|
end
|
@@ -9,6 +9,10 @@ describe GroupedValidations do
|
|
9
9
|
Person.should respond_to(:validation_group)
|
10
10
|
end
|
11
11
|
|
12
|
+
it "should not include instance methods by default" do
|
13
|
+
ActiveRecord::Base.include?(GroupedValidations::InstanceMethods).should be_false
|
14
|
+
end
|
15
|
+
|
12
16
|
it "should store defined validation group names" do
|
13
17
|
Person.validation_group(:dummy) { }
|
14
18
|
Person.validation_groups.should == [:dummy]
|
@@ -73,10 +77,9 @@ describe GroupedValidations do
|
|
73
77
|
p.should have(3).errors
|
74
78
|
end
|
75
79
|
|
76
|
-
it "should respect :on validation option" do
|
80
|
+
it "should respect :on => :create validation option" do
|
77
81
|
Person.validation_group :name do
|
78
82
|
validates_presence_of :first_name, :on => :create
|
79
|
-
validates_presence_of :last_name, :on => :update
|
80
83
|
end
|
81
84
|
|
82
85
|
p = Person.new
|
@@ -86,6 +89,22 @@ describe GroupedValidations do
|
|
86
89
|
p.group_valid?(:name)
|
87
90
|
p.should have(0).errors
|
88
91
|
|
92
|
+
p.save.should be_true
|
93
|
+
p.first_name = nil
|
94
|
+
p.group_valid?(:name)
|
95
|
+
p.should have(0).errors
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should respect :on => :update validation option" do
|
99
|
+
Person.validation_group :name do
|
100
|
+
validates_presence_of :last_name, :on => :update
|
101
|
+
end
|
102
|
+
|
103
|
+
p = Person.new
|
104
|
+
p.last_name = nil
|
105
|
+
p.group_valid?(:name)
|
106
|
+
p.should have(0).errors
|
107
|
+
|
89
108
|
p.save.should be_true
|
90
109
|
p.group_valid?(:name)
|
91
110
|
p.should have(1).errors
|
@@ -93,4 +112,20 @@ describe GroupedValidations do
|
|
93
112
|
p.group_valid?(:name)
|
94
113
|
p.should have(0).errors
|
95
114
|
end
|
115
|
+
|
116
|
+
it "should allow a validation group to appended with subsequent blocks" do
|
117
|
+
Person.class_eval do
|
118
|
+
validation_group :name do
|
119
|
+
validates_presence_of :first_name
|
120
|
+
end
|
121
|
+
validation_group :name do
|
122
|
+
validates_presence_of :last_name
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
p = Person.new
|
127
|
+
p.group_valid?(:name)
|
128
|
+
p.should have(2).errors
|
129
|
+
end
|
130
|
+
|
96
131
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grouped_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Adam Meehan
|
@@ -9,7 +15,7 @@ autorequire: grouped_validations
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-06-08 00:00:00 +10:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -41,21 +47,27 @@ rdoc_options: []
|
|
41
47
|
require_paths:
|
42
48
|
- lib
|
43
49
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
44
51
|
requirements:
|
45
52
|
- - ">="
|
46
53
|
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
47
57
|
version: "0"
|
48
|
-
version:
|
49
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
50
60
|
requirements:
|
51
61
|
- - ">="
|
52
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
53
66
|
version: "0"
|
54
|
-
version:
|
55
67
|
requirements: []
|
56
68
|
|
57
69
|
rubyforge_project: grouped_validations
|
58
|
-
rubygems_version: 1.3.
|
70
|
+
rubygems_version: 1.3.7
|
59
71
|
signing_key:
|
60
72
|
specification_version: 3
|
61
73
|
summary: Define validation groups in ActiveRecord for greater control over which validations to run.
|