Skip to content

Commit

Permalink
Introduces admin interface for comments
Browse files Browse the repository at this point in the history
Paginated view of all comments with the ability to delete them.
  • Loading branch information
hennevogel committed Sep 10, 2024
1 parent a12fb2d commit a9ef269
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
17 changes: 15 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class CommentsController < ApplicationController
include MarkdownHelper
before_action :get_parent, except: :reply_modal
load_and_authorize_resource
before_action :get_parent, only: %i[create update]
load_and_authorize_resource except: :index
authorize_resource only: :index
skip_before_action :verify_authenticity_token, only: [:reply_modal]

def index
@comments = Comment.accessible_by(current_ability).order('id DESC').page(params[:page])
end

def create
@comment = @parent.comments.build(comment_params)
@comment.commenter = current_user
Expand All @@ -28,6 +33,14 @@ def update
end
end

def destroy
@comment.destroy

respond_to do |format|
format.html { redirect_to comments_path, notice: 'Comment was successfully deleted.' }
end
end

def comment_params
params.require(:comment).permit(:commentable_id, :commentable_type, :commenter_id, :text, :commenter)
end
Expand Down
43 changes: 43 additions & 0 deletions app/views/comments/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- content_for :title do
Comments

.row
.col-sm-12
- if @comments.any?
.row
.col-sm-12
%table.table.table-hover
%thead
%th Date
%th User
%th Comment
%th Project
%th Actions
- @comments.each do |comment|
%tr
%td
= comment.updated_at
%td
- if comment.commenter
= link_to user_path(comment.commenter) do
= comment.commenter.name
- else
Deleted User
%td
= comment.text
%td
- if comment.commentable
= link_to project_path(comment.commentable) do
= comment.project.title
- else
Deleted Project/Comment
%td
.btn-group
= link_to comment_path(comment), method: :delete, data: { confirm: 'Are you sure you want to delete this comment?' }, class: 'btn btn-danger' do
Delete Comment
.row
.col-sm-12
.text-center
= paginate @comments
- else
No comments yet.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
resources :comments, only: %i[create update]
end

resources :comments, only: [] do
resources :comments, only: %i[index destroy] do
resources :comments, only: %i[create update]
collection do
get '/reply/:id', to: 'comments#reply_modal', as: 'reply_modal'
Expand Down

0 comments on commit a9ef269

Please sign in to comment.