diff options
-rw-r--r-- | app/assets/javascripts/admin/records.coffee | 24 | ||||
-rw-r--r-- | app/assets/stylesheets/admin/layout.scss | 6 | ||||
-rw-r--r-- | app/controllers/admin/blogs_controller.rb | 8 | ||||
-rw-r--r-- | app/controllers/blogs_controller.rb | 3 | ||||
-rw-r--r-- | app/models/blog.rb | 21 | ||||
-rw-r--r-- | app/views/admin/blogs/_form.html.haml | 4 | ||||
-rw-r--r-- | app/views/admin/blogs/drafts.html.haml | 10 | ||||
-rw-r--r-- | app/views/admin/blogs/index.html.haml | 4 | ||||
-rw-r--r-- | app/views/blogs/show.html.haml | 2 | ||||
-rw-r--r-- | app/views/layouts/admin.html.haml | 1 | ||||
-rw-r--r-- | config/routes.rb | 6 | ||||
-rw-r--r-- | db/migrate/20180704125527_add_blog_drafts.rb | 17 | ||||
-rw-r--r-- | db/schema.rb | 4 |
13 files changed, 99 insertions, 11 deletions
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 @@ | |||
1 | # Place all the behaviors and hooks related to the matching controller here. | 1 | # Place all the behaviors and hooks related to the matching controller here. |
2 | # All this logic will automatically be available in application.js. | 2 | # All this logic will automatically be available in application.js. |
3 | # You can use CoffeeScript in this file: http://coffeescript.org/ | 3 | # You can use CoffeeScript in this file: http://coffeescript.org/ |
4 | |||
5 | create_record_toggle = (checked) -> | ||
6 | if checked | ||
7 | $(".record-description-field").show() | ||
8 | else | ||
9 | $(".record-description-field").hide() | ||
10 | |||
11 | published_field_toggle = (checked) -> | ||
12 | if checked | ||
13 | $(".should-create-record-field input[type=checkbox]").prop("disabled", false) | ||
14 | else | ||
15 | $(".should-create-record-field input[type=checkbox]").prop("disabled", true) | ||
16 | $(".should-create-record-field input[type=checkbox]").prop("checked", false) | ||
17 | create_record_toggle(false) | ||
18 | |||
4 | $(document).on "turbolinks:load", -> | 19 | $(document).on "turbolinks:load", -> |
20 | if $(".published-field").length > 0 | ||
21 | published_field_toggle($(".published-field input[type=checkbox]").prop("checked")) | ||
5 | $(".should-create-record-field input[type=checkbox]").change -> | 22 | $(".should-create-record-field input[type=checkbox]").change -> |
6 | if $(".should-create-record-field input[type=checkbox]").prop("checked") | 23 | create_record_toggle($(this).prop("checked")) |
7 | $(".record-description-field").show() | 24 | $(".published-field input[type=checkbox]").change -> |
8 | else | 25 | published_field_toggle($(this).prop("checked")) |
9 | $(".record-description-field").hide() | ||
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 { | |||
190 | } | 190 | } |
191 | } | 191 | } |
192 | 192 | ||
193 | .published-field { | ||
194 | label { | ||
195 | font-size: .75em; | ||
196 | } | ||
197 | } | ||
198 | |||
193 | .record-description-field { | 199 | .record-description-field { |
194 | display: none; | 200 | display: none; |
195 | margin-top: 1em; | 201 | 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 | |||
2 | before_action :set_section | 2 | before_action :set_section |
3 | 3 | ||
4 | def index | 4 | def index |
5 | @blogs = Blog.order(created_at: :desc) | 5 | @blogs = Blog.where(published: true).order(published_at: :desc) |
6 | end | ||
7 | |||
8 | def drafts | ||
9 | @blogs = Blog.where(published: false).order(updated_at: :desc) | ||
6 | end | 10 | end |
7 | 11 | ||
8 | def new | 12 | def new |
@@ -42,7 +46,7 @@ class Admin::BlogsController < Admin::AdminController | |||
42 | private | 46 | private |
43 | 47 | ||
44 | def blog_params | 48 | def blog_params |
45 | params.require(:blog).permit(:title, :body, :slug, records_attributes: [:description, :_destroy]) | 49 | params.require(:blog).permit(:title, :body, :slug, :published, records_attributes: [:description, :_destroy]) |
46 | end | 50 | end |
47 | 51 | ||
48 | def set_section | 52 | 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 | |||
2 | 2 | ||
3 | def show | 3 | def show |
4 | @blog = Blog.find_by_slug(params[:slug]) | 4 | @blog = Blog.find_by_slug(params[:slug]) |
5 | |||
6 | raise ActiveRecord::RecordNotFound unless @blog | ||
7 | raise ActiveRecord::RecordNotFound unless @blog.published or user_signed_in? | ||
5 | end | 8 | end |
6 | 9 | ||
7 | end | 10 | 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 | |||
6 | 6 | ||
7 | accepts_nested_attributes_for :records, allow_destroy: true | 7 | accepts_nested_attributes_for :records, allow_destroy: true |
8 | 8 | ||
9 | before_save :set_published_at | ||
10 | |||
9 | def path | 11 | def path |
10 | "/says/#{slug}" | 12 | "/says/#{slug}" |
11 | end | 13 | end |
14 | |||
15 | def posted_at | ||
16 | if published | ||
17 | published_at | ||
18 | else | ||
19 | updated_at | ||
20 | end | ||
21 | end | ||
22 | |||
23 | private | ||
24 | def set_published_at | ||
25 | if self.published | ||
26 | if self.published_at.blank? | ||
27 | self.published_at = DateTime.now | ||
28 | end | ||
29 | else | ||
30 | self.published_at = nil | ||
31 | end | ||
32 | end | ||
12 | end | 33 | 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 | |||
@@ -16,6 +16,10 @@ | |||
16 | - f.object.errors.full_messages.each do |error| | 16 | - f.object.errors.full_messages.each do |error| |
17 | %li= error | 17 | %li= error |
18 | .details-module | 18 | .details-module |
19 | .published-field | ||
20 | = f.check_box :published | ||
21 | = f.label :published | ||
22 | .details-module | ||
19 | = f.fields_for :records, Record.new do |builder| | 23 | = f.fields_for :records, Record.new do |builder| |
20 | .should-create-record-field | 24 | .should-create-record-field |
21 | = builder.check_box :_destroy, {checked: false}, "0", "1" | 25 | = builder.check_box :_destroy, {checked: false}, "0", "1" |
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 @@ | |||
1 | %table#entries | ||
2 | %tr | ||
3 | %th Title | ||
4 | %th Last updated | ||
5 | %th | ||
6 | - @blogs.each do |blog| | ||
7 | %tr{ class: cycle("even", "odd") } | ||
8 | %td= blog.title | ||
9 | %td= blog.updated_at.strftime("%B %d, %Y, %l:%M%P") | ||
10 | %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 @@ | |||
1 | %table#entries | 1 | %table#entries |
2 | %tr | 2 | %tr |
3 | %th Title | 3 | %th Title |
4 | %th Date created | 4 | %th Date published |
5 | %th | 5 | %th |
6 | - @blogs.each do |blog| | 6 | - @blogs.each do |blog| |
7 | %tr{ class: cycle("even", "odd") } | 7 | %tr{ class: cycle("even", "odd") } |
8 | %td= blog.title | 8 | %td= blog.title |
9 | %td= blog.created_at.strftime("%B %d, %Y, %l:%M%P") | 9 | %td= blog.published_at.strftime("%B %d, %Y, %l:%M%P") |
10 | %td= link_to "Edit", edit_admin_blog_url(blog) | 10 | %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 @@ | |||
3 | %footer#blog-footer | 3 | %footer#blog-footer |
4 | This entry was posted on | 4 | This entry was posted on |
5 | = succeed "." do | 5 | = succeed "." do |
6 | %time= @blog.created_at.strftime("%B #{@blog.created_at.day.ordinalize}, %Y at %-I:%M:%S%P") | 6 | %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 @@ | |||
24 | = link_to "Blogs", admin_blogs_url, class: "major-link" | 24 | = link_to "Blogs", admin_blogs_url, class: "major-link" |
25 | %ul.minors | 25 | %ul.minors |
26 | %li.minor= link_to "New blog", new_admin_blog_url | 26 | %li.minor= link_to "New blog", new_admin_blog_url |
27 | %li.minor= link_to "Drafts", drafts_admin_blogs_url | ||
27 | %li{major_sidebar_attrs("streams")} | 28 | %li{major_sidebar_attrs("streams")} |
28 | = link_to "Streams", admin_streams_url, class: "major-link" | 29 | = link_to "Streams", admin_streams_url, class: "major-link" |
29 | %ul.minors | 30 | %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 | |||
2 | namespace :admin do | 2 | namespace :admin do |
3 | get '/', to: 'dashboard#index' | 3 | get '/', to: 'dashboard#index' |
4 | 4 | ||
5 | resources :blogs, except: [:show] | 5 | resources :blogs, except: [:show] do |
6 | collection do | ||
7 | get 'drafts' | ||
8 | end | ||
9 | end | ||
6 | 10 | ||
7 | resources :streams, except: [:show] do | 11 | resources :streams, except: [:show] do |
8 | resources :updates, except: [:index, :show] | 12 | 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 @@ | |||
1 | class AddBlogDrafts < ActiveRecord::Migration[5.1] | ||
2 | def change | ||
3 | add_column :blogs, :published, :boolean, null: false, default: false | ||
4 | add_column :blogs, :published_at, :datetime, null: true | ||
5 | |||
6 | reversible do |dir| | ||
7 | dir.up do | ||
8 | Blog.all.each do |blog| | ||
9 | blog.published = true | ||
10 | blog.published_at = blog.created_at | ||
11 | |||
12 | blog.save! | ||
13 | end | ||
14 | end | ||
15 | end | ||
16 | end | ||
17 | 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 @@ | |||
10 | # | 10 | # |
11 | # It's strongly recommended that you check this file into your version control system. | 11 | # It's strongly recommended that you check this file into your version control system. |
12 | 12 | ||
13 | ActiveRecord::Schema.define(version: 20180702221133) do | 13 | ActiveRecord::Schema.define(version: 20180704125527) do |
14 | 14 | ||
15 | create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| | 15 | create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| |
16 | t.string "title" | 16 | t.string "title" |
@@ -18,6 +18,8 @@ ActiveRecord::Schema.define(version: 20180702221133) do | |||
18 | t.string "slug", null: false | 18 | t.string "slug", null: false |
19 | t.datetime "created_at", null: false | 19 | t.datetime "created_at", null: false |
20 | t.datetime "updated_at", null: false | 20 | t.datetime "updated_at", null: false |
21 | t.boolean "published", default: false, null: false | ||
22 | t.datetime "published_at" | ||
21 | end | 23 | end |
22 | 24 | ||
23 | create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| | 25 | create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| |