validates_existence 0.6.2 → 0.7.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.
- data/lib/rails3.rb +12 -5
- metadata +2 -2
data/lib/rails3.rb
CHANGED
@@ -20,27 +20,34 @@ module Perfectline
|
|
20
20
|
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
21
21
|
association = record.class.reflect_on_association(normalized)
|
22
22
|
|
23
|
-
if association.nil?
|
23
|
+
if association.nil? || !association.belongs_to?
|
24
24
|
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
25
25
|
end
|
26
26
|
|
27
27
|
target_class = nil
|
28
28
|
|
29
29
|
# dealing with polymorphic belongs_to
|
30
|
-
if association.options
|
31
|
-
foreign_type = record.send(association.options
|
30
|
+
if association.options[:polymorphic]
|
31
|
+
foreign_type = record.send(association.options[:foreign_type] || association.foreign_type)
|
32
32
|
target_class = foreign_type.constantize unless foreign_type.nil?
|
33
33
|
else
|
34
34
|
target_class = association.klass
|
35
35
|
end
|
36
36
|
|
37
|
-
if value.nil?
|
37
|
+
if value.nil? || target_class.nil? || !target_class.exists?(value)
|
38
38
|
errors = [attribute]
|
39
39
|
|
40
40
|
# add the error on both :relation and :relation_id
|
41
41
|
if options[:both]
|
42
|
-
|
42
|
+
if Rails::VERSION::MINOR >= 1
|
43
|
+
foreign_key = association.foreign_key
|
44
|
+
else
|
45
|
+
foreign_key = association.primary_key_name
|
46
|
+
end
|
47
|
+
|
48
|
+
errors << (attribute.to_s.ends_with?("_id") ? normalized : foreign_key)
|
43
49
|
end
|
50
|
+
|
44
51
|
errors.each do |error|
|
45
52
|
record.errors.add(error, options[:message], :message => "does not exist")
|
46
53
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: validates_existence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.7.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tanel Suurhans
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-06-15 00:00:00 Z
|
15
15
|
dependencies: []
|
16
16
|
|
17
17
|
description: A library for validating model association existence.
|