From 65808633a2e0259b54cefb268ebf79418b49e67c Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 5 Nov 2023 20:32:13 -0500 Subject: Added forward and back links to blog posts --- app/assets/stylesheets/main/entries.scss | 24 ++++++++++++++++++++++++ app/controllers/blogs_controller.rb | 3 +++ app/models/blog.rb | 8 ++++++++ app/views/blogs/show.html.haml | 6 +++++- 4 files changed, 40 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/assets/stylesheets/main/entries.scss b/app/assets/stylesheets/main/entries.scss index 3b7215c..fd03de9 100644 --- a/app/assets/stylesheets/main/entries.scss +++ b/app/assets/stylesheets/main/entries.scss @@ -426,3 +426,27 @@ text-decoration: none; } } + +.back-post, .next-post { + margin-left: 1em; + margin-bottom: 1em; + margin-right: 1em; + + a, a:visited { + color: #555d66; + text-decoration: none; + font-size: .75em; + } + + a:hover { + text-decoration: underline; + } +} + +.back-post { + float: left; +} + +.next-post { + float: right; +} diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 2f9df49..033d6bb 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -24,6 +24,9 @@ class BlogsController < ApplicationController raise ActiveRecord::RecordNotFound unless @blog raise ActiveRecord::RecordNotFound unless @blog.published + @prev = @blog.prev + @next = @blog.next + body = Redcarpet::Markdown.new(Redcarpet::Render::StripDown).render(@blog.body) set_meta_tags(og: { diff --git a/app/models/blog.rb b/app/models/blog.rb index 03643bf..8599bcd 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -48,6 +48,14 @@ class Blog < ApplicationRecord end end + def prev + Blog.where(published: true).where("published_at < ?", published_at).order(published_at: :desc).first + end + + def next + Blog.where(published: true).where("published_at > ?", published_at).order(published_at: :asc).first + end + private def set_draft_title if self.title.blank? and not self.published diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml index 0c549ac..7558257 100644 --- a/app/views/blogs/show.html.haml +++ b/app/views/blogs/show.html.haml @@ -1,4 +1,8 @@ - title @blog.title -.breadcrumb= link_to "← Back to home page", root_path +- unless @prev.nil? + .back-post= link_to "← #{@prev.title}", @prev +- unless @next.nil? + .next-post= link_to "#{@next.title} →", @next +.clear = render @blog, short: false = show_comments(@blog) -- cgit 1.4.1