Fix #4982: Add route to remove a post from a favorite group

This commit is contained in:
evazion
2022-05-02 15:43:44 -05:00
parent d2502a0c40
commit 17ffe3590a
5 changed files with 47 additions and 0 deletions

View File

@@ -59,4 +59,12 @@ class FavoriteGroupsController < ApplicationController
respond_with(@favorite_group)
end
def remove_post
@favorite_group = authorize FavoriteGroup.find(params[:id])
@post = Post.find(params[:post_id])
@favorite_group.remove(@post)
respond_with(@favorite_group)
end
end

View File

@@ -17,6 +17,10 @@ class FavoriteGroupPolicy < ApplicationPolicy
update?
end
def remove_post?
update?
end
def can_enable_privacy?
record.creator.is_gold?
end

View File

@@ -0,0 +1,7 @@
<% if @favorite_group.errors.any? %>
Danbooru.notice("<%= j @favorite_group.errors.full_messages.join("; ") %>");
<% else %>
Danbooru.notice("Removed post from favorite group <%= j @favorite_group.pretty_name %>");
<% end %>
$("#add-to-favgroup-dialog").dialog("close");

View File

@@ -125,6 +125,7 @@ Rails.application.routes.draw do
resources :favorite_groups do
member do
put :add_post
put :remove_post
end
resource :order, :only => [:edit], :controller => "favorite_group_orders"
end

View File

@@ -116,6 +116,7 @@ class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest
should "render" do
@post = create(:post)
put_auth add_post_favorite_group_path(@favgroup), @user, params: {post_id: @post.id, format: "js"}
assert_response :success
assert_equal([@post.id], @favgroup.reload.post_ids)
end
@@ -123,7 +124,33 @@ class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest
should "not add posts to favgroups belonging to other users" do
@post = create(:post)
put_auth add_post_favorite_group_path(@favgroup), create(:user), params: {post_id: @post.id, format: "js"}
assert_response 403
assert_equal([], @favgroup.reload.post_ids)
end
end
context "remove_post action" do
should "render" do
@post = create(:post)
@favgroup.add(@post)
assert_equal([@post.id], @favgroup.reload.post_ids)
put_auth remove_post_favorite_group_path(@favgroup), @user, params: { post_id: @post.id, format: "js" }
assert_response :success
assert_equal([], @favgroup.reload.post_ids)
end
should "not remove posts from favgroups belonging to other users" do
@post = create(:post)
@favgroup.add(@post)
assert_equal([@post.id], @favgroup.reload.post_ids)
put_auth remove_post_favorite_group_path(@favgroup), create(:user), params: { post_id: @post.id, format: "js" }
assert_response 403
assert_equal([@post.id], @favgroup.reload.post_ids)
end
end