validates_existence 0.8.0 → 0.9.2
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/validates_existence.rb +85 -6
- metadata +21 -7
- data/lib/rails2.rb +0 -52
- data/lib/rails3.rb +0 -74
data/lib/validates_existence.rb
CHANGED
@@ -1,7 +1,86 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module Perfectline
|
4
|
+
module ValidatesExistence
|
5
|
+
module Validator
|
6
|
+
class ExistenceValidator < ActiveModel::EachValidator
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
# set the default message if its unspecified
|
10
|
+
options[:message] ||= :existence
|
11
|
+
options[:both] = true unless options.key?(:both)
|
12
|
+
options[:allow_new] = false unless options.key?(:allow_new)
|
13
|
+
super(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(record)
|
17
|
+
attributes.each do |attribute|
|
18
|
+
value = record.read_attribute_for_validation(attribute)
|
19
|
+
target_id = if !attribute.match(/_id$/) && options[:allow_nil]
|
20
|
+
association = record.class.reflect_on_association(attribute)
|
21
|
+
association.respond_to?(:foreign_key) ? record[association.foreign_key] : record[association.association_foreign_key]
|
22
|
+
else
|
23
|
+
value
|
24
|
+
end
|
25
|
+
next if (target_id.nil? && value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
|
26
|
+
validate_each(record, attribute, value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_each(record, attribute, value)
|
31
|
+
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
32
|
+
association = record.class.reflect_on_association(normalized)
|
33
|
+
|
34
|
+
if association.nil? || !association.belongs_to?
|
35
|
+
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
36
|
+
end
|
37
|
+
|
38
|
+
target_class = nil
|
39
|
+
|
40
|
+
# dealing with polymorphic belongs_to
|
41
|
+
if association.options[:polymorphic]
|
42
|
+
foreign_type = record.send(association.options[:foreign_type] || association.foreign_type)
|
43
|
+
target_class = foreign_type.constantize unless foreign_type.nil?
|
44
|
+
else
|
45
|
+
target_class = association.klass
|
46
|
+
end
|
47
|
+
|
48
|
+
if value.nil? || target_class.nil? || !exists?(target_class, value)
|
49
|
+
errors = [attribute]
|
50
|
+
|
51
|
+
# add the error on both :relation and :relation_id
|
52
|
+
if options[:both]
|
53
|
+
if ActiveRecord::VERSION::MAJOR > 3 || (ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR >= 1)
|
54
|
+
foreign_key = association.foreign_key
|
55
|
+
else
|
56
|
+
foreign_key = association.primary_key_name
|
57
|
+
end
|
58
|
+
|
59
|
+
errors << (attribute.to_s.ends_with?("_id") ? normalized : foreign_key)
|
60
|
+
end
|
61
|
+
|
62
|
+
messages = [:"#{record.class.i18n_scope}.errors.messages.existence", "does not exist"]
|
63
|
+
|
64
|
+
errors.each do |error|
|
65
|
+
record.errors.add(error, options[:message], :message => messages)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def exists?(target_class, value)
|
71
|
+
(options[:allow_new] && value.new_record?) || target_class.exists?(value)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
module ClassMethods
|
77
|
+
def validates_existence_of(*attr_names)
|
78
|
+
validates_with ActiveRecord::Base::ExistenceValidator, _merge_attributes(attr_names)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
7
83
|
end
|
84
|
+
|
85
|
+
ActiveRecord::Base.send(:include, Perfectline::ValidatesExistence::Validator)
|
86
|
+
ActiveRecord::Base.send(:extend, Perfectline::ValidatesExistence::ClassMethods)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_existence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,8 +10,24 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
14
|
-
dependencies:
|
13
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
15
31
|
description: A library for validating model association existence.
|
16
32
|
email:
|
17
33
|
- tanel.suurhans@perfectline.ee
|
@@ -23,8 +39,6 @@ extra_rdoc_files:
|
|
23
39
|
files:
|
24
40
|
- README.markdown
|
25
41
|
- install.rb
|
26
|
-
- lib/rails2.rb
|
27
|
-
- lib/rails3.rb
|
28
42
|
- lib/validates_existence.rb
|
29
43
|
- rails/init.rb
|
30
44
|
homepage: http://github.com/perfectline/validates_existence/tree/master
|
@@ -47,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
61
|
version: '0'
|
48
62
|
requirements: []
|
49
63
|
rubyforge_project:
|
50
|
-
rubygems_version: 1.8.
|
64
|
+
rubygems_version: 1.8.25
|
51
65
|
signing_key:
|
52
|
-
specification_version:
|
66
|
+
specification_version: 4
|
53
67
|
summary: Validates Rails model belongs_to association existence.
|
54
68
|
test_files: []
|
data/lib/rails2.rb
DELETED
@@ -1,52 +0,0 @@
|
|
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.key?(:both)
|
9
|
-
options[:allow_new] = false unless options.key?(:allow_new)
|
10
|
-
|
11
|
-
validates_each(attribute_names, options) do |record, attribute, value|
|
12
|
-
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
13
|
-
association = record.class.reflect_on_association(normalized)
|
14
|
-
|
15
|
-
if association.nil? or !association.belongs_to?
|
16
|
-
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
17
|
-
end
|
18
|
-
|
19
|
-
target_class = nil
|
20
|
-
|
21
|
-
# dealing with polymorphic belongs_to
|
22
|
-
if association.options.has_key?(:foreign_type)
|
23
|
-
foreign_type = record.send(association.options.fetch(:foreign_type))
|
24
|
-
target_class = foreign_type.constantize unless foreign_type.nil?
|
25
|
-
else
|
26
|
-
target_class = association.klass
|
27
|
-
end
|
28
|
-
|
29
|
-
if options[:allow_new]
|
30
|
-
exists = value.new_record? or target_class.exists?(value)
|
31
|
-
else
|
32
|
-
exists = target_class.exists?(value)
|
33
|
-
end
|
34
|
-
|
35
|
-
if target_class.nil? or !exists
|
36
|
-
errors = [attribute]
|
37
|
-
|
38
|
-
# add the error on both :relation and :relation_id
|
39
|
-
if options[:both]
|
40
|
-
errors.push(attribute.to_s.ends_with?("_id") ? normalized : association.primary_key_name)
|
41
|
-
end
|
42
|
-
|
43
|
-
errors.each do |error|
|
44
|
-
record.errors.add(error, options[:message], :default => "does not exist")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
data/lib/rails3.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
require 'active_model'
|
3
|
-
|
4
|
-
module Perfectline
|
5
|
-
module ValidatesExistence
|
6
|
-
|
7
|
-
module Rails3
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
class ExistenceValidator < ActiveModel::EachValidator
|
11
|
-
|
12
|
-
def initialize(options)
|
13
|
-
# set the default message if its unspecified
|
14
|
-
options[:message] ||= :existence
|
15
|
-
options[:both] = true unless options.key?(:both)
|
16
|
-
options[:allow_new] = false unless options.key?(:allow_new)
|
17
|
-
super(options)
|
18
|
-
end
|
19
|
-
|
20
|
-
def validate_each(record, attribute, value)
|
21
|
-
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
22
|
-
association = record.class.reflect_on_association(normalized)
|
23
|
-
|
24
|
-
if association.nil? || !association.belongs_to?
|
25
|
-
raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
|
26
|
-
end
|
27
|
-
|
28
|
-
target_class = nil
|
29
|
-
|
30
|
-
# dealing with polymorphic belongs_to
|
31
|
-
if association.options[:polymorphic]
|
32
|
-
foreign_type = record.send(association.options[:foreign_type] || association.foreign_type)
|
33
|
-
target_class = foreign_type.constantize unless foreign_type.nil?
|
34
|
-
else
|
35
|
-
target_class = association.klass
|
36
|
-
end
|
37
|
-
|
38
|
-
if value.nil? || target_class.nil? || !exists?(target_class, value)
|
39
|
-
errors = [attribute]
|
40
|
-
|
41
|
-
# add the error on both :relation and :relation_id
|
42
|
-
if options[:both]
|
43
|
-
if Rails::VERSION::MINOR >= 1
|
44
|
-
foreign_key = association.foreign_key
|
45
|
-
else
|
46
|
-
foreign_key = association.primary_key_name
|
47
|
-
end
|
48
|
-
|
49
|
-
errors << (attribute.to_s.ends_with?("_id") ? normalized : foreign_key)
|
50
|
-
end
|
51
|
-
|
52
|
-
messages = [:"#{record.class.i18n_scope}.errors.messages.existence", "does not exist"]
|
53
|
-
|
54
|
-
errors.each do |error|
|
55
|
-
record.errors.add(error, options[:message], :message => messages)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def exists?(target_class, value)
|
61
|
-
(options[:allow_new] && value.new_record?) || target_class.exists?(value)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
module ClassMethods
|
66
|
-
def validates_existence_of(*attr_names)
|
67
|
-
validates_with ExistenceValidator, _merge_attributes(attr_names)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|