validates_existence 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +48 -0
- data/Rakefile +45 -0
- data/VERSION.yml +4 -0
- data/init.rb +1 -0
- data/install.rb +2 -0
- data/lib/validates_existence.rb +52 -0
- data/validates_existence.gemspec +41 -0
- metadata +64 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 PerfectLine LLC (www.perfectline.co.uk)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# ValidatesExistence
|
2
|
+
|
3
|
+
This plugin library adds ActiveRecord models a way to check if a `:belongs_to` association actually exists upon saving.
|
4
|
+
This is achieved via adding a `validates_existence_of` validator to the base validations module.
|
5
|
+
It also supports `:allow_nil => true/false` and `:polymorphic => true` associations.
|
6
|
+
|
7
|
+
### Example
|
8
|
+
class Pony < ActiveRecord::Base
|
9
|
+
belongs_to :wizard
|
10
|
+
belongs_to :person, :polymorphic => true
|
11
|
+
|
12
|
+
validates_existence_of :wizard_id
|
13
|
+
validates_existence_of :wizard #works both ways
|
14
|
+
|
15
|
+
validates_existence_of :person, :allow_nil => true
|
16
|
+
end
|
17
|
+
|
18
|
+
pony = Pony.new
|
19
|
+
pony.wizard_id = 100 # such wizard does not exist ofcourse
|
20
|
+
pony.valid?
|
21
|
+
|
22
|
+
pony.errors.on(:wizard) #=> "does not exist"
|
23
|
+
|
24
|
+
## I18N
|
25
|
+
|
26
|
+
The default error message is `does not exist`.
|
27
|
+
This can be customized via Rails I18N like any other validation error message via `:existence` key.
|
28
|
+
|
29
|
+
|
30
|
+
### Example
|
31
|
+
|
32
|
+
This would be your customized en.yaml:
|
33
|
+
|
34
|
+
en:
|
35
|
+
activerecord:
|
36
|
+
errors:
|
37
|
+
messages:
|
38
|
+
existence: "has gone missing!"
|
39
|
+
|
40
|
+
## Honorable mentions
|
41
|
+
This plugin is inspired by ideas from **Josh Susser**
|
42
|
+
|
43
|
+
## Authors
|
44
|
+
**Tanel Suurhans** - tanel.suurhans_at_perfectline_d0t_ee
|
45
|
+
**Tarmo Lehtpuu** - tarmo.lehtpuu_at_perfectline_d0t_ee
|
46
|
+
|
47
|
+
## License
|
48
|
+
Copyright 2009 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rcov/rcovtask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |jewel|
|
12
|
+
jewel.name = 'validates_existence'
|
13
|
+
jewel.summary = 'Validates Rails model belongs_to association existence.'
|
14
|
+
jewel.email = 'tanel.suurhans@perfectline.ee'
|
15
|
+
jewel.homepage = 'http://github.com/perfectline/validates_existence/tree/master'
|
16
|
+
jewel.description = 'A library for validating model association existence.'
|
17
|
+
jewel.authors = ["Tanel Suurhans", "Tarmo Lehtpuu"]
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Test the plugin.'
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Generate documentation plugin.'
|
32
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = 'ValidatesExistence'
|
35
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
36
|
+
rdoc.rdoc_files.include('README.markdown')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Generate code coverage report'
|
41
|
+
Rcov::RcovTask.new(:rcov) do |rcov|
|
42
|
+
rcov.libs << 'test'
|
43
|
+
rcov.test_files = FileList['test/*_test.rb']
|
44
|
+
rcov.verbose = true
|
45
|
+
end
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_existence'
|
data/install.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Perfectline
|
2
|
+
module Validations
|
3
|
+
module ValidatesExistence
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:extend, ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def validates_existence_of(*attr_names)
|
12
|
+
configuration = {:message => "does not exist", :on => :save}
|
13
|
+
configuration.update(attr_names.extract_options!.symbolize_keys)
|
14
|
+
|
15
|
+
send(validation_method(configuration[:on] || :save), configuration) do |record|
|
16
|
+
|
17
|
+
attr_names.each do |attr|
|
18
|
+
attribute = attr.to_s.sub(/_id$/, '').to_sym
|
19
|
+
association = reflect_on_association(attribute)
|
20
|
+
|
21
|
+
if association.nil? || association.macro != :belongs_to
|
22
|
+
raise ArgumentError, "Can not validate existence on #{attribute}, not a belongs_to association."
|
23
|
+
end
|
24
|
+
|
25
|
+
value = record.__send__(association.primary_key_name)
|
26
|
+
next if value.nil? && configuration[:allow_nil]
|
27
|
+
|
28
|
+
if association.options.has_key?(:foreign_type)
|
29
|
+
foreign_type = record.__send__(association.options[:foreign_type])
|
30
|
+
|
31
|
+
if not foreign_type.blank?
|
32
|
+
association_class = foreign_type.constantize
|
33
|
+
else
|
34
|
+
record.errors.add(attr, configuration[:message]) and next
|
35
|
+
end
|
36
|
+
else
|
37
|
+
association_class = association.klass
|
38
|
+
end
|
39
|
+
|
40
|
+
unless association_class.exists?(value)
|
41
|
+
record.errors.add(attr, :existence, :default => configuration[:message])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ActiveRecord::Base.send(:include, Perfectline::Validations::ValidatesExistence)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{validates_existence}
|
5
|
+
s.version = "0.3.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Tanel Suurhans", "Tarmo Lehtpuu"]
|
9
|
+
s.date = %q{2009-11-20}
|
10
|
+
s.description = %q{A library for validating model association existence.}
|
11
|
+
s.email = %q{tanel.suurhans@perfectline.ee}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"README.markdown"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".gitignore",
|
17
|
+
"MIT-LICENSE",
|
18
|
+
"README.markdown",
|
19
|
+
"Rakefile",
|
20
|
+
"VERSION.yml",
|
21
|
+
"init.rb",
|
22
|
+
"install.rb",
|
23
|
+
"lib/validates_existence.rb",
|
24
|
+
"validates_existence.gemspec"
|
25
|
+
]
|
26
|
+
s.homepage = %q{http://github.com/perfectline/validates_existence/tree/master}
|
27
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
s.rubygems_version = %q{1.3.5}
|
30
|
+
s.summary = %q{Validates Rails model belongs_to association existence.}
|
31
|
+
|
32
|
+
if s.respond_to? :specification_version then
|
33
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
34
|
+
s.specification_version = 3
|
35
|
+
|
36
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
37
|
+
else
|
38
|
+
end
|
39
|
+
else
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_existence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tanel Suurhans
|
8
|
+
- Tarmo Lehtpuu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-11-20 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A library for validating model association existence.
|
18
|
+
email: tanel.suurhans@perfectline.ee
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README.markdown
|
29
|
+
- Rakefile
|
30
|
+
- VERSION.yml
|
31
|
+
- init.rb
|
32
|
+
- install.rb
|
33
|
+
- lib/validates_existence.rb
|
34
|
+
- validates_existence.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/perfectline/validates_existence/tree/master
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Validates Rails model belongs_to association existence.
|
63
|
+
test_files: []
|
64
|
+
|