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 %>
- <% if is_moderator_or_owner?(forum_post) %>
<%= link_to icon("fas","edit"), simple_discussion.edit_forum_thread_forum_post_path(@forum_thread, forum_post), diff --git a/app/views/simple_discussion/forum_threads/show.html.erb b/app/views/simple_discussion/forum_threads/show.html.erb index eb73340..9534310 100644 --- a/app/views/simple_discussion/forum_threads/show.html.erb +++ b/app/views/simple_discussion/forum_threads/show.html.erb @@ -1,6 +1,16 @@

<%= icon "fas", "thumb-tack", class: "text-muted" if @forum_thread.pinned? %> <%= @forum_thread.title %>

+
+ <% if is_moderator_or_owner?(@forum_thread) %> + <%= link_to icon("fas","trash"), simple_discussion.forum_thread_path(@forum_thread), + class: "text-muted", + method: :delete, + data: { toggle: "tooltip", placement: "left", confirm: "Are you sure you want to delete this thread?" }, + title: t('delete_this_thread') + %> + <% end %> +
From 734f8a44cbe7bfda337b1fa9583daeef950ce252 Mon Sep 17 00:00:00 2001 From: Vaishnav Deore <86405648+Waishnav@users.noreply.github.com> Date: Mon, 5 Aug 2024 20:39:27 +0530 Subject: [PATCH 2/2] fix: you shown to moderators (#31) --- app/controllers/simple_discussion/application_controller.rb | 5 +++++ app/views/simple_discussion/forum_posts/_forum_post.html.erb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/simple_discussion/application_controller.rb b/app/controllers/simple_discussion/application_controller.rb index d9587fd..e464f26 100644 --- a/app/controllers/simple_discussion/application_controller.rb +++ b/app/controllers/simple_discussion/application_controller.rb @@ -12,6 +12,11 @@ def is_moderator_or_owner?(object) end helper_method :is_moderator_or_owner? + def is_owner?(object) + object.user == current_user + end + helper_method :is_owner? + def is_moderator? current_user.respond_to?(:moderator?) && current_user.moderator? end 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 79688d7..9f2964f 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -19,7 +19,7 @@
avatar of user

<%= 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") %>