Skip to content

Commit

Permalink
Big spike of refactoring.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Franz <andrea@gravityblast.com>
  • Loading branch information
seancribbs authored and gravityblast committed Aug 11, 2009
1 parent 6e33a95 commit c12feaa
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 33 deletions.
19 changes: 9 additions & 10 deletions app/helpers/copy_move_helper.rb
Expand Up @@ -4,17 +4,16 @@ def page_parent_select_tag
list = homes.inject([]) do |l, home|
l.concat build_tree(home, [])
end
options = options_for_select list, (@page.parent ? @page.parent.id : nil)
select_tag 'parent', options
select_tag 'parent_id', options, options_for_select(list)
end

def build_tree page, list, level = 0
label = "#{'-'*level}#{page.title}"
id = page.id
list << [label, id]
page.children.each do |p|
build_tree p, list, level + 1
end
list
def build_tree(page, list, level = 0)
label = "#{'-'*level}#{page.title}"
id = page.id
list << [label, id]
page.children.each do |p|
build_tree p, list, level + 1
end
list
end
end
21 changes: 10 additions & 11 deletions copy_move_extension.rb
@@ -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
51 changes: 51 additions & 0 deletions lib/copy_move.rb
@@ -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
91 changes: 91 additions & 0 deletions lib/copy_move/controller.rb
@@ -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
19 changes: 7 additions & 12 deletions lib/tasks/copy_move_extension_tasks.rake
Expand Up @@ -2,7 +2,7 @@ namespace :radiant do
namespace :extensions do
namespace :copy_move do

desc "Runs the migration of the CopyMove extension"
desc "Runs the migration of the Copy Move extension"
task :migrate => :environment do
require 'radiant/extension_migrator'
if ENV["VERSION"]
Expand All @@ -12,22 +12,17 @@ namespace :radiant do
end
end

desc "Copies public assets of the CopyMove extension to the instance public/ directory."
desc "Copies public assets of the Copy Move to the instance public/ directory."
task :update => :environment do
is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
puts "Copying assets from CopyMoveExtension"
Dir[CopyMoveExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
path = file.sub(CopyMoveExtension.root, '')
directory = File.dirname(path)
puts "Copying #{path}..."
mkdir_p RAILS_ROOT + directory
cp file, RAILS_ROOT + path
mkdir_p RAILS_ROOT + directory, :verbose => false
cp file, RAILS_ROOT + path, :verbose => false
end
end

desc "Migrates and copies files in public/admin"
task :install => [:environment, :migrate, :update] do
end

end
end
end
end
end

0 comments on commit c12feaa

Please sign in to comment.