Skip to content

Commit

Permalink
Create a copy of a site tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed Feb 10, 2011
1 parent 72edd56 commit 5018764
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
36 changes: 35 additions & 1 deletion app/models/site_node.rb
Expand Up @@ -42,7 +42,7 @@ class SiteNode < DomainModel

named_scope :with_type, lambda { |type| {:conditions => {:node_type => type}} }

attr_accessor :created_by_id
attr_accessor :created_by_id, :copying

# Expires the entire site when save or deleted
expires_site
Expand Down Expand Up @@ -479,6 +479,8 @@ def can_index?
protected

def after_create #:nodoc:
return if @copying

if self.node_type == 'P'
self.page_revisions.create( :language => Configuration.languages[0], :revision => '0.01', :active => 1, :created_by_id => self.created_by_id )
self.page_revisions[0].page_paragraphs.create(:display_type => 'html', :zone_idx => 1 )
Expand Down Expand Up @@ -581,4 +583,36 @@ def domain_link(*paths)
def self.domain_link(*paths)
Configuration.domain_link(SiteNode.link(*paths))
end

def copy_modifiers(node)
node.site_node_modifiers.each do |mod|
attrs = mod.attributes
%w(id site_node_id position).each { |fld| attrs.delete(fld) }
self.site_node_modifiers.create attrs
end
end

def copy(node, opts={})
attrs = node.attributes
%w(lft rgt id parent_id site_version_id).each { |fld| attrs.delete(fld) }
nd = SiteNode.new attrs
nd.site_version_id = self.site_version_id
nd.copying = true
nd.save

nd.copy_modifiers node

node.live_revisions.each do |rev|
tmp_rev = rev.create_temporary
tmp_rev.revision_container = nd
tmp_rev.save
tmp_rev.make_real
end

nd.move_to_child_of(self)

node.children.each { |child| nd.copy(child, opts) } if opts[:children]

nd
end
end
11 changes: 10 additions & 1 deletion app/models/site_version.rb
Expand Up @@ -79,5 +79,14 @@ def self.remove_archived(nd)
nd.child_cache_set(new_child_cache)
nd
end


def copy(new_name)
new_version = SiteVersion.create :name => new_name, :default_version => false
new_root = new_version.site_nodes.new :node_type => 'R', :title => ''
new_root.copying = true
new_root.save
new_root.copy_modifiers self.root
self.root.children.each { |child| new_root.copy(child, :children => true) }
new_version
end
end

0 comments on commit 5018764

Please sign in to comment.