discourse/app/models/published_page.rb
Jarek Radosz 17cf300b71
DEV: Use more specific error responses (#9472)
* DEV: Use `render_json_error` (Adds specs for Admin::GroupsController)
* DEV: Use a specific error on blank category slug (Fixes a `render_json_error` warning)
* DEV: Use a specific error on reviewable claim conflict (Fixes a `render_json_error` warning)
* DEV: Use specific errors in Admin::UsersController (Fixes `render_json_error` warnings)
* FIX: PublishedPages error responses
* FIX: TopicsController error responses (There was an issue of two separate `Topic` instances for the same record. This makes sure there's only one up-to-date instance.)
2020-04-21 03:50:20 +02:00

49 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)
pp = nil
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