Skip to content

Commit

Permalink
fix: moderator can delete the forum thread & post (#29)
Browse files Browse the repository at this point in the history
* fix: moderator can delete the forum thread & post
  • Loading branch information
Waishnav authored Jul 30, 2024
1 parent 3abb960 commit 3bd69f1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
9 changes: 7 additions & 2 deletions app/controllers/simple_discussion/forum_threads_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/forum_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<%= content_tag :div, id: dom_id(forum_post), class: "forum-post" do %>
<div class="forum-post-header">

<% if is_moderator_or_owner?(forum_post) %>
<div class="float-right">
<%= link_to icon("fas","edit"), simple_discussion.edit_forum_thread_forum_post_path(@forum_thread, forum_post),
Expand Down
10 changes: 10 additions & 0 deletions app/views/simple_discussion/forum_threads/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<div class="row">
<div class="col-md-12 block-inline mb-2">
<h2 style="display:inline"><%= icon "fas", "thumb-tack", class: "text-muted" if @forum_thread.pinned? %> <%= @forum_thread.title %></h2>
<div class="float-right d-flex">
<% 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 %>
</div>
</div>
</div>

Expand Down

0 comments on commit 3bd69f1

Please sign in to comment.