validation_scopes 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/validation_scopes.rb +40 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e189d48da303b09816e41dd0340f1bdb869d68a
|
4
|
+
data.tar.gz: cb046e7bf929b6211e062eed7e0be384e1ba67ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37d48cd86d626b98b2e7a6b0214e815ef500b11bf1eb46e4c5f6239ba50cd315a4027539f42db3ca80b573fe66b1dd49d235ac7faaa7884cf22a8400742989bb
|
7
|
+
data.tar.gz: bef028153a6bdc041f2faa683a4afe1e5852d2ede8d62f64e363db31ef833d1f02e45c8a38d2366b67cf996c5e4a03d1d407c20046eff33e11345801769d7890
|
data/CHANGELOG.md
CHANGED
data/lib/validation_scopes.rb
CHANGED
@@ -6,23 +6,41 @@ module ValidationScopes
|
|
6
6
|
base.extend ClassMethods
|
7
7
|
end
|
8
8
|
|
9
|
+
# Based on AssociatedValidator from ActiveRecord, see:
|
10
|
+
# activerecord-4.2.0/lib/active_record/validations/associated.rb @ line 3
|
11
|
+
class AssociatedValidator < ActiveModel::EachValidator
|
12
|
+
def validate_each(record, attribute, value)
|
13
|
+
all_valid = Array.wrap(value).all? do |r|
|
14
|
+
r.marked_for_destruction? || r.send("no_#{options[:scope]}?")
|
15
|
+
end
|
16
|
+
unless all_valid
|
17
|
+
record.errors.add(attribute, 'is invalid')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
9
22
|
module ClassMethods
|
10
|
-
def
|
11
|
-
@
|
12
|
-
|
23
|
+
def validation_scopes
|
24
|
+
@validation_scopes ||= []
|
25
|
+
end
|
13
26
|
|
14
|
-
|
15
|
-
|
16
|
-
|
27
|
+
def validation_proxies
|
28
|
+
@validation_proxies ||= {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def validation_scope(scope)
|
32
|
+
validation_scopes << scope
|
17
33
|
|
18
34
|
base_class = self
|
19
35
|
|
20
|
-
|
36
|
+
superclass = self.superclass.validation_proxies[scope] || DelegateClass(base_class)
|
37
|
+
proxy_class = Class.new(superclass) do
|
21
38
|
include ActiveModel::Validations
|
22
39
|
|
23
|
-
|
24
|
-
|
25
|
-
|
40
|
+
@scope = scope
|
41
|
+
def self.validates_associated(*attr_names)
|
42
|
+
validates_with AssociatedValidator,
|
43
|
+
_merge_attributes(attr_names).reverse_merge(scope: @scope)
|
26
44
|
end
|
27
45
|
|
28
46
|
# Hacks to support dynamic_model helpers
|
@@ -30,14 +48,22 @@ module ValidationScopes
|
|
30
48
|
self
|
31
49
|
end
|
32
50
|
|
33
|
-
# Rails 3 default implementation of model_name blows up for anonymous classes
|
34
51
|
class << self; self; end.class_eval do
|
52
|
+
# Rails 3 default implementation of model_name blows up for anonymous
|
53
|
+
# classes
|
35
54
|
define_method(:model_name) do
|
36
55
|
base_class.model_name
|
37
56
|
end
|
57
|
+
# Before Rails 4.1 callback functions were created using the class
|
58
|
+
# name, so we must name our anonymous classes.
|
59
|
+
define_method(:name) do
|
60
|
+
"#{base_class.name}#{scope.to_s.classify}ValidationProxy"
|
61
|
+
end
|
38
62
|
end
|
39
63
|
end
|
40
64
|
|
65
|
+
validation_proxies[scope] = proxy_class
|
66
|
+
|
41
67
|
yield proxy_class
|
42
68
|
|
43
69
|
define_method(scope) do
|
@@ -53,14 +79,14 @@ module ValidationScopes
|
|
53
79
|
end
|
54
80
|
|
55
81
|
define_method("init_validation_scope_for_#{scope}") do
|
56
|
-
unless instance_variable_defined?("
|
57
|
-
instance_variable_set("
|
82
|
+
unless instance_variable_defined?("@validation_#{scope}")
|
83
|
+
instance_variable_set("@validation_#{scope}", proxy_class.new(self))
|
58
84
|
end
|
59
85
|
end
|
60
86
|
|
61
87
|
define_method("validation_scope_proxy_for_#{scope}") do
|
62
88
|
send "init_validation_scope_for_#{scope}"
|
63
|
-
instance_variable_get("
|
89
|
+
instance_variable_get("@validation_#{scope}")
|
64
90
|
end
|
65
91
|
end
|
66
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabe da Silveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|