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
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
belongs_to :user, foreign_key: 'updated_by'
|
2009-05-15 17:32:45 +02:00
|
|
|
|
2020-08-14 14:18:53 +02:00
|
|
|
acts_as_versioned version_column: :lock_version
|
2023-05-12 13:01:12 +02:00
|
|
|
self.non_versioned_columns += %w[permalink created_at title]
|
2012-11-24 16:41:34 +01:00
|
|
|
|
2023-05-12 13:01:12 +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
|
|
|
|
2023-05-12 13:01:12 +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
|
|
|
|
2023-05-12 13:01:12 +02: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)
|
2023-05-12 13:01:12 +02:00
|
|
|
title.gsub(%r{[/.,;@\s]}, '_').gsub(/["']/, '')
|
2009-06-11 22:40:56 +02:00
|
|
|
end
|
|
|
|
|
2011-03-09 13:36:02 +01:00
|
|
|
def homepage?
|
2023-05-12 13:01:12 +02:00
|
|
|
permalink == 'Home'
|
2011-03-09 13:36:02 +01:00
|
|
|
end
|
|
|
|
|
2017-10-20 12:10:54 +02:00
|
|
|
def self.dashboard
|
2023-05-12 13:01:12 +02:00
|
|
|
where(permalink: 'Dashboard').first
|
2017-10-20 12:10:54 +02:00
|
|
|
end
|
|
|
|
|
2015-01-19 23:41:56 +01:00
|
|
|
def self.public_front_page
|
2023-05-12 13:01:12 +02:00
|
|
|
where(permalink: 'Public_frontpage').first
|
2015-01-19 23:41:56 +01:00
|
|
|
end
|
|
|
|
|
2020-03-06 11:46:39 +01:00
|
|
|
def self.welcome_mail
|
2023-05-12 13:01:12 +02:00
|
|
|
where(permalink: 'Welcome_mail').first
|
2020-03-06 11:46:39 +01:00
|
|
|
end
|
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
def set_permalink
|
2023-05-12 13:01:12 +02:00
|
|
|
return if title.blank?
|
|
|
|
|
|
|
|
self.permalink = Page.count == 0 ? 'Home' : Page.permalink(title)
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
2017-10-20 12:08:52 +02:00
|
|
|
|
2014-01-04 20:12:01 +01:00
|
|
|
def diff
|
|
|
|
current = versions.latest
|
2023-05-12 13:01:12 +02:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
when /^\+/ then o += "#{line.chomp}<br />" unless line.chomp == '+'
|
|
|
|
when /^-/ then o += "#{line.chomp}<br />" unless line.chomp == '-'
|
2014-01-04 20:12:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
o
|
|
|
|
else
|
|
|
|
current.body
|
|
|
|
end
|
|
|
|
end
|
2009-06-11 23:51:26 +02:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def update_permalink
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless changed.include?('title')
|
|
|
|
|
|
|
|
set_permalink
|
|
|
|
self.old_title = changes['title'].first # Save title for creating redirect
|
2009-06-11 23:51:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_redirect
|
2023-05-12 13:01:12 +02:00
|
|
|
return if old_title.blank?
|
|
|
|
|
|
|
|
Page.create redirect: id,
|
|
|
|
title: old_title,
|
|
|
|
body: I18n.t('model.page.redirect', title: title),
|
|
|
|
permalink: Page.permalink(old_title),
|
|
|
|
updated_by: updated_by
|
2009-06-11 23:51:26 +02:00
|
|
|
end
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|