diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index b0ca9164..87c47a14 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 @@ -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 diff --git a/app/views/comments/index.html.haml b/app/views/comments/index.html.haml new file mode 100644 index 00000000..950de311 --- /dev/null +++ b/app/views/comments/index.html.haml @@ -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. diff --git a/config/routes.rb b/config/routes.rb index 8e79ba22..7b77e50a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'