From e11dedec034c4180985adf4a9f176b07121f0a41 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 4 Jul 2018 11:46:13 -0400 Subject: Blog drafts no longer require slugs They do still technically require titles, but the engine will fill in "Untitled draft" if it is left blank. Unpublished posts can be viewed at a different URL than published posts would be. Quick links to view published and unpublished posts have been added to the admin panel. refs #1 --- app/models/blog.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'app/models/blog.rb') diff --git a/app/models/blog.rb b/app/models/blog.rb index 495c6eb..322a808 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -1,26 +1,26 @@ class Blog < ApplicationRecord has_many :records, as: :recordable, inverse_of: :recordable - validates :title, :body, presence: true - validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ + validates :title, presence: true + validates :body, presence: true, if: :published + validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published accepts_nested_attributes_for :records, allow_destroy: true + before_validation :set_draft_title before_save :set_published_at def path "/says/#{slug}" end - def posted_at - if published - published_at - else - updated_at + private + def set_draft_title + if self.title.blank? and not self.published + self.title = "Untitled draft" + end end - end - private def set_published_at if self.published if self.published_at.blank? -- cgit 1.4.1