Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrea Franz <andrea@gravityblast.com>
- Loading branch information
1 parent
6e33a95
commit c12feaa
Showing
9 changed files
with
480 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
require_dependency 'application_controller' | ||
|
||
class CopyMoveExtension < Radiant::Extension | ||
version "1.9.1" | ||
version "2.0.0" | ||
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.copy_move_index 'admin/pages/copy_move/:id', :controller => 'copy_move', :action => 'index' | ||
map.copy_move_copy_move 'admin/pages/copy_move/:id/copy_move', :controller => 'copy_move', :action => 'copy_move' | ||
map.with_options(:controller => "admin/pages") do |cm| | ||
cm.copy_admin_page 'admin/pages/:id/copy', :action => 'copy' | ||
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 | ||
before_filter do |c| | ||
c.include_stylesheet 'admin/copy_move' | ||
end | ||
include CopyMove::Controller | ||
helper :copy_move | ||
end | ||
|
||
Page.class_eval { include CopyMove } | ||
admin.page.index.add :sitemap_head, 'copy_move_extra_th' | ||
admin.page.index.add :node, 'copy_move_extra_td', :after => "add_child_column" | ||
end | ||
|
||
def deactivate | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module CopyMove | ||
class CircularHierarchy < ActiveRecord::ActiveRecordError | ||
def initialize(record) | ||
@record = record | ||
super("Page #{record.title} cannot be made a descendant of itself.") | ||
end | ||
end | ||
|
||
def new_slug_and_title_under(parent) | ||
test_page = self.clone | ||
test_page.parent = parent | ||
until test_page.valid? | ||
index = (index || 0) + 1 | ||
test_page.title = "#{title} (Copy#{' '+index if index > 1})" | ||
test_page.slug = "#{slug}-#{index}" | ||
end | ||
{:slug => test_page.slug, :title => test_page.title} | ||
end | ||
|
||
def move_under(parent) | ||
raise CircularHierarchy.new(self) if parent == self || parent.ancestors.include?(self) | ||
update_attributes!(:parent_id => parent.id) | ||
end | ||
|
||
def copy_to(parent, status = nil) | ||
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 | ||
end | ||
new_page.status_id = status || new_page.status_id | ||
new_page.save! | ||
end | ||
end | ||
|
||
def copy_with_children_to(parent, status = nil) | ||
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) | ||
copy_to(parent, status).tap do |new_page| | ||
children.each {|child| child.copy_tree_to(new_page, status) } | ||
end | ||
end | ||
|
||
private | ||
def copiable_attributes | ||
self.attributes.dup.delete_if {|k,v| [:id, :parent_id].include?(k.to_sym) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module CopyMove | ||
module Controller | ||
def self.included(base) | ||
base.class_eval do | ||
before_filter do |c| | ||
c.include_stylesheet 'admin/copy_move' | ||
end | ||
before_filter :load_model, :only => [:copy, :copy_children, :copy_tree, :move] | ||
before_filter :load_parent, :only => [:copy, :copy_children, :copy_tree, :move] | ||
end | ||
end | ||
|
||
def copy | ||
@new_page = @page.copy_to(@parent) | ||
respond_to do |wants| | ||
wants.html do | ||
flash[:notice] = "A copy of <strong>#{@page.title}</strong> was created under <strong>#{@parent.title}</strong>." | ||
redirect_to admin_pages_url | ||
end | ||
wants.xml do | ||
render :xml => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
wants.json do | ||
render :json => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
end | ||
end | ||
|
||
def copy_children | ||
@new_page = @page.copy_with_children_to(@parent) | ||
respond_to do |wants| | ||
wants.html do | ||
flash[:notice] = "Copies of <strong>#{@page.title}</strong> and its immediate children were created under <strong>#{@parent.title}</strong>." | ||
redirect_to admin_pages_url | ||
end | ||
wants.xml do | ||
render :xml => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
wants.json do | ||
render :json => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
end | ||
end | ||
|
||
def copy_tree | ||
@new_page = @page.copy_tree_to(@parent) | ||
respond_to do |wants| | ||
wants.html do | ||
flash[:notice] = "Copies of <strong>#{@page.title}</strong> and all its descendants were created under <strong>#{@parent.title}</strong>." | ||
redirect_to admin_pages_url | ||
end | ||
wants.xml do | ||
render :xml => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
wants.json do | ||
render :json => @new_page, :status => :created, :location => admin_page_url(@new_page, :format => params[:format]) | ||
end | ||
end | ||
end | ||
|
||
def move | ||
@page.move_under(@parent) | ||
respond_to do |wants| | ||
wants.html do | ||
flash[:notice] = "Page <strong>#{@page.title}</strong> and all its descendants were moved under <strong>#{@parent.title}</strong>." | ||
redirect_to admin_pages_url | ||
end | ||
wants.xml do | ||
render :xml => @page | ||
end | ||
wants.json do | ||
render :json => @page | ||
end | ||
end | ||
rescue CopyMove::CircularHierarchy => e | ||
respond_to do |wants| | ||
wants.html do | ||
flash[:error] = e.message | ||
redirect_to admin_pages_url | ||
end | ||
wants.xml { render :xml => e, :status => :conflict } | ||
wants.json { render :json => e, :status => :conflict } | ||
end | ||
end | ||
|
||
private | ||
def load_parent | ||
@parent = Page.find(params[:parent_id]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.