Create a redirect when editing the title of a page.

This commit is contained in:
Benjamin Meichsner 2009-06-11 23:51:26 +02:00
parent 434fc4b2ff
commit 55729e20f9
6 changed files with 36 additions and 10 deletions

View file

@ -1,7 +1,7 @@
class PagesController < ApplicationController class PagesController < ApplicationController
def index def index
@page = Page.find_by_permalink "home" @page = Page.find_by_permalink "Home"
if @page if @page
render :action => 'show' render :action => 'show'
@ -15,6 +15,9 @@ class PagesController < ApplicationController
if @page.nil? if @page.nil?
redirect_to new_page_path(:title => params[:permalink]) redirect_to new_page_path(:title => params[:permalink])
elsif @page.redirect?
flash[:notice] = "Weitergeleitet von #{@page.title} ..."
redirect_to wiki_page_path(Page.find(@page.redirect).permalink)
end end
end end
@ -79,7 +82,7 @@ class PagesController < ApplicationController
end end
def all def all
@pages = Page.all :order => 'created_at' @pages = Page.all :order => 'created_at', :conditions => {:redirect => nil}
end end
def version def version

View file

@ -38,7 +38,6 @@ module PagesHelper
toc << "#" * number + " #{name}\n" toc << "#" * number + " #{name}\n"
end end
logger.debug("TOC: #{toc}")
toc = Wikitext::Parser.new.parse toc toc = Wikitext::Parser.new.parse toc
toc.gsub(/<li>([^<>\n]*)/) do toc.gsub(/<li>([^<>\n]*)/) do

View file

@ -18,20 +18,43 @@ class Page < ActiveRecord::Base
belongs_to :user, :foreign_key => 'updated_by' belongs_to :user, :foreign_key => 'updated_by'
acts_as_versioned :version_column => :lock_version acts_as_versioned :version_column => :lock_version
self.non_versioned_columns += ['permalink', 'created_at'] self.non_versioned_columns += ['permalink', 'created_at', 'title']
attr_accessor :old_title # Save title to create redirect page when editing title
validates_presence_of :title, :body validates_presence_of :title, :body
validates_uniqueness_of :permalink validates_uniqueness_of :permalink, :title
before_validation_on_create :set_permalink before_validation_on_create :set_permalink
before_validation_on_update :update_permalink
after_update :create_redirect
def self.permalink(title) def self.permalink(title)
Wikitext::Parser.new.parse "[[#{title}]]" title.gsub(" ", "_")
end end
def set_permalink def set_permalink
unless self.permalink.blank? unless title.blank?
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title) self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
end end
end end
protected
def update_permalink
if changed.include?("title")
set_permalink
self.old_title = changes["title"].first # Save title for creating redirect
end
end
def create_redirect
unless old_title.blank?
Page.create :redirect => id,
:title => old_title,
:body => "Weiterleitung auf [[#{title}]]..",
:permalink => Page.permalink(old_title),
:updated_by => updated_by
end
end
end end

View file

@ -1,4 +1,4 @@
- title @version.title - title @page.title
#page-versions{:style => "float:right"} #page-versions{:style => "float:right"}
%b= "Version vom #{format_datetime @version.updated_at}" %b= "Version vom #{format_datetime @version.updated_at}"

View file

@ -1,7 +1,7 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get} map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get}
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[\w\._\-\%\d]+/ map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/
map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'home' map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home'
map.logout '/logout', :controller => 'login', :action => 'logout' map.logout '/logout', :controller => 'login', :action => 'logout'
map.my_profile '/home/profile', :controller => 'home', :action => 'profile' map.my_profile '/home/profile', :controller => 'home', :action => 'profile'

View file

@ -6,6 +6,7 @@ class CreatePages < ActiveRecord::Migration
t.string :permalink t.string :permalink
t.integer :lock_version, :default => 0 t.integer :lock_version, :default => 0
t.integer :updated_by t.integer :updated_by
t.integer :redirect
t.timestamps t.timestamps
end end