grouped_validations 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -2
- data/Gemfile.lock +13 -9
- data/lib/grouped_validations.rb +42 -45
- data/lib/grouped_validations/version.rb +1 -1
- data/spec/grouped_validations_spec.rb +3 -2
- metadata +4 -4
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.
|
5
|
-
activesupport (= 3.
|
6
|
-
builder (~>
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
activemodel (3.2.2)
|
5
|
+
activesupport (= 3.2.2)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activesupport (3.2.2)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
builder (3.0.0)
|
10
11
|
columnize (0.3.1)
|
11
12
|
diff-lcs (1.1.2)
|
12
|
-
i18n (0.
|
13
|
+
i18n (0.6.0)
|
13
14
|
linecache (0.43)
|
15
|
+
multi_json (1.1.0)
|
16
|
+
rake (0.8.7)
|
14
17
|
rspec (2.0.1)
|
15
18
|
rspec-core (~> 2.0.1)
|
16
19
|
rspec-expectations (~> 2.0.1)
|
@@ -31,7 +34,8 @@ PLATFORMS
|
|
31
34
|
ruby
|
32
35
|
|
33
36
|
DEPENDENCIES
|
34
|
-
activemodel (~> 3.
|
35
|
-
activesupport (~> 3.
|
37
|
+
activemodel (~> 3.2.0)
|
38
|
+
activesupport (~> 3.2.0)
|
39
|
+
rake (~> 0.8.7)
|
36
40
|
rspec (~> 2.0.0)
|
37
41
|
ruby-debug
|
data/lib/grouped_validations.rb
CHANGED
@@ -34,60 +34,57 @@ module GroupedValidations
|
|
34
34
|
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
validation_groups.each do |group|
|
42
|
-
_run_group_validation_callbacks(group, context)
|
43
|
-
end
|
44
|
-
errors.empty?
|
37
|
+
def valid?(context=nil)
|
38
|
+
super
|
39
|
+
validation_groups.each do |group|
|
40
|
+
_run_group_validation_callbacks(group, context)
|
45
41
|
end
|
42
|
+
errors.empty?
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
errors.empty?
|
55
|
-
end
|
56
|
-
alias_method :group_valid?, :groups_valid?
|
57
|
-
|
58
|
-
def grouped_errors(context=nil)
|
59
|
-
original_errors = @errors.dup if @errors
|
60
|
-
@errors = nil
|
61
|
-
grouped = {}
|
62
|
-
|
63
|
-
with_validation_context(context) do
|
64
|
-
_run_validate_callbacks
|
65
|
-
grouped[nil] = @errors
|
66
|
-
|
67
|
-
validation_groups.each do |group|
|
68
|
-
@errors = nil
|
69
|
-
send(:"_run_validate_#{group}_callbacks")
|
70
|
-
grouped[group] = @errors
|
71
|
-
end
|
72
|
-
end
|
73
|
-
grouped.values.all?(&:empty?) ? {} : grouped
|
74
|
-
ensure
|
75
|
-
@errors = original_errors
|
45
|
+
def groups_valid?(*groups)
|
46
|
+
options = groups.extract_options!
|
47
|
+
errors.clear
|
48
|
+
groups.each do |group|
|
49
|
+
raise "Validation group '#{group}' not defined" unless validation_groups.include?(group)
|
50
|
+
_run_group_validation_callbacks(group, options[:context])
|
76
51
|
end
|
52
|
+
errors.empty?
|
53
|
+
end
|
54
|
+
alias_method :group_valid?, :groups_valid?
|
55
|
+
|
56
|
+
def grouped_errors(context=nil)
|
57
|
+
original_errors = @errors.dup if @errors
|
58
|
+
@errors = nil
|
59
|
+
grouped = {}
|
77
60
|
|
78
|
-
|
79
|
-
|
61
|
+
with_validation_context(context) do
|
62
|
+
_run_validate_callbacks
|
63
|
+
grouped[nil] = @errors
|
64
|
+
|
65
|
+
validation_groups.each do |group|
|
66
|
+
@errors = nil
|
80
67
|
send(:"_run_validate_#{group}_callbacks")
|
68
|
+
grouped[group] = @errors
|
81
69
|
end
|
82
70
|
end
|
71
|
+
grouped.values.all?(&:empty?) ? {} : grouped
|
72
|
+
ensure
|
73
|
+
@errors = original_errors
|
74
|
+
end
|
83
75
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
yield
|
88
|
-
ensure
|
89
|
-
self.validation_context = current_context
|
76
|
+
def _run_group_validation_callbacks(group, context=nil)
|
77
|
+
with_validation_context(context) do
|
78
|
+
send(:"_run_validate_#{group}_callbacks")
|
90
79
|
end
|
80
|
+
end
|
91
81
|
|
82
|
+
def with_validation_context(context)
|
83
|
+
context ||= (persisted? ? :update : :create)
|
84
|
+
current_context, self.validation_context = validation_context, context
|
85
|
+
yield
|
86
|
+
ensure
|
87
|
+
self.validation_context = current_context
|
92
88
|
end
|
89
|
+
|
93
90
|
end
|
@@ -213,8 +213,9 @@ describe GroupedValidations do
|
|
213
213
|
|
214
214
|
it 'should return hash of error hashes with validation groups as keys' do
|
215
215
|
errors = person.grouped_errors
|
216
|
-
|
217
|
-
errors[:
|
216
|
+
|
217
|
+
errors[:first_name_group][:first_name].should eq ["can't be blank"]
|
218
|
+
errors[:last_name_group][:last_name].should eq ["can't be blank"]
|
218
219
|
end
|
219
220
|
|
220
221
|
it 'should return hash of errors for validations outside a validation group, for nil key' do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grouped_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
@@ -15,7 +15,7 @@ autorequire: grouped_validations
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-05-03 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|