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 +- config/routes.rb | 6 +++--- db/migrate/20180704144707_unrequire_blog_slug.rb | 5 +++++ db/schema.rb | 4 ++-- 12 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 app/views/admin/blogs/show.html.haml create mode 100644 db/migrate/20180704144707_unrequire_blog_slug.rb 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") diff --git a/config/routes.rb b/config/routes.rb index 37e7f90..ac5b84d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ Rails.application.routes.draw do namespace :admin do get '/', to: 'dashboard#index' - resources :blogs, except: [:show] do + resources :blogs do collection do get 'drafts' end @@ -22,9 +22,9 @@ Rails.application.routes.draw do root "records#index" - get 'says/:slug', to: 'blogs#show' + get 'says/:slug', to: 'blogs#show', as: :blog - get 'thinks/:slug', to: 'streams#show' + get 'thinks/:slug', to: 'streams#show', as: :stream mount Pokeviewer::Engine => '/poke3' end diff --git a/db/migrate/20180704144707_unrequire_blog_slug.rb b/db/migrate/20180704144707_unrequire_blog_slug.rb new file mode 100644 index 0000000..e9444db --- /dev/null +++ b/db/migrate/20180704144707_unrequire_blog_slug.rb @@ -0,0 +1,5 @@ +class UnrequireBlogSlug < ActiveRecord::Migration[5.1] + def change + change_column_null :blogs, :slug, true + end +end diff --git a/db/schema.rb b/db/schema.rb index d885500..0279d8e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180704125527) do +ActiveRecord::Schema.define(version: 20180704144707) do create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "title" t.text "body" - t.string "slug", null: false + t.string "slug" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "published", default: false, null: false -- cgit 1.4.1