2019-01-13 07:05:54 +01:00
|
|
|
class Page < ApplicationRecord
|
2012-11-24 16:41:34 +01:00
|
|
|
include ActsAsTree
|
2012-11-24 16:49:19 +01:00
|
|
|
|
2009-05-15 17:32:45 +02:00
|
|
|
belongs_to :user, :foreign_key => 'updated_by'
|
|
|
|
|
2012-11-24 16:36:40 +01:00
|
|
|
acts_as_versioned version_column: :lock_version, limit: 20
|
|
|
|
self.non_versioned_columns += %w(permalink created_at title)
|
2012-11-24 16:41:34 +01:00
|
|
|
|
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
|
|
|
|
2011-05-11 11:17:02 +02:00
|
|
|
before_validation :set_permalink, :on => :create
|
|
|
|
before_validation :update_permalink, :on => :update
|
2009-06-11 23:51:26 +02:00
|
|
|
after_update :create_redirect
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :non_redirected, -> { where(:redirect => nil) }
|
|
|
|
scope :no_parent, -> { where(: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
|
|
|
|
|
2017-10-20 12:10:54 +02:00
|
|
|
def self.dashboard
|
|
|
|
where(permalink: "Dashboard").first
|
|
|
|
end
|
|
|
|
|
2015-01-19 23:41:56 +01:00
|
|
|
def self.public_front_page
|
|
|
|
where(permalink: "Public_frontpage").first
|
|
|
|
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
|
2017-10-20 12:08:52 +02:00
|
|
|
|
2014-01-04 20:12:01 +01:00
|
|
|
def diff
|
|
|
|
current = versions.latest
|
|
|
|
old = versions.where(["page_id = ? and lock_version < ?", current.page_id, current.lock_version]).order('lock_version DESC').first
|
2017-10-20 12:08:52 +02:00
|
|
|
|
2014-01-04 20:12:01 +01:00
|
|
|
if old
|
|
|
|
o = ''
|
|
|
|
Diffy::Diff.new(old.body, current.body).each do |line|
|
|
|
|
case line
|
|
|
|
when /^\+/ then o += "#{line.chomp}<br />" unless line.chomp == "+"
|
|
|
|
when /^-/ then o += "#{line.chomp}<br />" unless line.chomp == "-"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o
|
|
|
|
else
|
|
|
|
current.body
|
|
|
|
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,
|
2013-03-22 01:21:44 +01:00
|
|
|
:body => I18n.t('model.page.redirect', :title => title),
|
2009-06-11 23:51:26 +02:00
|
|
|
:permalink => Page.permalink(old_title),
|
|
|
|
:updated_by => updated_by
|
|
|
|
end
|
|
|
|
end
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|