Create a redirect when editing the title of a page.
This commit is contained in:
parent
434fc4b2ff
commit
55729e20f9
6 changed files with 36 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
|||
class PagesController < ApplicationController
|
||||
|
||||
def index
|
||||
@page = Page.find_by_permalink "home"
|
||||
@page = Page.find_by_permalink "Home"
|
||||
|
||||
if @page
|
||||
render :action => 'show'
|
||||
|
@ -15,6 +15,9 @@ class PagesController < ApplicationController
|
|||
|
||||
if @page.nil?
|
||||
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
|
||||
|
||||
|
@ -79,7 +82,7 @@ class PagesController < ApplicationController
|
|||
end
|
||||
|
||||
def all
|
||||
@pages = Page.all :order => 'created_at'
|
||||
@pages = Page.all :order => 'created_at', :conditions => {:redirect => nil}
|
||||
end
|
||||
|
||||
def version
|
||||
|
|
|
@ -38,7 +38,6 @@ module PagesHelper
|
|||
|
||||
toc << "#" * number + " #{name}\n"
|
||||
end
|
||||
logger.debug("TOC: #{toc}")
|
||||
toc = Wikitext::Parser.new.parse toc
|
||||
|
||||
toc.gsub(/<li>([^<>\n]*)/) do
|
||||
|
|
|
@ -18,20 +18,43 @@ class Page < ActiveRecord::Base
|
|||
belongs_to :user, :foreign_key => 'updated_by'
|
||||
|
||||
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_uniqueness_of :permalink
|
||||
validates_uniqueness_of :permalink, :title
|
||||
|
||||
before_validation_on_create :set_permalink
|
||||
before_validation_on_update :update_permalink
|
||||
after_update :create_redirect
|
||||
|
||||
def self.permalink(title)
|
||||
Wikitext::Parser.new.parse "[[#{title}]]"
|
||||
title.gsub(" ", "_")
|
||||
end
|
||||
|
||||
def set_permalink
|
||||
unless self.permalink.blank?
|
||||
unless title.blank?
|
||||
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
|
||||
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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
- title @version.title
|
||||
- title @page.title
|
||||
|
||||
#page-versions{:style => "float:right"}
|
||||
%b= "Version vom #{format_datetime @version.updated_at}"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
ActionController::Routing::Routes.draw do |map|
|
||||
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 "/wiki", :controller => 'pages', :action => 'show', :permalink => 'home'
|
||||
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/
|
||||
map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home'
|
||||
|
||||
map.logout '/logout', :controller => 'login', :action => 'logout'
|
||||
map.my_profile '/home/profile', :controller => 'home', :action => 'profile'
|
||||
|
|
|
@ -6,6 +6,7 @@ class CreatePages < ActiveRecord::Migration
|
|||
t.string :permalink
|
||||
t.integer :lock_version, :default => 0
|
||||
t.integer :updated_by
|
||||
t.integer :redirect
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue