discourse/app/models/published_page.rb
Robin Ward e1f8014acd
FEATURE: Support for publishing topics as pages (#9364)
If the feature is enabled, staff members can construct a URL and publish a
topic for others to browse without the regular Discourse chrome.

This is useful if you want to use Discourse like a CMS and publish
topics as articles, which can then be embedded into other systems.
2020-04-08 12:52:36 -04:00

47 lines
1.0 KiB
Ruby

# frozen_string_literal: true
class PublishedPage < ActiveRecord::Base
belongs_to :topic
validates_presence_of :slug
validates_uniqueness_of :slug, :topic_id
validate :slug_format
def slug_format
if slug !~ /^[a-zA-Z\-\_0-9]+$/
errors.add(:slug, I18n.t("publish_page.slug_errors.invalid"))
elsif ["check-slug", "by-topic"].include?(slug)
errors.add(:slug, I18n.t("publish_page.slug_errors.unavailable"))
end
end
def path
"/pub/#{slug}"
end
def url
"#{Discourse.base_url}#{path}"
end
def self.publish!(publisher, topic, slug)
transaction do
pp = find_or_initialize_by(topic: topic)
pp.slug = slug.strip
if pp.save
StaffActionLogger.new(publisher).log_published_page(topic.id, slug)
return [true, pp]
end
end
[false, pp]
end
def self.unpublish!(publisher, topic)
if pp = PublishedPage.find_by(topic_id: topic.id)
pp.destroy!
StaffActionLogger.new(publisher).log_unpublished_page(topic.id, pp.slug)
end
end
end