Permalink
Browse files

Replaced valid? in before_save callback with simpler sanity check

Minor refactors
  • Loading branch information...
Stefan Henzen
Stefan Henzen committed Jun 27, 2012
1 parent 6d96228 commit 522d66367081922889378e9d1509e2384154eada
Showing with 23 additions and 5 deletions.
  1. +4 −0 Gemfile
  2. +4 −0 lib/ancestry.rb
  3. +1 −1 lib/ancestry/has_ancestry.rb
  4. +9 −3 lib/ancestry/instance_methods.rb
  5. +5 −1 test/environment.rb
View
@@ -2,6 +2,10 @@ source 'http://rubygems.org'
gemspec
+group :development, :test do
+ gem 'debugger' if RUBY_VERSION =~ /\A1.9/
+end
+
group :development do
gem 'rdoc'
end
View
@@ -2,3 +2,7 @@
require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/instance_methods')
require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/exceptions')
require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/has_ancestry')
+
+module Ancestry
+ ANCESTRY_PATTERN = /\A[0-9]+(\/[0-9]+)*\Z/
+end
@@ -27,7 +27,7 @@ def has_ancestry options = {}
self.base_class = self
# Validate format of ancestry column value
- validates_format_of ancestry_column, :with => /\A[0-9]+(\/[0-9]+)*\Z/, :allow_nil => true
+ validates_format_of ancestry_column, :with => Ancestry::ANCESTRY_PATTERN, :allow_nil => true
# Validate that the ancestor ids don't include own id
validate :ancestry_exclude_self
@@ -9,8 +9,8 @@ def ancestry_exclude_self
def update_descendants_with_new_ancestry
# Skip this if callbacks are disabled
unless ancestry_callbacks_disabled?
- # If node is valid, not a new record and ancestry was updated ...
- if changed.include?(self.base_class.ancestry_column.to_s) && !new_record? && valid?
+ # If node is not a new record and ancestry was updated and the new ancestry is sane ...
+ if changed.include?(self.base_class.ancestry_column.to_s) && !new_record? && sane_ancestry?
# ... for each descendant ...
unscoped_descendants.each do |descendant|
# ... replace old ancestry with new ancestry
@@ -221,9 +221,15 @@ def primary_key_type
end
def unscoped_descendants
- self.base_class.send(:with_exclusive_scope) do
+ self.base_class.unscoped do
self.base_class.all(:conditions => descendant_conditions)
end
end
+
+ # basically validates the ancestry, but also applied if validation is
+ # bypassed to determine if chidren should be affected
+ def sane_ancestry?
+ ancestry.nil? || (ancestry.to_s =~ Ancestry::ANCESTRY_PATTERN && !ancestor_ids.include?(self.id))
+ end
end
end
View
@@ -9,7 +9,11 @@
require 'active_record'
require 'active_support/test_case'
require 'test/unit'
-require 'ancestry'
+
+# this is to make absolutely sure we test this one, not the one
+# installed on the system.
+require File.expand_path('../../lib/ancestry', __FILE__)
+
require 'debugger' if RUBY_VERSION =~ /\A1.9/
class AncestryTestDatabase

0 comments on commit 522d663

Please sign in to comment.