Skip to content

Commit

Permalink
Replaced valid? in before_save callback with simpler sanity check
Browse files Browse the repository at this point in the history
Minor refactors
  • Loading branch information
Stefan Henzen committed Jun 27, 2012
1 parent 6d96228 commit 522d663
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/ancestry.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/ancestry/has_ancestry.rb
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/ancestry/instance_methods.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion test/environment.rb
Expand Up @@ -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
Expand Down

0 comments on commit 522d663

Please sign in to comment.