Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clone_with_modifications! method and tests/documentation #55

Open
wants to merge 1 commit into
base: 599198450656d18fb0afd88b390cc73ad9431ccd
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ For example, from IRB:
Additionally, if you think something is wrong with your depth cache:

>> TreeNode.rebuild_depth_cache!

= Copying

Ancestry provides a method to clone a tree, optionally replacing attributes and/or preserving a reference to the parent. This allows the creation of structures that use a 'default' tree and make copies of it. Example

class Category < ActiveRecord::Base
belongs_to :project
belongs_to :source_category, :class_name => "Category"
scope :defaults, where(:project_id => nil)

def self.import_from_defaults_for(project)
defaults.roots.all.each do |root|
root.clone_with_modifications!({:project_id => project.id}, nil, :source_category_id)
end
end

class Project < ActiveRecord::Base
has_many :categories, :dependent => :destroy
after_create Proc.new { |p| Category.import_from_defaults_for(self) }
end

= Tests

Expand Down
13 changes: 13 additions & 0 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ def apply_orphan_strategy
end
end
end

# Clone an object and all children
# => replacing values with those from attributes if present
# => setting parent to new parent if present
# => setting the "original_id_field_name" if present to the id of the original object
def clone_with_modifications!(attributes = nil, parent = nil, original_id_field_name = nil)
clone = self.class.create!(self.attributes.merge(:ancestry => nil).merge(attributes))
clone.send("#{original_id_field_name}=", self.id) if original_id_field_name
clone.parent = parent
self.children.each { |child| child.clone_with_modifications!(attributes, clone, original_id_field_name) }
clone.save!
clone
end

# The ancestry value for this record's children
def child_ancestry
Expand Down
18 changes: 18 additions & 0 deletions test/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,22 @@ def test_sort_by_ancestry
assert_equal [n1, n2, n4, n3, n5].map(&:id), arranged.map(&:id)
end
end

def test_clone_with_modifications!
AncestryTestDatabase.with_model :extra_columns => {:source_id => :integer, :project_id => :integer} do |model|
n1 = model.create!(:project_id => nil)
n2 = model.create!(:parent => n1, :project_id => nil)
n3 = model.create!(:parent => n2, :project_id => nil)
n4 = model.create!(:parent => n2, :project_id => nil)
n5 = model.create!(:parent => n1, :project_id => nil)

n1c = n1.clone_with_modifications!({:project_id => 1}, nil, :source_id)

assert_equal n1.id, n1c.source_id
assert_equal 1, n1c.project_id
assert_equal n1.descendants.count, n1c.descendants.count
assert_equal 1, n1c.descendants.last.project_id
assert_equal n1.descendants.last.id, n1c.descendants.last.source_id
end
end
end