posts: rework post deletion to use dialog box.
Rework post deletion from using a separate page to using a dialog box, like flagging. * Add `DELETE /posts/:id` endpoint. * Remove `POST /moderator/post/posts/:id/delete` endpoint.
This commit is contained in:
@@ -4,18 +4,6 @@ module Moderator
|
|||||||
skip_before_action :api_check
|
skip_before_action :api_check
|
||||||
respond_to :html, :json, :xml, :js
|
respond_to :html, :json, :xml, :js
|
||||||
|
|
||||||
def confirm_delete
|
|
||||||
@post = ::Post.find(params[:id])
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete
|
|
||||||
@post = authorize ::Post.find(params[:id])
|
|
||||||
if params[:commit] == "Delete"
|
|
||||||
@post.delete!(params[:reason], :move_favorites => params[:move_favorites].present?)
|
|
||||||
end
|
|
||||||
redirect_to(post_path(@post))
|
|
||||||
end
|
|
||||||
|
|
||||||
def confirm_move_favorites
|
def confirm_move_favorites
|
||||||
@post = ::Post.find(params[:id])
|
@post = ::Post.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,6 +56,18 @@ class PostsController < ApplicationController
|
|||||||
respond_with_post_after_update(@post)
|
respond_with_post_after_update(@post)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@post = authorize Post.find(params[:id])
|
||||||
|
|
||||||
|
if params[:commit] == "Delete"
|
||||||
|
move_favorites = params.dig(:post, :move_favorites).to_s.truthy?
|
||||||
|
@post.delete!(params.dig(:post, :reason), move_favorites: move_favorites)
|
||||||
|
flash[:notice] = "Post deleted"
|
||||||
|
end
|
||||||
|
|
||||||
|
respond_with_post_after_update(@post)
|
||||||
|
end
|
||||||
|
|
||||||
def revert
|
def revert
|
||||||
@post = authorize Post.find(params[:id])
|
@post = authorize Post.find(params[:id])
|
||||||
@version = @post.versions.find(params[:version_id])
|
@version = @post.versions.find(params[:version_id])
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ class PostPolicy < ApplicationPolicy
|
|||||||
user.is_approver? && !record.is_deleted?
|
user.is_approver? && !record.is_deleted?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
delete?
|
||||||
|
end
|
||||||
|
|
||||||
def ban?
|
def ban?
|
||||||
user.is_approver? && !record.is_banned?
|
user.is_approver? && !record.is_banned?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
<h1>Delete Post</h1>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<%= PostPresenter.preview(@post, show_deleted: true) %>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<%= form_tag(delete_moderator_post_post_path, :style => "clear: both;", :class => "simple_form") do %>
|
|
||||||
<% if @post.parent_id %>
|
|
||||||
<div class="input">
|
|
||||||
<label for="move_favorites">
|
|
||||||
<%= check_box_tag "move_favorites" %>
|
|
||||||
Move favorites to parent?
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
<p style="font-weight: bold;">Note: If the reason you are planning to delete this post is because it is from a banned artist, please <%= link_to "ban", confirm_ban_moderator_post_post_path(@post) %> this post instead of deleting it.</p>
|
|
||||||
|
|
||||||
<div class="input">
|
|
||||||
<label for="reason">Reason</label>
|
|
||||||
<%= text_area_tag "reason" %>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<%= submit_tag "Delete" %>
|
|
||||||
<%= submit_tag "Cancel" %>
|
|
||||||
<% end %>
|
|
||||||
5
app/views/posts/destroy.js.erb
Normal file
5
app/views/posts/destroy.js.erb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<% if params[:commit] == "Delete" %>
|
||||||
|
location.reload();
|
||||||
|
<% else %>
|
||||||
|
Danbooru.Utility.dialog("Delete Post", "<%= j render "posts/partials/show/delete_dialog", post: @post %>");
|
||||||
|
<% end %>
|
||||||
9
app/views/posts/partials/show/_delete_dialog.html.erb
Normal file
9
app/views/posts/partials/show/_delete_dialog.html.erb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<div class="delete-post-dialog-body">
|
||||||
|
<%= edit_form_for(post, method: :delete, remote: true) do |f| %>
|
||||||
|
<input type="hidden" name="commit" value="Delete">
|
||||||
|
<%= f.input :reason, as: :dtext, inline: true, input_html: { value: "" } %>
|
||||||
|
<% if post.parent_id.present? %>
|
||||||
|
<%= f.input :move_favorites, label: "Move favorites to parent", as: :boolean, input_html: { checked: false } %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
<li id="post-option-move-favorites"><%= link_to "Move favorites", confirm_move_favorites_moderator_post_post_path(post_id: post.id) %></li>
|
<li id="post-option-move-favorites"><%= link_to "Move favorites", confirm_move_favorites_moderator_post_post_path(post_id: post.id) %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% elsif policy(post).delete? %>
|
<% elsif policy(post).delete? %>
|
||||||
<li id="post-option-delete"><%= link_to "Delete", confirm_delete_moderator_post_post_path(post_id: post.id) %></li>
|
<li id="post-option-delete"><%= link_to "Delete", post, method: :delete, remote: true %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if post.is_approvable? && !post.is_deleted? %>
|
<% if post.is_approvable? && !post.is_deleted? %>
|
||||||
|
|||||||
@@ -13,9 +13,7 @@ Rails.application.routes.draw do
|
|||||||
namespace :post do
|
namespace :post do
|
||||||
resources :posts, :only => [:delete, :expunge, :confirm_delete] do
|
resources :posts, :only => [:delete, :expunge, :confirm_delete] do
|
||||||
member do
|
member do
|
||||||
get :confirm_delete
|
|
||||||
post :expunge
|
post :expunge
|
||||||
post :delete
|
|
||||||
get :confirm_move_favorites
|
get :confirm_move_favorites
|
||||||
post :move_favorites
|
post :move_favorites
|
||||||
get :confirm_ban
|
get :confirm_ban
|
||||||
@@ -175,7 +173,7 @@ Rails.application.routes.draw do
|
|||||||
end
|
end
|
||||||
resources :post_replacements, :only => [:index, :new, :create, :update]
|
resources :post_replacements, :only => [:index, :new, :create, :update]
|
||||||
resources :post_votes, only: [:index]
|
resources :post_votes, only: [:index]
|
||||||
resources :posts, only: [:index, :show, :update] do
|
resources :posts, only: [:index, :show, :update, :destroy] do
|
||||||
resources :events, :only => [:index], :controller => "post_events"
|
resources :events, :only => [:index], :controller => "post_events"
|
||||||
resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements"
|
resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements"
|
||||||
resource :artist_commentary, :only => [:index, :show] do
|
resource :artist_commentary, :only => [:index, :show] do
|
||||||
|
|||||||
@@ -15,26 +15,6 @@ module Moderator
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "confirm_delete action" do
|
|
||||||
should "render" do
|
|
||||||
get_auth confirm_delete_moderator_post_post_path(@post), @admin
|
|
||||||
assert_response :success
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "delete action" do
|
|
||||||
should "render" do
|
|
||||||
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
|
|
||||||
assert(@post.reload.is_deleted?)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "work even if the deleter has flagged the post previously" do
|
|
||||||
create(:post_flag, post: @post, creator: @admin)
|
|
||||||
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
|
|
||||||
assert(@post.reload.is_deleted?)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "confirm_move_favorites action" do
|
context "confirm_move_favorites action" do
|
||||||
should "render" do
|
should "render" do
|
||||||
get_auth confirm_move_favorites_moderator_post_post_path(@post), @admin
|
get_auth confirm_move_favorites_moderator_post_post_path(@post), @admin
|
||||||
|
|||||||
@@ -633,6 +633,43 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "destroy action" do
|
||||||
|
setup do
|
||||||
|
@approver = create(:approver)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "delete the post" do
|
||||||
|
delete_auth post_path(@post), @approver, params: { commit: "Delete", post: { reason: "test" } }
|
||||||
|
|
||||||
|
assert_redirected_to @post
|
||||||
|
assert_equal(true, @post.reload.is_deleted?)
|
||||||
|
assert_equal("test", @post.flags.last.reason)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "delete the post even if the deleter has flagged the post previously" do
|
||||||
|
create(:post_flag, post: @post, creator: @approver)
|
||||||
|
delete_auth post_path(@post), @approver, params: { commit: "Delete", post: { reason: "test" } }
|
||||||
|
|
||||||
|
assert_redirected_to @post
|
||||||
|
assert_equal(true, @post.reload.is_deleted?)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not delete the post if the user is unauthorized" do
|
||||||
|
delete_auth post_path(@post), @user, params: { commit: "Delete" }
|
||||||
|
|
||||||
|
assert_response 403
|
||||||
|
assert_equal(false, @post.is_deleted?)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render the delete post dialog for an xhr request" do
|
||||||
|
delete_auth post_path(@post), @approver, xhr: true
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
assert_equal(false, @post.is_deleted?)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
context "revert action" do
|
context "revert action" do
|
||||||
setup do
|
setup do
|
||||||
PostVersion.sqs_service.stubs(:merge?).returns(false)
|
PostVersion.sqs_service.stubs(:merge?).returns(false)
|
||||||
|
|||||||
Reference in New Issue
Block a user