Skip to content

Commit 19457ef

Browse files
committed
Finish Post's comments feature for admin page section
1 parent 32aa7f3 commit 19457ef

17 files changed

+292
-28
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class CommentsController < ApplicationController
2+
layout 'mainadmin'
3+
before_action :authenticate_user!, :set_paper_trail_whodunnit
4+
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
5+
6+
def index
7+
@comments = Comment.order(:id).all
8+
end
9+
10+
def show
11+
@comment = current_comment
12+
end
13+
14+
def new
15+
@comment = Comment.new
16+
end
17+
18+
def edit
19+
@comment = current_comment
20+
end
21+
22+
def destroy
23+
comment = current_comment
24+
comment.destroy
25+
redirect_to comments_path, notice: "Comment id:#{comment.id} for Post:'#{comment.post.title}' deleted"
26+
end
27+
28+
def create
29+
@comment = Comment.new comment_params
30+
if @comment.save
31+
redirect_to comment_path(@comment), notice: "Comment id:#{@comment.id} for Post:'#{@comment.post.title}' deleted"
32+
else
33+
render 'new'
34+
end
35+
end
36+
37+
def update
38+
@comment = current_comment
39+
if @comment.update comment_params
40+
redirect_to comment_path(@comment), notice: "Comment id:#{@comment.id} for Post:'#{@comment.post.title}' has been updated"
41+
else
42+
render 'edit'
43+
end
44+
end
45+
46+
def audit_trail
47+
comment = current_comment
48+
@audit_trail = ApplicationHelper::AuditTrail.to_model comment.versions
49+
render 'shared/audit_trail'
50+
end
51+
52+
private
53+
54+
def current_comment
55+
@current_comment ||= Comment.find params[:id]
56+
end
57+
58+
def comment_params
59+
params.require(:comment).permit(:name, :email, :content, :post_id)
60+
end
61+
62+
def record_not_found
63+
redirect_to comments_path, notice: 'Comments not found'
64+
end
65+
end

app/controllers/posts_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def audit_trail
5151
render 'shared/audit_trail'
5252
end
5353

54+
def comments
55+
@post = current_post
56+
render 'comments'
57+
end
58+
5459
private
5560

5661
def current_post

app/models/comment.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
require 'markdown_helper'
2+
13
class Comment < ApplicationRecord
4+
5+
include MarkdownHelper
6+
27
has_paper_trail
38

49
belongs_to :post
10+
511
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, allow_blank: true, message: 'email format is not valid' }
12+
validates :name, :content, presence: true
13+
614
end

app/models/post.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
require 'markdown_helper'
2+
13
class Post < ApplicationRecord
4+
5+
include MarkdownHelper
6+
27
has_paper_trail
38

49
belongs_to :category
@@ -19,20 +24,6 @@ def permalink
1924
super || self.permalink = "#{title.strip.downcase.gsub " ", "_"}"
2025
end
2126

22-
def content_to_html
23-
renderer = CodeRayify.new(filter_html: true, hard_wrap: true)
24-
markdown = Redcarpet::Markdown.new(
25-
renderer,
26-
fenced_code_blocks: true,
27-
autolink: true,
28-
tables: true,
29-
strikethrough: true,
30-
lax_spacing: true,
31-
space_after_headers: true,
32-
)
33-
markdown.render(content).html_safe
34-
end
35-
3627
def self.all_release
3728
release_post = Post.order('release_date DESC').where(release: true)
3829
release_post.map do |p|
@@ -46,10 +37,4 @@ def self.all_release
4637
}
4738
end
4839
end
49-
50-
class CodeRayify < Redcarpet::Render::HTML
51-
def block_code(code, language)
52-
CodeRay.scan(code, language).div
53-
end
54-
end
5540
end

app/views/admin/index.html.slim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
- replace :title, "Dashboard"
2-
- replace :content do
2+
- replace :admin_content do
33
.column
4-
.div
5-
| Welcome, Dashboard is not available yet!
4+
h1.title.is-size-5-mobile.is-size-3-desktop.has-text-grey-light.has-text-centered
5+
br
6+
| Welcome, no dashboard available!

