did_validates_presence_of 0.0.1
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 +21 -0
- data/lib/validates_existence.rb +47 -0
- metadata +68 -0
data/README.markdown
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# ValidatesExistence
|
|
2
|
+
|
|
3
|
+
This plugin adds a new `validates_existence_of` method to `ActiveRecord::Base`.
|
|
4
|
+
|
|
5
|
+
The `validates_existence_of` validator checks that a foreign key in a `belongs_to`
|
|
6
|
+
association points to an existing record. If `:allow_nil => true`, then the key
|
|
7
|
+
itself may be nil. A non-nil key requires that the foreign object must exist.
|
|
8
|
+
Works with polymorphic `belongs_to`.
|
|
9
|
+
|
|
10
|
+
The default error message is "does not exist".
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
class Person < ActiveRecord::Base
|
|
15
|
+
belongs_to :address
|
|
16
|
+
validates_existence_of :address
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Note that this validation performs a query to see if the record in question exists.
|
|
20
|
+
|
|
21
|
+
*Copyright (c) 2007-2008 Josh Susser. Released under the MIT license.*
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Validations
|
|
3
|
+
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend ClassMethods
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
|
|
10
|
+
# The validates_existence_of validator checks that a foreign key in a belongs_to
|
|
11
|
+
# association points to an exisiting record. If :allow_nil => true, then the key
|
|
12
|
+
# itself may be nil. A non-nil key requires that the foreign object must exist.
|
|
13
|
+
# Works with polymorphic belongs_to.
|
|
14
|
+
def validates_existence_of(*attr_names)
|
|
15
|
+
configuration = { :message => "does not exist", :on => :save }
|
|
16
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
|
17
|
+
|
|
18
|
+
attr_names.each do |attr_name|
|
|
19
|
+
unless (assoc = reflect_on_association(attr_name)) && assoc.macro == :belongs_to
|
|
20
|
+
raise ArgumentError, "Cannot validate existence of :#{attr_name} because it is not a belongs_to association."
|
|
21
|
+
end
|
|
22
|
+
send(validation_method(configuration[:on])) do |record|
|
|
23
|
+
unless configuration[:if] && !evaluate_condition(configuration[:if], record)
|
|
24
|
+
fk_value = record[assoc.primary_key_name]
|
|
25
|
+
unless fk_value.nil? && configuration[:allow_nil]
|
|
26
|
+
if (foreign_type = assoc.options[:foreign_type]) # polymorphic
|
|
27
|
+
foreign_type_value = record[assoc.options[:foreign_type]]
|
|
28
|
+
if !foreign_type_value.blank?
|
|
29
|
+
assoc_class = foreign_type_value.constantize
|
|
30
|
+
else
|
|
31
|
+
record.errors.add(attr_name, configuration[:message])
|
|
32
|
+
next
|
|
33
|
+
end
|
|
34
|
+
else # not polymorphic
|
|
35
|
+
assoc_class = assoc.klass
|
|
36
|
+
end
|
|
37
|
+
record.errors.add(attr_name, configuration[:message]) unless assoc_class && assoc_class.exists?(fk_value)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end # ClassMethods
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: did_validates_presence_of
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 29
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.0.1
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Josh Susser
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2011-02-14 00:00:00 +01:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies: []
|
|
21
|
+
|
|
22
|
+
description: Rails plugin that provides a validates_existence_of method for ActiveRecord models to check existence of records referenced by belongs_to associations.
|
|
23
|
+
email: didier@nocoffee.fr
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions: []
|
|
27
|
+
|
|
28
|
+
extra_rdoc_files:
|
|
29
|
+
- README.markdown
|
|
30
|
+
files:
|
|
31
|
+
- lib/validates_existence.rb
|
|
32
|
+
- README.markdown
|
|
33
|
+
has_rdoc: true
|
|
34
|
+
homepage: https://github.com/joshsusser/validates_existence
|
|
35
|
+
licenses: []
|
|
36
|
+
|
|
37
|
+
post_install_message:
|
|
38
|
+
rdoc_options:
|
|
39
|
+
- --charset=UTF-8
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
none: false
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
hash: 3
|
|
48
|
+
segments:
|
|
49
|
+
- 0
|
|
50
|
+
version: "0"
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
none: false
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
hash: 3
|
|
57
|
+
segments:
|
|
58
|
+
- 0
|
|
59
|
+
version: "0"
|
|
60
|
+
requirements: []
|
|
61
|
+
|
|
62
|
+
rubyforge_project:
|
|
63
|
+
rubygems_version: 1.4.2
|
|
64
|
+
signing_key:
|
|
65
|
+
specification_version: 3
|
|
66
|
+
summary: check existence of records in associations
|
|
67
|
+
test_files: []
|
|
68
|
+
|