2009-03-25 19:54:04 +01:00
|
|
|
class Page < ActiveRecord::Base
|
|
|
|
|
2009-05-15 17:32:45 +02:00
|
|
|
belongs_to :user, :foreign_key => 'updated_by'
|
|
|
|
|
2009-12-01 20:20:41 +01:00
|
|
|
acts_as_versioned :version_column => :lock_version, :limit => 20
|
2009-06-11 23:51:26 +02:00
|
|
|
self.non_versioned_columns += ['permalink', 'created_at', 'title']
|
2009-08-12 18:41:25 +02:00
|
|
|
acts_as_tree :order => "title"
|
2009-06-11 23:51:26 +02:00
|
|
|
|
|
|
|
attr_accessor :old_title # Save title to create redirect page when editing title
|
2009-05-15 17:32:45 +02:00
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
validates_presence_of :title, :body
|
2009-06-11 23:51:26 +02:00
|
|
|
validates_uniqueness_of :permalink, :title
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2009-05-13 17:32:43 +02:00
|
|
|
before_validation_on_create :set_permalink
|
2009-06-11 23:51:26 +02:00
|
|
|
before_validation_on_update :update_permalink
|
|
|
|
after_update :create_redirect
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2009-09-28 17:22:24 +02:00
|
|
|
named_scope :non_redirected, :conditions => {:redirect => nil}
|
2009-10-05 17:25:19 +02:00
|
|
|
named_scope :no_parent, :conditions => {:parent_id => nil}
|
2011-03-09 13:36:02 +01:00
|
|
|
|
2009-06-11 22:40:56 +02:00
|
|
|
def self.permalink(title)
|
2009-08-11 14:30:35 +02:00
|
|
|
title.gsub(/[\/\.,;@\s]/, "_").gsub(/[\"\']/, "")
|
2009-06-11 22:40:56 +02:00
|
|
|
end
|
|
|
|
|
2011-03-09 13:36:02 +01:00
|
|
|
def homepage?
|
|
|
|
permalink == "Home"
|
|
|
|
end
|
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
def set_permalink
|
2009-06-11 23:51:26 +02:00
|
|
|
unless title.blank?
|
2009-06-11 22:40:56 +02:00
|
|
|
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
end
|
2009-06-11 23:51:26 +02:00
|
|
|
|
|
|
|
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
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: pages
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# title :string(255)
|
|
|
|
# body :text
|
|
|
|
# permalink :string(255)
|
|
|
|
# lock_version :integer default(0)
|
|
|
|
# updated_by :integer
|
|
|
|
# redirect :integer
|
|
|
|
# parent_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
|