From 17d660fd4b8516d6b446f7c41d7b076a29a242a1 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 12 Oct 2023 17:44:38 -0400 Subject: Added comment deleting Also I seem to have fixed the JS errors that were preventing the fancy stuff in the admin panel from working. --- app/controllers/admin/comments_controller.rb | 23 ++++++++++++++++++++++ app/helpers/admin/comments_helper.rb | 2 ++ app/views/admin/comments/index.html.haml | 23 ++++++++++++++++++++++ app/views/comments/_comment.html.haml | 2 +- app/views/layouts/admin.html.haml | 2 ++ config/initializers/js_routes.rb | 7 +++++++ config/routes.rb | 2 ++ test/controllers/admin/comments_controller_test.rb | 8 ++++++++ 8 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/comments_controller.rb create mode 100644 app/helpers/admin/comments_helper.rb create mode 100644 app/views/admin/comments/index.html.haml create mode 100644 config/initializers/js_routes.rb create mode 100644 test/controllers/admin/comments_controller_test.rb diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb new file mode 100644 index 0000000..bd255ad --- /dev/null +++ b/app/controllers/admin/comments_controller.rb @@ -0,0 +1,23 @@ +class Admin::CommentsController < Admin::AdminController + before_action :set_section + + def index + @comments = Comment.order(updated_at: :desc).paginate(page: params[:page], per_page: 20) + end + + def destroy + if Comment.destroy(params[:id]) + flash.notice = "Comment successfully deleted." + else + flash.alert = "Could not delete comment." + end + + redirect_to admin_comments_url + end + + private + + def set_section + @section = "comments" + end +end diff --git a/app/helpers/admin/comments_helper.rb b/app/helpers/admin/comments_helper.rb new file mode 100644 index 0000000..ce9444d --- /dev/null +++ b/app/helpers/admin/comments_helper.rb @@ -0,0 +1,2 @@ +module Admin::CommentsHelper +end diff --git a/app/views/admin/comments/index.html.haml b/app/views/admin/comments/index.html.haml new file mode 100644 index 0000000..7b25c05 --- /dev/null +++ b/app/views/admin/comments/index.html.haml @@ -0,0 +1,23 @@ +- title "Comments" +%table#entries + %tr + %th Text + %th Author + %th Blog post + %th Date published + %th + - @comments.each do |comment| + %tr{ class: cycle("even", "odd") } + %td= comment.body + %td + %ul + %li= comment.username + %li= comment.email + - unless comment.website.empty? + %li= comment.website + %td= link_to comment.blog.title, comment.blog + %td= comment.published_at.strftime("%B %d, %Y, %l:%M%P") + %td + %ul.admin-actions + %li= link_to "View", blog_url(comment.blog, anchor: "comment-#{comment.id}") + %li= link_to "Delete", admin_comment_url(comment), method: :delete, data: { confirm: "Are you sure?" } diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 3e10759..3ad21e9 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -1,4 +1,4 @@ -.blog-comment +.blog-comment{ id: "comment-#{comment.id}" } %blockquote.bubble.rounded.bottom = image_tag comment.gravatar_url, class: "comment-avatar" = markdown(comment.body, { restricted: true }) diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index 1c865b0..5d75ef5 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -37,5 +37,7 @@ = link_to "Games", admin_games_url, class: "major-link" %ul.minors %li.minor= link_to "New game", new_admin_game_url + %li{major_sidebar_attrs("comments")} + = link_to "Comments", admin_comments_url, class: "major-link" #main = yield diff --git a/config/initializers/js_routes.rb b/config/initializers/js_routes.rb new file mode 100644 index 0000000..7bc0c65 --- /dev/null +++ b/config/initializers/js_routes.rb @@ -0,0 +1,7 @@ +JsRoutes.setup do |c| + # Setup your JS module system: + # ESM, CJS, AMD, UMD or nil + # c.module_type = "ESM" + c.module_type = nil + c.namespace = 'Routes' +end diff --git a/config/routes.rb b/config/routes.rb index 2bae007..dcfe673 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,8 @@ Rails.application.routes.draw do resources :links, except: [:show] resources :games, except: [:show] + + resources :comments, only: [:index, :destroy] end devise_for :users, controllers: { diff --git a/test/controllers/admin/comments_controller_test.rb b/test/controllers/admin/comments_controller_test.rb new file mode 100644 index 0000000..33f1a47 --- /dev/null +++ b/test/controllers/admin/comments_controller_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class Admin::CommentsControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get admin_comments_index_url + assert_response :success + end +end -- cgit 1.4.1