From d48adb741c5c30ba3f2d3c039a7e342b43add352 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 4 Jul 2018 10:42:21 -0400 Subject: Added blog drafts An unpublished post is not viewable unless you are logged in. The "Create record" field is disabled for unpublished posts, though this is only in JavaScript and the backend will not disallow creating records for unpublished posts if forced to. Unpublishing a post does not destroy records for that post. This only applies to blog posts, currently; streams and stream updates cannot be drafted. Unpublished posts still require titles and slugs. There is no autosaving functionality yet. refs #1 --- app/assets/javascripts/admin/records.coffee | 24 ++++++++++++++++++++---- app/assets/stylesheets/admin/layout.scss | 6 ++++++ app/controllers/admin/blogs_controller.rb | 8 ++++++-- app/controllers/blogs_controller.rb | 3 +++ app/models/blog.rb | 21 +++++++++++++++++++++ app/views/admin/blogs/_form.html.haml | 4 ++++ app/views/admin/blogs/drafts.html.haml | 10 ++++++++++ app/views/admin/blogs/index.html.haml | 4 ++-- app/views/blogs/show.html.haml | 2 +- app/views/layouts/admin.html.haml | 1 + config/routes.rb | 6 +++++- db/migrate/20180704125527_add_blog_drafts.rb | 17 +++++++++++++++++ db/schema.rb | 4 +++- 13 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 app/views/admin/blogs/drafts.html.haml create mode 100644 db/migrate/20180704125527_add_blog_drafts.rb diff --git a/app/assets/javascripts/admin/records.coffee b/app/assets/javascripts/admin/records.coffee index 69cd471..3a1ed51 100644 --- a/app/assets/javascripts/admin/records.coffee +++ b/app/assets/javascripts/admin/records.coffee @@ -1,9 +1,25 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ + +create_record_toggle = (checked) -> + if checked + $(".record-description-field").show() + else + $(".record-description-field").hide() + +published_field_toggle = (checked) -> + if checked + $(".should-create-record-field input[type=checkbox]").prop("disabled", false) + else + $(".should-create-record-field input[type=checkbox]").prop("disabled", true) + $(".should-create-record-field input[type=checkbox]").prop("checked", false) + create_record_toggle(false) + $(document).on "turbolinks:load", -> + if $(".published-field").length > 0 + published_field_toggle($(".published-field input[type=checkbox]").prop("checked")) $(".should-create-record-field input[type=checkbox]").change -> - if $(".should-create-record-field input[type=checkbox]").prop("checked") - $(".record-description-field").show() - else - $(".record-description-field").hide() + create_record_toggle($(this).prop("checked")) + $(".published-field input[type=checkbox]").change -> + published_field_toggle($(this).prop("checked")) diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index 6645709..f68cf6a 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -190,6 +190,12 @@ body { } } +.published-field { + label { + font-size: .75em; + } +} + .record-description-field { display: none; margin-top: 1em; diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index fa46ab8..9706df3 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -2,7 +2,11 @@ class Admin::BlogsController < Admin::AdminController before_action :set_section def index - @blogs = Blog.order(created_at: :desc) + @blogs = Blog.where(published: true).order(published_at: :desc) + end + + def drafts + @blogs = Blog.where(published: false).order(updated_at: :desc) end def new @@ -42,7 +46,7 @@ class Admin::BlogsController < Admin::AdminController private def blog_params - params.require(:blog).permit(:title, :body, :slug, records_attributes: [:description, :_destroy]) + params.require(:blog).permit(:title, :body, :slug, :published, records_attributes: [:description, :_destroy]) end def set_section diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 5e72601..f31287b 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -2,6 +2,9 @@ class BlogsController < ApplicationController def show @blog = Blog.find_by_slug(params[:slug]) + + raise ActiveRecord::RecordNotFound unless @blog + raise ActiveRecord::RecordNotFound unless @blog.published or user_signed_in? end end diff --git a/app/models/blog.rb b/app/models/blog.rb index 1ace11b..495c6eb 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -6,7 +6,28 @@ class Blog < ApplicationRecord accepts_nested_attributes_for :records, allow_destroy: true + before_save :set_published_at + def path "/says/#{slug}" end + + def posted_at + if published + published_at + else + updated_at + end + end + + private + def set_published_at + if self.published + if self.published_at.blank? + self.published_at = DateTime.now + end + else + self.published_at = nil + end + end end diff --git a/app/views/admin/blogs/_form.html.haml b/app/views/admin/blogs/_form.html.haml index 4a1c410..2066aed 100644 --- a/app/views/admin/blogs/_form.html.haml +++ b/app/views/admin/blogs/_form.html.haml @@ -15,6 +15,10 @@ %ul - f.object.errors.full_messages.each do |error| %li= error + .details-module + .published-field + = f.check_box :published + = f.label :published .details-module = f.fields_for :records, Record.new do |builder| .should-create-record-field diff --git a/app/views/admin/blogs/drafts.html.haml b/app/views/admin/blogs/drafts.html.haml new file mode 100644 index 0000000..91d3214 --- /dev/null +++ b/app/views/admin/blogs/drafts.html.haml @@ -0,0 +1,10 @@ +%table#entries + %tr + %th Title + %th Last updated + %th + - @blogs.each do |blog| + %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) diff --git a/app/views/admin/blogs/index.html.haml b/app/views/admin/blogs/index.html.haml index 448617a..c5db4f1 100644 --- a/app/views/admin/blogs/index.html.haml +++ b/app/views/admin/blogs/index.html.haml @@ -1,10 +1,10 @@ %table#entries %tr %th Title - %th Date created + %th Date published %th - @blogs.each do |blog| %tr{ class: cycle("even", "odd") } %td= blog.title - %td= blog.created_at.strftime("%B %d, %Y, %l:%M%P") + %td= blog.published_at.strftime("%B %d, %Y, %l:%M%P") %td= link_to "Edit", edit_admin_blog_url(blog) diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml index c44d3f4..48c07dc 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.created_at.strftime("%B #{@blog.created_at.day.ordinalize}, %Y at %-I:%M:%S%P") + %time= @blog.posted_at.strftime("%B #{@blog.posted_at.day.ordinalize}, %Y at %-I:%M:%S%P") diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index 68bbd96..cb48c50 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -24,6 +24,7 @@ = link_to "Blogs", admin_blogs_url, class: "major-link" %ul.minors %li.minor= link_to "New blog", new_admin_blog_url + %li.minor= link_to "Drafts", drafts_admin_blogs_url %li{major_sidebar_attrs("streams")} = link_to "Streams", admin_streams_url, class: "major-link" %ul.minors diff --git a/config/routes.rb b/config/routes.rb index 7369514..37e7f90 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,11 @@ Rails.application.routes.draw do namespace :admin do get '/', to: 'dashboard#index' - resources :blogs, except: [:show] + resources :blogs, except: [:show] do + collection do + get 'drafts' + end + end resources :streams, except: [:show] do resources :updates, except: [:index, :show] diff --git a/db/migrate/20180704125527_add_blog_drafts.rb b/db/migrate/20180704125527_add_blog_drafts.rb new file mode 100644 index 0000000..07c5078 --- /dev/null +++ b/db/migrate/20180704125527_add_blog_drafts.rb @@ -0,0 +1,17 @@ +class AddBlogDrafts < ActiveRecord::Migration[5.1] + def change + add_column :blogs, :published, :boolean, null: false, default: false + add_column :blogs, :published_at, :datetime, null: true + + reversible do |dir| + dir.up do + Blog.all.each do |blog| + blog.published = true + blog.published_at = blog.created_at + + blog.save! + end + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2ca0b58..d885500 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180702221133) do +ActiveRecord::Schema.define(version: 20180704125527) do create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "title" @@ -18,6 +18,8 @@ ActiveRecord::Schema.define(version: 20180702221133) do t.string "slug", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "published", default: false, null: false + t.datetime "published_at" end create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| -- cgit 1.4.1