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/assets/stylesheets/admin/layout.scss | 10 ++++++++++ app/controllers/admin/blogs_controller.rb | 10 ++++++++++ app/controllers/blogs_controller.rb | 2 +- app/models/blog.rb | 18 +++++++++--------- app/views/admin/blogs/_form.html.haml | 6 ++++++ app/views/admin/blogs/drafts.html.haml | 5 ++++- app/views/admin/blogs/index.html.haml | 5 ++++- app/views/admin/blogs/show.html.haml | 5 +++++ app/views/blogs/show.html.haml | 2 +- 9 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 app/views/admin/blogs/show.html.haml (limited to 'app') diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index f68cf6a..b825c25 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -227,6 +227,16 @@ body { } } +#entry-preview-link { + a { + text-decoration: none; + + &:hover { + text-decoration: underline; + } + } +} + #flash { display: inline-block; color: black; diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index 9706df3..1035c12 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -9,6 +9,16 @@ class Admin::BlogsController < Admin::AdminController @blogs = Blog.where(published: false).order(updated_at: :desc) end + def show + @blog = Blog.find(params[:id]) + + if @blog.published + redirect_to blog_url(@blog.slug) + else + render layout: "application" + end + end + def new @blog = Blog.new end diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index f31287b..8ee472e 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -4,7 +4,7 @@ class BlogsController < ApplicationController @blog = Blog.find_by_slug(params[:slug]) raise ActiveRecord::RecordNotFound unless @blog - raise ActiveRecord::RecordNotFound unless @blog.published or user_signed_in? + raise ActiveRecord::RecordNotFound unless @blog.published end end 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? diff --git a/app/views/admin/blogs/_form.html.haml b/app/views/admin/blogs/_form.html.haml index 2066aed..12f7a82 100644 --- a/app/views/admin/blogs/_form.html.haml +++ b/app/views/admin/blogs/_form.html.haml @@ -15,6 +15,12 @@ %ul - f.object.errors.full_messages.each do |error| %li= error + - unless f.object.new_record? + #entry-preview-link.details-module + - if f.object.published + = link_to "View post", blog_url(f.object.slug_was), target: "entry-preview" + - else + = link_to "Preview post", admin_blog_url(f.object), target: "entry-preview" .details-module .published-field = f.check_box :published diff --git a/app/views/admin/blogs/drafts.html.haml b/app/views/admin/blogs/drafts.html.haml index 91d3214..8f2d369 100644 --- a/app/views/admin/blogs/drafts.html.haml +++ b/app/views/admin/blogs/drafts.html.haml @@ -7,4 +7,7 @@ %tr{ class: cycle("even", "odd") } %td= blog.title %td= blog.updated_at.strftime("%B %d, %Y, %l:%M%P") - %td= link_to "Edit", edit_admin_blog_url(blog) + %td + %ul.admin-actions + %li= link_to "Preview", admin_blog_url(blog) + %li= link_to "Edit", edit_admin_blog_url(blog) diff --git a/app/views/admin/blogs/index.html.haml b/app/views/admin/blogs/index.html.haml index c5db4f1..427d922 100644 --- a/app/views/admin/blogs/index.html.haml +++ b/app/views/admin/blogs/index.html.haml @@ -7,4 +7,7 @@ %tr{ class: cycle("even", "odd") } %td= blog.title %td= blog.published_at.strftime("%B %d, %Y, %l:%M%P") - %td= link_to "Edit", edit_admin_blog_url(blog) + %td + %ul.admin-actions + %li= link_to "View", blog_url(blog.slug) + %li= link_to "Edit", edit_admin_blog_url(blog) diff --git a/app/views/admin/blogs/show.html.haml b/app/views/admin/blogs/show.html.haml new file mode 100644 index 0000000..7875cab --- /dev/null +++ b/app/views/admin/blogs/show.html.haml @@ -0,0 +1,5 @@ += render partial: "blogs/blog", object: @blog +%footer#blog-footer + This draft was last updated on + = succeed "." do + %time= @blog.updated_at.strftime("%B #{@blog.updated_at.day.ordinalize}, %Y at %-I:%M:%S%P") diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml index 48c07dc..8ab4523 100644 --- a/app/views/blogs/show.html.haml +++ b/app/views/blogs/show.html.haml @@ -3,4 +3,4 @@ %footer#blog-footer This entry was posted on = succeed "." do - %time= @blog.posted_at.strftime("%B #{@blog.posted_at.day.ordinalize}, %Y at %-I:%M:%S%P") + %time= @blog.published_at.strftime("%B #{@blog.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") -- cgit 1.4.1