Permalink
Browse files

code cleanup

  • Loading branch information...
1 parent b48181e commit c65a7922d3d8e6e84975d80113abd407408a8f7b @raszi committed Nov 4, 2011
Showing with 88 additions and 78 deletions.
  1. +26 −20 lib/validates_existence.rb
  2. +62 −58 test/validates_existence_test.rb
View
@@ -12,34 +12,40 @@ module ClassMethods
# itself may be nil. A non-nil key requires that the foreign object must exist.
# Works with polymorphic belongs_to.
def validates_existence_of(*attr_names)
- configuration = { :message => :non_existent, :on => :save }
- configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
-
- send(validation_method(configuration[:on]), configuration) do |record|
+ opts = { :message => :non_existent, :on => :save }
+ opts.update(attr_names.extract_options!)
+
+ send(validation_method(opts[:on]), opts) do |record|
attr_names.each do |attr_name|
attr_name = attr_name.to_s.sub(/_id$/, '').to_sym
+
unless (assoc = reflect_on_association(attr_name)) && assoc.macro == :belongs_to
raise ArgumentError, "Cannot validate existence of :#{attr_name} because it is not a belongs_to association."
end
- associated_object = record.send(attr_name)
- next if !associated_object.nil? && (associated_object.new_record? || associated_object.id)
-
- fk_value = record.send assoc.primary_key_name.to_sym
- unless fk_value.nil? && configuration[:allow_nil] || fk_value.blank? && configuration[:allow_blank]
- if (foreign_type = assoc.options[:foreign_type]) # polymorphic
- foreign_type_value = record[assoc.options[:foreign_type]]
- if !foreign_type_value.blank?
- assoc_class = foreign_type_value.constantize
- else
- record.errors.add(attr_name, :does_not_exist, :default => configuration[:message])
- next
- end
- else # not polymorphic
- assoc_class = assoc.klass
+ attr_key = assoc.primary_key_name.to_sym
+ fk_value = record.send(attr_key)
+
+ next if fk_value.nil? && opts[:allow_nil] || fk_value.blank? && opts[:allow_blank]
+
+ if fk_value.nil?
+ associated_object = record.send(attr_name)
+ next if !associated_object.nil? && (associated_object.new_record? || associated_object.id)
+ end
+
+ if foreign_type = assoc.options[:foreign_type] # polymorphic
+ foreign_type_value = record[assoc.options[:foreign_type]]
+ if foreign_type_value.blank?
+ record.errors.add(attr_name, :does_not_exist, :default => opts[:message])
+ next
+ else
+ assoc_class = foreign_type_value.constantize
end
- record.errors.add(attr_name, :does_not_exist, :default => configuration[:message]) unless assoc_class && assoc_class.exists?(fk_value)
+ else # not polymorphic
+ assoc_class = assoc.klass
end
+
+ record.errors.add(attr_name, :does_not_exist, :default => opts[:message]) unless assoc_class && assoc_class.exists?(fk_value)
end
end
end
@@ -1,14 +1,11 @@
require File.dirname(__FILE__) + '/init'
-
-# Blog
-
+ # Blog
class Blog < ActiveRecord::Base
has_many :posts
end
-# Post
-
+ # Post
class Post < ActiveRecord::Base
belongs_to :blog
has_many :comments, :as => :commentable
@@ -32,8 +29,7 @@ class PostWithRequiredBlogUnless < Post
attr_accessor :condition
end
-# Comment
-
+ # Comment
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
@@ -54,131 +50,139 @@ def setup
@default_blog = Blog.create
@default_post = PostWithoutRequiredBlog.create
end
-
+
def teardown
drop_all_tables
end
# PostWithRequiredBlog
def test_should_create_post_with_required_blog_with_valid_blog
- @post = PostWithRequiredBlog.new :blog_id => @default_blog.id
- assert @post.save
+ obj = PostWithRequiredBlog.new :blog_id => @default_blog.id
+ assert obj.valid?
+
+ obj = PostWithRequiredBlog.new :blog => @default_blog
+ assert obj.valid?
end
def test_should_not_create_post_with_required_blog_when_blog_is_nil
- @post = PostWithRequiredBlog.new
- assert !@post.save
- assert @post.errors.on(:blog)
+ obj = PostWithRequiredBlog.new
+ assert !obj.valid?
+ assert obj.errors.on(:blog)
+ end
+
+ def test_should_create_post_with_required_blog_when_blog_does_exist
+ obj = PostWithRequiredBlog.new :blog_id => nil, :blog => @default_blog
+ assert obj.valid?
end
def test_should_not_create_post_with_required_blog_when_blog_does_not_exist
- @post = PostWithRequiredBlog.new :blog_id => '2'
- assert !@post.save
- assert @post.errors.on(:blog)
+ obj = PostWithRequiredBlog.new :blog_id => '2'
+ assert !obj.valid?
+ assert obj.errors.on(:blog)
end
def test_should_create_post_with_required_blog_when_blog_is_a_new_blog
- @post = PostWithRequiredBlog.new :blog => Blog.new
- assert @post.save
+ obj = PostWithRequiredBlog.new :blog => Blog.new
+ assert obj.valid?
end
# PostWithoutRequiredBlog
def test_should_create_post_without_required_blog_with_valid_blog
- @post = PostWithoutRequiredBlog.new :blog_id => @default_blog.id
- assert @post.save
+ obj = PostWithoutRequiredBlog.new :blog_id => @default_blog.id
+ assert obj.valid?
end
def test_should_create_post_without_required_blog_with_a_new_blog
- @post = PostWithoutRequiredBlog.new :blog => Blog.new
- assert @post.save
+ obj = PostWithoutRequiredBlog.new :blog => Blog.new
+ assert obj.valid?
end
def test_should_create_post_without_required_blog_when_blog_is_nil
- @post = PostWithoutRequiredBlog.new
- assert @post.save
+ obj = PostWithoutRequiredBlog.new
+ assert obj.valid?
end
def test_should_not_create_post_without_required_blog_when_blog_does_not_exist
- @post = PostWithoutRequiredBlog.new :blog_id => '2'
- assert !@post.save
- assert @post.errors.on(:blog)
+ obj = PostWithoutRequiredBlog.new :blog_id => '2'
+ assert !obj.valid?
+ assert obj.errors.on(:blog)
end
# Polymorphic CommentWithRequiredCommentable
def test_should_create_comment_with_required_commentable_with_valid_commentable
- @comment = CommentWithRequiredCommentable.new :commentable_id => @default_post.id, :commentable_type => 'Post'
- assert @comment.save
+ obj = CommentWithRequiredCommentable.new :commentable_id => @default_post.id, :commentable_type => 'Post'
+ assert obj.valid?
end
def test_should_create_comment_with_required_commentable_with_a_new_commentable
- @comment = CommentWithRequiredCommentable.new :commentable => Post.new
- assert @comment.save
+ obj = CommentWithRequiredCommentable.new :commentable => Post.new
+ assert obj.valid?
end
def test_should_not_create_comment_with_required_commentable_when_commentable_is_nil
- @comment = CommentWithRequiredCommentable.new
- assert !@comment.save
- assert @comment.errors.on(:commentable)
+ obj = CommentWithRequiredCommentable.new
+ assert !obj.valid?
+ assert obj.errors.on(:commentable)
end
def test_should_not_create_comment_with_required_commentable_when_commentable_does_not_exist
- @comment = CommentWithRequiredCommentable.new :commentable_id => '2', :commentable_type => 'Post'
- assert !@comment.save
- assert @comment.errors.on(:commentable)
+ obj = CommentWithRequiredCommentable.new :commentable_id => '2', :commentable_type => 'Post'
+ assert !obj.valid?
+ assert obj.errors.on(:commentable)
end
# Polymorphic CommentWithoutRequiredCommentable
def test_should_create_comment_without_required_commentable_with_valid_commentable
- @comment = CommentWithoutRequiredCommentable.new :commentable_id => @default_post.id, :commentable_type => 'Post'
- assert @comment.save
+ obj = CommentWithoutRequiredCommentable.new :commentable_id => @default_post.id, :commentable_type => 'Post'
+ assert obj.valid?
end
def test_should_create_comment_without_required_commentable_with_a_new_commentable
- @comment = CommentWithoutRequiredCommentable.new :commentable => Post.new
- assert @comment.save
+ obj = CommentWithoutRequiredCommentable.new :commentable => Post.new
+ assert obj.valid?
end
def test_should_create_comment_without_required_commentable_when_commentable_is_nil
- @comment = CommentWithoutRequiredCommentable.new
- assert @comment.save
+ obj = CommentWithoutRequiredCommentable.new
+ assert obj.valid?
end
def test_should_not_create_comment_without_required_commentable_when_commentable_does_not_exist
- @comment = CommentWithoutRequiredCommentable.new :commentable_id => '2', :commentable_type => 'Post'
- assert !@comment.save
- assert @comment.errors.on(:commentable)
+ obj = CommentWithoutRequiredCommentable.new :commentable_id => '2', :commentable_type => 'Post'
+ assert !obj.valid?
+ assert obj.errors.on(:commentable)
end
# PostWithRequiredBlogIf (:if => :condition)
def test_post_should_require_blog_when_if_condition_is_true
- @post = PostWithRequiredBlogIf.new
- @post.condition = true
- assert !@post.save
+ obj = PostWithRequiredBlogIf.new
+ obj.condition = true
+ assert !obj.valid?
end
def test_post_should_not_require_blog_when_if_condition_is_false
- @post = PostWithRequiredBlogIf.new
- @post.condition = false
- assert @post.save
+ obj = PostWithRequiredBlogIf.new
+ obj.condition = false
+ assert obj.valid?
end
# PostWithRequiredBlogUnless (:unless => :condition)
def test_post_should_require_blog_when_unless_condition_is_false
- @post = PostWithRequiredBlogUnless.new
- @post.condition = false
- assert !@post.save
+ obj = PostWithRequiredBlogUnless.new
+ obj.condition = false
+ assert !obj.valid?
end
def test_post_should_not_require_blog_when_unless_condition_is_true
- @post = PostWithRequiredBlogUnless.new
- @post.condition = true
- assert @post.save
+ obj = PostWithRequiredBlogUnless.new
+ obj.condition = true
+ assert obj.valid?
end
end

0 comments on commit c65a792

Please sign in to comment.