Skip to content

Commit

Permalink
Merge pull request #227 from javiyu/bugfix/set_parent_with_non_defaul…
Browse files Browse the repository at this point in the history
…t_ancestry_column

Fix set parent with non default ancestry column
  • Loading branch information
stefankroes committed Mar 19, 2015
2 parents 2d80781 + 5e8847d commit 74a0ada
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ def unscoped_descendants

# Validates the ancestry, but can also be applied if validation is bypassed to determine if children should be affected
def sane_ancestry?
ancestry.nil? || (ancestry.to_s =~ Ancestry::ANCESTRY_PATTERN && !ancestor_ids.include?(self.id))
ancestry_value = read_attribute(self.ancestry_base_class.ancestry_column)
ancestry_value.nil? || (ancestry_value.to_s =~ Ancestry::ANCESTRY_PATTERN && !ancestor_ids.include?(self.id))
end

def unscoped_find id
Expand Down
12 changes: 12 additions & 0 deletions test/concerns/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def test_descendants_move_with_node
end
end

def test_set_parent_with_non_default_ancestry_column
AncestryTestDatabase.with_model :depth => 3, :width => 3, :ancestry_column => :alternative_ancestry do |model, roots|
root1, root2, root3 = roots.map(&:first)
assert_no_difference 'root1.descendants.size' do
assert_difference 'root2.descendants.size', root1.subtree.size do
root1.parent = root2
root1.save!
end
end
end
end

def test_setup_test_nodes
AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
assert_equal Array, roots.class
Expand Down

0 comments on commit 74a0ada

Please sign in to comment.