perfectline-validates_existence 0.2.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 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
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 1
@@ -0,0 +1,50 @@
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
+ record.errors.add(attr, :existence, :default => configuration[:message]) unless association_class.exists?(value)
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ ActiveRecord::Base.send(:extend, Perfectline::Validations::ValidatesExistence)
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perfectline-validates_existence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Tanel Suurhans
8
+ - Tarmo Lehtpuu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-03 00:00:00 -07: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
+ - README.markdown
27
+ - Rakefile
28
+ - VERSION.yml
29
+ - lib/validates_existence.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/perfectline/validates_existence/tree/master
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Validates Rails model belongs_to association existence.
56
+ test_files: []
57
+