Skip to content

Instantly share code, notes, and snippets.

@crux
Created February 13, 2013 13:54
Show Gist options
  • Save crux/4944750 to your computer and use it in GitHub Desktop.
Save crux/4944750 to your computer and use it in GitHub Desktop.
doc_pu plugin page sorting from the redmine console
# sorting the doc_pu wiki pages in a redmine. The doc_pu plugin contains an
# option to manually reoorder pages but this is only one by and is awfull to
# use for lots of references.
#
# 'install' this code in the redmine dir:
# $ cat > lib/ac/doc_pu_sorter.rb
#
# execute with the right user(redmine) in the production mode on the server
# from the command line:
#
# $ sudo -u redmine RAILS_ENV=production ./script/runner \
# Ac::DocPuSorter.sort 18
#
# where '18' is the doc_pu id of the page you want to reorder.
#
# NOTE:
# 1. Ofcourse this should be included as an feature of the doc_pu plugin
# itself but there is simply no time for this now.
#
# 2. Alphanumeric sorting is limited but in our case its is perfectly fine
# because the page titles explicity contains a numbering scheme which gives us
# exaclty the order we intended.
#
module Ac
module DocPuSorter
extend self
def doit
puts "strike!"
end
def list
docs = DocPuDocument.all
docs.each do |doc|
puts "#{doc.id} :: #{doc.name}"
end
end
def sort
doc = DocPuDocument.find(Integer(ARGV.shift))
puts "#{doc.id} :: #{doc.name}, #{doc.wiki_pages.count} pages"
# DocPuDocument.find(18).wiki_pages.first.wiki_page.title
print "--- pages in order which is currently saved"
by_order = doc.wiki_pages.sort do |a,b|
a.wiki_page_order <=> b.wiki_page_order
end
by_order.each do |wp|
puts "#{wp.wiki_page_order}.\t#{wp.wiki_page.title}"
end
print "--- pages sorted by title"
by_title = doc.wiki_pages.sort do |a,b|
a.wiki_page.title <=> b.wiki_page.title
end
by_title.each_with_index do |wp, idx|
puts "#{wp.wiki_page_order} | #{idx}.\t#{wp.wiki_page.title}"
wp.wiki_page_order = idx
wp.save
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment