Skip to content
Browse files

Allow specification of primary key format, to facility non-integer pr…

…imary keys.
  • Loading branch information...
1 parent e84fa5e commit bbd4b55cde7bf5ac99479033244dd22bd221d634 Rolf Timmermans committed Oct 26, 2010
Showing with 15 additions and 2 deletions.
  1. +3 −2 lib/ancestry/has_ancestry.rb
  2. +12 −0 test/has_ancestry_test.rb
View
5 lib/ancestry/has_ancestry.rb
@@ -7,7 +7,7 @@ def has_ancestry options = {}
# Check options
raise Ancestry::AncestryException.new("Options for has_ancestry must be in a hash.") unless options.is_a? Hash
options.each do |key, value|
- unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column].include? key
+ unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :primary_key_format].include? key
raise Ancestry::AncestryException.new("Unknown option for has_ancestry: #{key.inspect} => #{value.inspect}.")
end
end
@@ -31,7 +31,8 @@ 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
+ key_format = options[:primary_key_format] || /[0-9]+/
+ validates_format_of ancestry_column, :with => /\A#{key_format.source}(\/#{key_format.source})*\Z/, :allow_nil => true
# Validate that the ancestor ids don't include own id
validate :ancestry_exclude_self
View
12 test/has_ancestry_test.rb
@@ -213,6 +213,18 @@ def test_ancestry_column_validation
node.valid?; assert !node.errors[model.ancestry_column].blank?
end
end
+
+ AncestryTestDatabase.with_model :primary_key_format => /[0-9a-z]+/ do |model|
+ node = model.create
+ ['xk7', '9x1/l4n', 'r1c/4z9/8ps', nil].each do |value|
+ node.send :write_attribute, model.ancestry_column, value
+ node.valid?; assert node.errors[model.ancestry_column].blank?
+ end
+ ['s9a/xk2/', '/s92/d92', 'X', 'X/Y', 'S23', '/xk2'].each do |value|
+ node.send :write_attribute, model.ancestry_column, value
+ node.valid?; assert !node.errors[model.ancestry_column].blank?
+ end
+ end
end
def test_descendants_move_with_node

0 comments on commit bbd4b55

Please sign in to comment.
Something went wrong with that request. Please try again.