validation_reflection 0.3.8 → 1.0.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README +3 -3
- data/VERSION.yml +4 -4
- data/lib/validation_reflection.rb +82 -104
- data/test/validation_reflection_test.rb +4 -4
- data/validation_reflection.gemspec +5 -5
- metadata +17 -9
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Validation Reflection
|
2
2
|
=====================
|
3
3
|
|
4
|
-
Version 0.
|
4
|
+
Version 1.0.0-beta4, 2010-06-12
|
5
5
|
|
6
6
|
This plugin adds reflective access to validations
|
7
7
|
|
@@ -31,9 +31,9 @@ ActiveRecord::Reflection::MacroReflection. For example
|
|
31
31
|
|
32
32
|
Usually, all the standard Rails validations are reflected.
|
33
33
|
You can change this -- add or remove validations -- in an
|
34
|
-
application-specific
|
34
|
+
application-specific initializer,
|
35
35
|
|
36
|
-
config/
|
36
|
+
config/initializers/validation_reflection.rb
|
37
37
|
|
38
38
|
In that file change config.reflected_validations to suit your
|
39
39
|
needs. Say, you have a custom validation for email addresses,
|
data/VERSION.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
:major:
|
3
|
-
:minor:
|
4
|
-
:build:
|
5
|
-
:patch:
|
2
|
+
:major: 1
|
3
|
+
:minor: 0
|
4
|
+
:build: "beta4"
|
5
|
+
:patch: 0
|
@@ -1,126 +1,104 @@
|
|
1
|
-
require 'active_record/reflection'
|
2
1
|
require 'ostruct'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
mattr_accessor :reflected_validations,
|
33
|
-
:in_ignored_subvalidation
|
34
|
-
|
35
|
-
def included(base) # :nodoc:
|
36
|
-
return if base.kind_of?(::ActiveRecordExtensions::ValidationReflection::ClassMethods)
|
37
|
-
base.extend(ClassMethods)
|
38
|
-
end
|
3
|
+
module ValidationReflection # :nodoc:
|
4
|
+
|
5
|
+
extend self
|
6
|
+
|
7
|
+
CORE_VALIDATONS = [
|
8
|
+
:validates_acceptance_of,
|
9
|
+
:validates_associated,
|
10
|
+
:validates_confirmation_of,
|
11
|
+
:validates_exclusion_of,
|
12
|
+
:validates_format_of,
|
13
|
+
:validates_inclusion_of,
|
14
|
+
:validates_length_of,
|
15
|
+
:validates_numericality_of,
|
16
|
+
:validates_presence_of,
|
17
|
+
:validates_uniqueness_of,
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
@@reflected_validations = CORE_VALIDATONS.dup
|
21
|
+
|
22
|
+
@@in_ignored_subvalidation = false
|
23
|
+
|
24
|
+
mattr_accessor :reflected_validations,
|
25
|
+
:in_ignored_subvalidation
|
26
|
+
|
27
|
+
def included(base) # :nodoc:
|
28
|
+
return if base.kind_of?(::ValidationReflection::ClassMethods)
|
29
|
+
base.extend(ClassMethods)
|
30
|
+
end
|
39
31
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
32
|
+
# Iterate through all validations and store/cache the info
|
33
|
+
# for later easy access.
|
34
|
+
#
|
35
|
+
def install(base)
|
36
|
+
@@reflected_validations.each do |validation_type|
|
37
|
+
next if base.respond_to?(:"#{validation_type}_with_reflection")
|
38
|
+
ignore_subvalidations = false
|
39
|
+
|
40
|
+
if validation_type.kind_of?(::Hash)
|
41
|
+
ignore_subvalidations = validation_type[:ignore_subvalidations]
|
42
|
+
validation_type = validation_type[:method]
|
50
43
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
next if base.respond_to?(:"#{validation_type}_with_reflection")
|
59
|
-
ignore_subvalidations = false
|
60
|
-
|
61
|
-
if validation_type.kind_of?(::Hash)
|
62
|
-
ignore_subvalidations = validation_type[:ignore_subvalidations]
|
63
|
-
validation_type = validation_type[:method]
|
64
|
-
end
|
65
|
-
|
66
|
-
base.class_eval %{
|
67
|
-
class << self
|
68
|
-
def #{validation_type}_with_reflection(*attr_names)
|
69
|
-
ignoring_subvalidations(#{ignore_subvalidations}) do
|
70
|
-
#{validation_type}_without_reflection(*attr_names)
|
71
|
-
remember_validation_metadata(:#{validation_type}, *attr_names)
|
72
|
-
end
|
44
|
+
|
45
|
+
base.class_eval %{
|
46
|
+
class << self
|
47
|
+
def #{validation_type}_with_reflection(*attr_names)
|
48
|
+
ignoring_subvalidations(#{ignore_subvalidations}) do
|
49
|
+
#{validation_type}_without_reflection(*attr_names)
|
50
|
+
remember_validation_metadata(:#{validation_type}, *attr_names)
|
73
51
|
end
|
74
|
-
alias_method_chain :#{validation_type}, :reflection
|
75
52
|
end
|
76
|
-
|
77
|
-
|
53
|
+
alias_method_chain :#{validation_type}, :reflection
|
54
|
+
end
|
55
|
+
}, __FILE__, __LINE__
|
78
56
|
end
|
57
|
+
end
|
58
|
+
alias :reload :install
|
79
59
|
|
80
|
-
|
60
|
+
module ClassMethods
|
81
61
|
|
82
|
-
|
62
|
+
include ::ValidationReflection
|
83
63
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
64
|
+
# Returns an array of MacroReflection objects for all validations in the class
|
65
|
+
def reflect_on_all_validations
|
66
|
+
self.read_inheritable_attribute(:reflected_validations) || []
|
67
|
+
end
|
88
68
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
end
|
69
|
+
# Returns an array of MacroReflection objects for all validations defined for the field +attr_name+.
|
70
|
+
def reflect_on_validations_for(attr_name)
|
71
|
+
self.reflect_on_all_validations.select do |reflection|
|
72
|
+
reflection.name == attr_name.to_sym
|
94
73
|
end
|
74
|
+
end
|
95
75
|
|
96
|
-
|
76
|
+
private
|
97
77
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
78
|
+
# Store validation info for easy and fast access.
|
79
|
+
#
|
80
|
+
def remember_validation_metadata(validation_type, *attr_names)
|
81
|
+
configuration = attr_names.last.is_a?(::Hash) ? attr_names.pop : {}
|
82
|
+
attr_names.flatten.each do |attr_name|
|
83
|
+
self.write_inheritable_array :reflected_validations,
|
84
|
+
[::ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)]
|
106
85
|
end
|
86
|
+
end
|
107
87
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
ensure
|
115
|
-
self.in_ignored_subvalidation = save_ignore
|
88
|
+
def ignoring_subvalidations(ignore)
|
89
|
+
save_ignore = self.in_ignored_subvalidation
|
90
|
+
unless self.in_ignored_subvalidation
|
91
|
+
self.in_ignored_subvalidation = ignore
|
92
|
+
yield
|
116
93
|
end
|
94
|
+
ensure
|
95
|
+
self.in_ignored_subvalidation = save_ignore
|
96
|
+
end
|
117
97
|
|
118
|
-
end
|
119
98
|
end
|
120
99
|
end
|
121
100
|
|
122
|
-
|
123
|
-
include
|
124
|
-
::
|
125
|
-
::ActiveRecordExtensions::ValidationReflection.install(self)
|
101
|
+
ActiveSupport.on_load(:active_record) do
|
102
|
+
include ValidationReflection
|
103
|
+
::ValidationReflection.install(self)
|
126
104
|
end
|
@@ -17,13 +17,13 @@ end
|
|
17
17
|
require 'validation_reflection'
|
18
18
|
|
19
19
|
ActiveRecord::Base.class_eval do
|
20
|
-
include
|
21
|
-
::
|
22
|
-
::
|
20
|
+
include ValidationReflection
|
21
|
+
::ValidationReflection.reflected_validations << :validates_something_weird
|
22
|
+
::ValidationReflection.reflected_validations << {
|
23
23
|
:method => :validates_something_selfcontained,
|
24
24
|
:ignore_subvalidations => true
|
25
25
|
}
|
26
|
-
::
|
26
|
+
::ValidationReflection.install(self)
|
27
27
|
end
|
28
28
|
|
29
29
|
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{validation_reflection}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0.beta4"
|
9
9
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Christopher Redinger"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-06-12}
|
13
13
|
s.description = %q{Adds reflective access to validations}
|
14
14
|
s.email = %q{redinger@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.homepage = %q{http://github.com/redinger/validation_reflection}
|
32
32
|
s.rdoc_options = ["--charset=UTF-8"]
|
33
33
|
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.3.
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
35
|
s.summary = %q{Adds reflective access to validations}
|
36
36
|
s.test_files = [
|
37
37
|
"test/test_helper.rb",
|
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
|
|
42
42
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
43
|
s.specification_version = 3
|
44
44
|
|
45
|
-
if Gem::Version.new(Gem::
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
46
|
else
|
47
47
|
end
|
48
48
|
else
|
metadata
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation_reflection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: -1512032865
|
5
|
+
prerelease: true
|
5
6
|
segments:
|
7
|
+
- 1
|
6
8
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
9
|
+
- 0
|
10
|
+
- beta4
|
11
|
+
version: 1.0.0.beta4
|
10
12
|
platform: ruby
|
11
13
|
authors:
|
12
14
|
- Christopher Redinger
|
@@ -14,7 +16,7 @@ autorequire:
|
|
14
16
|
bindir: bin
|
15
17
|
cert_chain: []
|
16
18
|
|
17
|
-
date: 2010-
|
19
|
+
date: 2010-06-12 00:00:00 -04:00
|
18
20
|
default_executable:
|
19
21
|
dependencies: []
|
20
22
|
|
@@ -48,23 +50,29 @@ rdoc_options:
|
|
48
50
|
require_paths:
|
49
51
|
- lib
|
50
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
51
54
|
requirements:
|
52
55
|
- - ">="
|
53
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
54
58
|
segments:
|
55
59
|
- 0
|
56
60
|
version: "0"
|
57
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
58
63
|
requirements:
|
59
|
-
- - "
|
64
|
+
- - ">"
|
60
65
|
- !ruby/object:Gem::Version
|
66
|
+
hash: 25
|
61
67
|
segments:
|
62
|
-
-
|
63
|
-
|
68
|
+
- 1
|
69
|
+
- 3
|
70
|
+
- 1
|
71
|
+
version: 1.3.1
|
64
72
|
requirements: []
|
65
73
|
|
66
74
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.3.
|
75
|
+
rubygems_version: 1.3.7
|
68
76
|
signing_key:
|
69
77
|
specification_version: 3
|
70
78
|
summary: Adds reflective access to validations
|