Permalink
Browse files

Plugin without tests

  • Loading branch information...
1 parent b530993 commit 86d1b803d81bc5ffaa68b68bcd2df4aa4ebfaf5e @bogdansss bogdansss committed Jun 6, 2008
Showing with 147 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. 0 README
  3. +41 −0 README.rdoc
  4. +22 −0 Rakefile
  5. +1 −0 init.rb
  6. +1 −0 install.rb
  7. +49 −0 lib/validates_existance_of.rb
  8. +4 −0 tasks/validates_existance_of_tasks.rake
  9. +8 −0 test/validates_existance_of_test.rb
  10. +1 −0 uninstall.rb
View
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Bogdan Sandulescu
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
0 README
No changes.
View
@@ -0,0 +1,41 @@
+= Validates Existance Of Plugin
+
+Validates the existance of an associated record in the database through a belongs_to association.
+
+validates_existance_of :ar_belongs_to_association
+
+Supports polymorphic associations.
+
+== Usage
+
+ validates_existance_of :belongs_to_association
+
+== Example
+
+ class Country < ActiveRecord::Base
+
+ has_many :users
+
+ end
+
+ class User < ActiveRecord::Base
+
+ belongs_to :country
+
+ validates_presence_of :country_id
+
+ # Will pass if the associated country exists in the database
+ validates_existance_of :country
+
+ end
+
+== Instalation
+
+ git clone git://github.com/bogdans83/validatesexistanceof.git
+
+or use "braid": http://evil.che.lu/projects/braid
+
+== Uninteresting stuff
+ Author:: Bogdan Sandulescu <bogdan at okapistudio the point com>
+ License:: Copyright 2008 Bogdan Sandulescu
+ Released under an MIT-style license.
View
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the validates_existance_of plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the validates_existance_of plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ValidatesExistanceOf'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/lib/validates_existance_of'
View
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,49 @@
+module ValidatesExistanceOf
+
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ end
+ end
+
+ module ClassMethods
+ # Validates if the associated object exists in the database
+ # Usage: validates_existance_of :ar_association
+ # Supports only belongs_to associations
+ def validates_existance_of(*attr_names)
+ configuration = {
+ :message => ActiveRecord::Errors.default_error_messages[:invalid],
+ :on => :save
+ }
+
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
+
+ validates_each(attr_names, configuration) do |record, attr, value|
+ reflection = record.class.reflect_on_association(attr)
+ if !reflection.nil? && reflection.macro == :belongs_to
+ if !reflection.options.has_key?(:polymorphic)
+ c = eval(reflection.class_name, binding)
+ obj = c.find(:first, :conditions => ["id = ?", record.send(reflection.primary_key_name)])
+ record.errors.add(reflection.primary_key_name, configuration[:message]) if obj.nil?
+ else
+ # If this is a polymorphic association
+ # We try to see if both polymorphic_type is present because we infer the class name from it.
+ if !reflection.options[:foreign_type].blank? && !record.send(reflection.options[:foreign_type]).blank?
+ c = eval(reflection.send(reflection.options[:foreign_type]), binding)
+ obj = c.exists?(record.send(reflection.primary_key_name))
+ record.errors.add(reflection.primary_key_name, configuration[:message]) if obj.nil?
+ else
+ record.errors.add(reflection.primary_key_name, configuration[:message])
+ end
+ end
+ end
+ end
+ end
+
+ end
+
+end
+
+class ActiveRecord::Base
+ include ValidatesExistanceOf
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :validates_existance_of do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ValidatesExistanceOfTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
View
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 86d1b80

Please sign in to comment.