From 3bd69f1e58f78c965e62c4d5a4ea6267fcd8ef1f Mon Sep 17 00:00:00 2001 From: Vaishnav Deore <86405648+Waishnav@users.noreply.github.com> Date: Tue, 30 Jul 2024 12:30:22 +0530 Subject: [PATCH 1/2] fix: moderator can delete the forum thread & post (#29) * fix: moderator can delete the forum thread & post --- .../simple_discussion/application_controller.rb | 2 +- .../simple_discussion/forum_threads_controller.rb | 9 +++++++-- app/models/forum_thread.rb | 2 +- .../simple_discussion/forum_posts/_forum_post.html.erb | 1 - .../simple_discussion/forum_threads/show.html.erb | 10 ++++++++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/controllers/simple_discussion/application_controller.rb b/app/controllers/simple_discussion/application_controller.rb index 11c3841..d9587fd 100644 --- a/app/controllers/simple_discussion/application_controller.rb +++ b/app/controllers/simple_discussion/application_controller.rb @@ -13,7 +13,7 @@ def is_moderator_or_owner?(object) helper_method :is_moderator_or_owner? def is_moderator? - current_user.respond_to?(:moderator) && current_user.moderator? + current_user.respond_to?(:moderator?) && current_user.moderator? end helper_method :is_moderator? diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 6edc92d..570597a 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -1,7 +1,7 @@ class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationController before_action :authenticate_user!, only: [:mine, :participating, :new, :create] - before_action :set_forum_thread, only: [:show, :edit, :update] - before_action :require_mod_or_author_for_thread!, only: [:edit, :update] + before_action :set_forum_thread, only: [:show, :edit, :update, :destroy] + before_action :require_mod_or_author_for_thread!, only: [:edit, :update, :destroy] def index @forum_threads = ForumThread.pinned_first.sorted.includes(:user, :forum_category).paginate(page: page_number) @@ -60,6 +60,11 @@ def update end end + def destroy + @forum_thread.destroy! + redirect_to simple_discussion.forum_threads_path + end + private def set_forum_thread diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index 18ad3c7..1bc79e7 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -5,7 +5,7 @@ class ForumThread < ApplicationRecord belongs_to :forum_category belongs_to :user - has_many :forum_posts + has_many :forum_posts, dependent: :destroy has_many :forum_subscriptions has_many :optin_subscribers, -> { where(forum_subscriptions: {subscription_type: :optin}) }, through: :forum_subscriptions, source: :user has_many :optout_subscribers, -> { where(forum_subscriptions: {subscription_type: :optout}) }, through: :forum_subscriptions, source: :user diff --git a/app/views/simple_discussion/forum_posts/_forum_post.html.erb b/app/views/simple_discussion/forum_posts/_forum_post.html.erb index 88e4809..79688d7 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -2,7 +2,6 @@ <%= content_tag :div, id: dom_id(forum_post), class: "forum-post" do %>
<%= forum_post.user.name %> <%= forum_user_badge(forum_post.user) %> - <% if is_moderator_or_owner?(forum_post) %>(You)<% end %> + <% if is_owner?(forum_post) %>(You)<% end %>
<%= t('on') %> <%= forum_post.created_at.strftime("%b %d, %Y") %>