Allow non-integer primary keys. #26

Merged
2 commits merged into from Oct 26, 2010
Jump to file or symbol
Failed to load files and symbols.
+17 −2
Split
View
@@ -80,6 +80,8 @@ The has_ancestry methods supports the following options:
- Migrate: add_column [table], :ancestry_depth, :integer, :default => 0
- Build cache: TreeNode.rebuild_depth_cache!
:depth_cache_column Pass in a symbol to store depth cache in a different column
+ :primary_key_format Supply a regular expression that matches the format of your primary key.
+ By default, primary keys only match integers ([0-9]+).
= (Named) Scopes
@@ -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
@@ -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