foodsoft/app/models/page.rb

55 lines
1.4 KiB
Ruby
Raw Normal View History

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
self.non_versioned_columns += ['permalink', 'created_at', 'title']
acts_as_tree :order => "title"
attr_accessor :old_title # Save title to create redirect page when editing title
2009-05-15 17:32:45 +02:00
validates_presence_of :title, :body
validates_uniqueness_of :permalink, :title
2011-05-11 11:17:02 +02:00
before_validation :set_permalink, :on => :create
before_validation :update_permalink, :on => :update
after_update :create_redirect
scope :non_redirected, :conditions => {:redirect => nil}
scope :no_parent, :conditions => {:parent_id => nil}
def self.permalink(title)
title.gsub(/[\/\.,;@\s]/, "_").gsub(/[\"\']/, "")
end
def homepage?
permalink == "Home"
end
def set_permalink
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