Permalink
Browse files

added more cases for CircularHierarchy errors to preview infinite cop…

…ying loop, also fixed a fixnum to string type error
  • Loading branch information...
1 parent 00ad958 commit 90f0df9662004ce2d82071bb9dd4f2e512b08e1f Nathan Batson committed May 2, 2011
Showing with 23 additions and 10 deletions.
  1. +10 −0 config/routes.rb
  2. +0 −9 copy_move_extension.rb
  3. +9 −0 lib/copy_move/controller.rb
  4. +4 −1 lib/copy_move/model.rb
View
@@ -0,0 +1,10 @@
+ActionController::Routing::Routes.draw do |map|
+
+ map.with_options(:controller => "admin/pages") do |cm|
+ cm.copy_page_admin_page '/admin/pages/:id/copy_page', :action => 'copy_page'
+ cm.copy_children_admin_page '/admin/pages/:id/copy_children', :action => 'copy_children'
+ cm.copy_tree_admin_page '/admin/pages/:id/copy_tree', :action => 'copy_tree'
+ cm.move_admin_page '/admin/pages/:id/move', :action => 'move'
+ end
+
+end
View
@@ -5,15 +5,6 @@ class CopyMoveExtension < Radiant::Extension
description "Adds the ability to copy and move a page and all of its children"
url "http://gravityblast.com/projects/radiant-copymove-extension/"
- define_routes do |map|
- map.with_options(:controller => "admin/pages") do |cm|
- cm.copy_page_admin_page '/admin/pages/:id/copy_page', :action => 'copy_page'
- cm.copy_children_admin_page '/admin/pages/:id/copy_children', :action => 'copy_children'
- cm.copy_tree_admin_page '/admin/pages/:id/copy_tree', :action => 'copy_tree'
- cm.move_admin_page '/admin/pages/:id/move', :action => 'move'
- end
- end
-
def activate
Admin::PagesController.class_eval do
include CopyMove::Controller
@@ -14,18 +14,27 @@ def copy_page
@new_page = @page.copy_to(@parent, params[:status_id])
flash[:notice] = "A copy of <strong>#{@page.title}</strong> was created under <strong>#{@parent.title}</strong>."
redirect_to admin_pages_url
+ rescue CopyMove::CircularHierarchy => e
+ flash[:error] = e.message
+ redirect_to admin_pages_url
end
def copy_children
@new_page = @page.copy_with_children_to(@parent, params[:status_id])
flash[:notice] = "Copies of <strong>#{@page.title}</strong> and its immediate children were created under <strong>#{@parent.title}</strong>."
redirect_to admin_pages_url
+ rescue CopyMove::CircularHierarchy => e
+ flash[:error] = e.message
+ redirect_to admin_pages_url
end
def copy_tree
@new_page = @page.copy_tree_to(@parent, params[:status_id])
flash[:notice] = "Copies of <strong>#{@page.title}</strong> and all its descendants were created under <strong>#{@parent.title}</strong>."
redirect_to admin_pages_url
+ rescue CopyMove::CircularHierarchy => e
+ flash[:error] = e.message
+ redirect_to admin_pages_url
end
def move
View
@@ -5,7 +5,7 @@ def new_slug_and_title_under(parent)
test_page.parent = parent
until test_page.valid?
index = (index || 0) + 1
- test_page.title = "#{title} (Copy#{' '+index if index > 1})"
+ test_page.title = "#{title} (Copy#{' ' + String(index) if index > 1})"
test_page.slug = "#{slug}-#{index}"
end
{:slug => test_page.slug, :title => test_page.title}
@@ -17,6 +17,7 @@ def move_under(parent)
end
def copy_to(parent, status = nil)
+ raise CircularHierarchy.new(self) if parent == self || parent.ancestors.include?(self)
parent.children.build(copiable_attributes.symbolize_keys.merge(new_slug_and_title_under(parent))).tap do |new_page|
self.parts.each do |part|
new_page.parts << part.clone
@@ -27,12 +28,14 @@ def copy_to(parent, status = nil)
end
def copy_with_children_to(parent, status = nil)
+ raise CircularHierarchy.new(self) if parent == self || parent.ancestors.include?(self)
copy_to(parent, status).tap do |new_page|
children.each {|child| child.copy_to(new_page, status) }
end
end
def copy_tree_to(parent, status = nil)
+ raise CircularHierarchy.new(self) if parent == self || parent.ancestors.include?(self)
copy_to(parent, status).tap do |new_page|
children.each {|child| child.copy_tree_to(new_page, status) }
end

0 comments on commit 90f0df9

Please sign in to comment.