app/views/comments/_form.html.slim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
= form_for @comment do |f|
2+
.field
3+
= f.label "Name", class: "label"
4+
.control
5+
= f.text_field :name, class: "input #{ @comment.errors[:name].any? ? 'is-danger' : '' }"
6+
- if @comment.errors[:name]
7+
p.help.is-danger
8+
= @comment.errors[:name].join(', ')
9+
.field
10+
= f.label "Email", class: "label"
11+
.control
12+
= f.text_field :email, class: "input #{ @comment.errors[:email].any? ? 'is-danger' : '' }"
13+
- if @comment.errors[:email]
14+
p.help.is-danger
15+
= @comment.errors[:email].join(', ')
16+
.field
17+
= f.label "Post", class: "label"
18+
.control
19+
.select
20+
= f.collection_select :post_id, Post.order(:permalink).all, :id, :permalink, include_blank: false
21+
.field
22+
= f.label "Content", class: "label"
23+
.control
24+
= f.text_area :content, rows: 8, class: "textarea #{ @comment.errors[:content].any? ? 'is-danger' : ''}"
25+
- if @comment.errors[:content]
26+
p.help.is-danger
27+
= @comment.errors[:content].join(', ')
28+
29+
div
30+
= f.submit button_text, class: "button is-primary is-rounded"

app/views/comments/edit.html.slim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- replace :title, "Comment (Edit)"
2+
- replace :admin_content do
3+
.column.is-4
4+
= render partial: 'form', locals: {button_text: 'Update'}

app/views/comments/index.html.slim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- replace :title, "Comments"
2+
- replace :admin_content do
3+
.column
4+
div
5+
= link_to 'New', new_comment_path(@comment), class: "button is-rounded is-primary"
6+
br
7+
table.table.is-striped.is-narrow
8+
thead
9+
tr
10+
th Date
11+
th Name
12+
th Email
13+
th Post Title
14+
tbody
15+
- @comments.each do |comment|
16+
tr
17+
td
18+
= comment.created_at.strftime("%Y-%-m-%-d %H:%M")
19+
td
20+
= comment.name
21+
td
22+
= comment.email
23+
td
24+
= comment.post.title
25+
td
26+
= link_to 'detail', comment_path(comment), class: "button is-info"
27+
div
28+
| Size:
29+
b = @comments.size

app/views/comments/new.html.slim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- replace :title, "Comment (New)"
2+
- replace :admin_content do
3+
.column.is-4
4+
= render partial: 'form', locals: {button_text: 'Create'}

app/views/comments/show.html.slim

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
- replace :title, "Comment"
2+
- replace :admin_content do
3+
.column
4+
table.table.is-narrow.is-hoverable
5+
tbody
6+
tr
7+
td.has-text-weight-semibold Id
8+
td
9+
| :
10+
td = @comment.id
11+
tr
12+
td.has-text-weight-semibold Name
13+
td
14+
| :
15+
td = @comment.name
16+
tr
17+
td.has-text-weight-semibold Email
18+
td
19+
| :
20+
td = @comment.email
21+
tr
22+
td.has-text-weight-semibold Post
23+
td
24+
| :
25+
td
26+
= link_to "#{@comment.post.title}", post_path(@comment.post)
27+
tr
28+
td.has-text-weight-semibold Created at
29+
td
30+
| :
31+
td = @comment.created_at
32+
article.message.is-dark
33+
.message-header
34+
| Content
35+
.message-body
36+
.content
37+
= @comment.content_to_html
38+
br
39+
.field.is-grouped
40+
p.control
41+
div
42+
= link_to 'Edit', edit_comment_path(@comment), class: "button is-primary is-rounded"
43+
p.control
44+
div
45+
= link_to 'Delete', comment_path(@comment), method: :delete, class: "button is-warning is-rounded", data: {confirm: "Confirmation to delete id:'#{@comment.id}'?"}
46+
p.control
47+
div
48+
= link_to 'Audit Trail', audit_trail_comment_path(@comment), class: "button is-info is-rounded"

0 commit comments

Comments
 (0)