validates_existence 0.5.2 → 0.5.3
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.
- data/README.markdown +12 -1
- data/lib/validates_existence.rb +95 -9
- metadata +19 -10
- data/lib/rails2/validator.rb +0 -40
- data/lib/rails3/validator.rb +0 -55
data/README.markdown
CHANGED
@@ -7,6 +7,11 @@ It also supports `:allow_nil => true/false` and `:polymorphic => true` associati
|
|
7
7
|
Version 0.4.0 also adds Rails 3 support (the appropriate version is used automatically).
|
8
8
|
Version 0.5.0 introduces a new option - `:both` and changes the default behaviour of error attaching.
|
9
9
|
|
10
|
+
#### Changes in 0.5.3
|
11
|
+
|
12
|
+
Cleaned up the code, the whole library is now in one file with separate modules for Rails 2 and Rails 3.
|
13
|
+
Also renamed `should_macros` folder to `shoulda`.
|
14
|
+
|
10
15
|
#### Changes in 0.5.0
|
11
16
|
|
12
17
|
In verions prior to 0.5.0 the error message was attached to the field which the validation was defined on.
|
@@ -14,6 +19,12 @@ For example if the validation was on `:relation_id` field, then the error was ac
|
|
14
19
|
The new default behaviour is attaching the error on both fields: `:relation` and `:relation_id` for convenience.
|
15
20
|
This functionality can be controlled through the `:both` option, which accepts true/false value and defaults to true.
|
16
21
|
|
22
|
+
#### TODO
|
23
|
+
|
24
|
+
+ replace T::U tests with RSpec
|
25
|
+
+ create RSpec Matcher
|
26
|
+
+ separate set of tests for Rails 3
|
27
|
+
|
17
28
|
### Installation
|
18
29
|
First install the gem:
|
19
30
|
sudo gem install validates_existence
|
@@ -57,7 +68,7 @@ First install the gem:
|
|
57
68
|
validates :person, :existence => { :allow_nil => true, :both => false }
|
58
69
|
|
59
70
|
# the old method is supported also
|
60
|
-
|
71
|
+
validates_existence_of :wizard
|
61
72
|
end
|
62
73
|
|
63
74
|
## I18N
|
data/lib/validates_existence.rb
CHANGED
@@ -1,11 +1,97 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Perfectline
|
2
|
+
module ValidatesExistence
|
3
|
+
|
4
|
+
module Rails2
|
5
|
+
def validates_existence_of(*attribute_names)
|
6
|
+
options = attribute_names.extract_options!.symbolize_keys
|
7
|
+
options[:message] ||= :existence
|
8
|
+
options[:both] = true unless options.has_key?(:both)
|
9
|
+
|
10
|
+
validates_each(attribute_names, options) do |record, attribute, value|
|
11
|
+
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
12
|
+
association = record.class.reflect_on_association(normalized)
|
13
|
+
|
14
|
+
if association.nil? or !association.belongs_to?
|
15
|
+
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
16
|
+
end
|
17
|
+
|
18
|
+
target_class = nil
|
19
|
+
|
20
|
+
# dealing with polymorphic belongs_to
|
21
|
+
if association.options.has_key?(:foreign_type)
|
22
|
+
foreign_type = record.send(association.options.fetch(:foreign_type))
|
23
|
+
target_class = foreign_type.constantize unless foreign_type.nil?
|
24
|
+
else
|
25
|
+
target_class = association.klass
|
26
|
+
end
|
27
|
+
|
28
|
+
if target_class.nil? or !target_class.exists?(value)
|
29
|
+
record.errors.add(attribute, options[:message], :default => "does not exist")
|
30
|
+
|
31
|
+
# add the error on both :relation and :relation_id
|
32
|
+
if options[:both]
|
33
|
+
normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
|
34
|
+
record.errors.add(normalized, options[:message], :default => "does not exist")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Rails3
|
42
|
+
extend ActiveSupport::Concern
|
43
|
+
|
44
|
+
class ExistenceValidator < ActiveModel::EachValidator
|
4
45
|
|
5
|
-
|
6
|
-
|
46
|
+
def initialize(options)
|
47
|
+
# set the default message if its unspecified
|
48
|
+
options[:message] ||= :existence
|
49
|
+
options[:both] ||= true
|
50
|
+
super(options)
|
51
|
+
end
|
52
|
+
|
53
|
+
def validate_each(record, attribute, value)
|
54
|
+
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
55
|
+
association = record.class.reflect_on_association(normalized)
|
56
|
+
|
57
|
+
if association.nil? or !association.belongs_to?
|
58
|
+
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
59
|
+
end
|
60
|
+
|
61
|
+
target_class = nil
|
62
|
+
|
63
|
+
# dealing with polymorphic belongs_to
|
64
|
+
if association.options.has_key?(:foreign_type)
|
65
|
+
foreign_type = record.send(association.options.fetch(:foreign_type))
|
66
|
+
target_class = foreign_type.constantize unless foreign_type.nil?
|
67
|
+
else
|
68
|
+
target_class = association.klass
|
69
|
+
end
|
70
|
+
|
71
|
+
if value.nil? or target_class.nil? or !target_class.exists?(value)
|
72
|
+
record.errors.add(attribute, options[:message], :default => "does not exist")
|
73
|
+
|
74
|
+
# add the error on both :relation and :relation_id
|
75
|
+
if options[:both]
|
76
|
+
normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
|
77
|
+
record.errors.add(normalized, options[:message], :default => "does not exist")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module ClassMethods
|
84
|
+
def validates_existence_of(*attr_names)
|
85
|
+
validates_with ExistenceValidator, _merge_attributes(attr_names)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
if Rails::VERSION::MAJOR >= 3
|
94
|
+
ActiveRecord::Base.send(:include, Perfectline::ValidatesExistence::Rails3)
|
7
95
|
else
|
8
|
-
|
9
|
-
|
10
|
-
ActiveRecord::Base.__send__(:extend, Perfectline::ValidatesExistence)
|
11
|
-
end
|
96
|
+
ActiveRecord::Base.send(:extend, Perfectline::ValidatesExistence::Rails2)
|
97
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 3
|
9
|
+
version: 0.5.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tanel Suurhans
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-09-05 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -32,10 +32,17 @@ extra_rdoc_files:
|
|
32
32
|
files:
|
33
33
|
- README.markdown
|
34
34
|
- install.rb
|
35
|
-
- lib/rails2/validator.rb
|
36
|
-
- lib/rails3/validator.rb
|
37
35
|
- lib/validates_existence.rb
|
38
36
|
- rails/init.rb
|
37
|
+
- test/models/user_with_poly.rb
|
38
|
+
- test/models/name.rb
|
39
|
+
- test/models/user_with_allow_nil.rb
|
40
|
+
- test/models/user_with_has_many.rb
|
41
|
+
- test/models/user_with_poly_allow_nil.rb
|
42
|
+
- test/models/user_with_both.rb
|
43
|
+
- test/models/user.rb
|
44
|
+
- test/validates_existence_test.rb
|
45
|
+
- test/test_helper.rb
|
39
46
|
has_rdoc: true
|
40
47
|
homepage: http://github.com/perfectline/validates_existence/tree/master
|
41
48
|
licenses: []
|
@@ -46,6 +53,7 @@ rdoc_options:
|
|
46
53
|
require_paths:
|
47
54
|
- lib
|
48
55
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
49
57
|
requirements:
|
50
58
|
- - ">="
|
51
59
|
- !ruby/object:Gem::Version
|
@@ -53,6 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
61
|
- 0
|
54
62
|
version: "0"
|
55
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
56
65
|
requirements:
|
57
66
|
- - ">="
|
58
67
|
- !ruby/object:Gem::Version
|
@@ -62,17 +71,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
71
|
requirements: []
|
63
72
|
|
64
73
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.3.
|
74
|
+
rubygems_version: 1.3.7
|
66
75
|
signing_key:
|
67
76
|
specification_version: 3
|
68
77
|
summary: Validates Rails model belongs_to association existence.
|
69
78
|
test_files:
|
79
|
+
- test/models/user_with_poly.rb
|
70
80
|
- test/models/name.rb
|
71
|
-
- test/models/user.rb
|
72
81
|
- test/models/user_with_allow_nil.rb
|
73
|
-
- test/models/user_with_both.rb
|
74
82
|
- test/models/user_with_has_many.rb
|
75
|
-
- test/models/user_with_poly.rb
|
76
83
|
- test/models/user_with_poly_allow_nil.rb
|
77
|
-
- test/
|
84
|
+
- test/models/user_with_both.rb
|
85
|
+
- test/models/user.rb
|
78
86
|
- test/validates_existence_test.rb
|
87
|
+
- test/test_helper.rb
|
data/lib/rails2/validator.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
module Perfectline
|
2
|
-
module ValidatesExistence
|
3
|
-
|
4
|
-
def validates_existence_of(*attribute_names)
|
5
|
-
options = attribute_names.extract_options!.symbolize_keys
|
6
|
-
options[:message] ||= :existence
|
7
|
-
options[:both] = true unless options.has_key?(:both)
|
8
|
-
|
9
|
-
validates_each(attribute_names, options) do |record, attribute, value|
|
10
|
-
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
11
|
-
association = record.class.reflect_on_association(normalized)
|
12
|
-
|
13
|
-
if association.nil? or !association.belongs_to?
|
14
|
-
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
15
|
-
end
|
16
|
-
|
17
|
-
target_class = nil
|
18
|
-
|
19
|
-
# dealing with polymorphic belongs_to
|
20
|
-
if association.options.has_key?(:foreign_type)
|
21
|
-
foreign_type = record.__send__(association.options.fetch(:foreign_type))
|
22
|
-
target_class = foreign_type.constantize unless foreign_type.nil?
|
23
|
-
else
|
24
|
-
target_class = association.klass
|
25
|
-
end
|
26
|
-
|
27
|
-
if target_class.nil? or !target_class.exists?(value)
|
28
|
-
record.errors.add(attribute, options[:message], :default => "does not exist")
|
29
|
-
|
30
|
-
# add the error on both :relation and :relation_id
|
31
|
-
if options[:both]
|
32
|
-
normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
|
33
|
-
record.errors.add(normalized, options[:message], :default => "does not exist")
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
data/lib/rails3/validator.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
module Perfectline
|
2
|
-
module ValidatesExistence
|
3
|
-
|
4
|
-
module InstanceMethods
|
5
|
-
class ExistenceValidator < ActiveModel::EachValidator
|
6
|
-
|
7
|
-
def initialize(options)
|
8
|
-
# set the default message if its unspecified
|
9
|
-
options[:message] ||= :existence
|
10
|
-
options[:both] ||= true
|
11
|
-
super(options)
|
12
|
-
end
|
13
|
-
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
16
|
-
association = record.class.reflect_on_association(normalized)
|
17
|
-
|
18
|
-
if association.nil? or !association.belongs_to?
|
19
|
-
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
20
|
-
end
|
21
|
-
|
22
|
-
target_class = nil
|
23
|
-
|
24
|
-
# dealing with polymorphic belongs_to
|
25
|
-
if association.options.has_key?(:foreign_type)
|
26
|
-
foreign_type = record.__send__(association.options.fetch(:foreign_type))
|
27
|
-
target_class = foreign_type.constantize unless foreign_type.nil?
|
28
|
-
else
|
29
|
-
target_class = association.klass
|
30
|
-
end
|
31
|
-
|
32
|
-
if target_class.nil? or !target_class.exists?(value)
|
33
|
-
record.errors.add(attribute, options[:message], :default => "does not exist")
|
34
|
-
|
35
|
-
# add the error on both :relation and :relation_id
|
36
|
-
if options[:both]
|
37
|
-
normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
|
38
|
-
record.errors.add(normalized, options[:message], :default => "does not exist")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
module ClassMethods
|
47
|
-
|
48
|
-
def validates_existence_of(*attr_names)
|
49
|
-
validates_with ExistenceValidator, _merge_attributes(attr_names)
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
end
|