Fixed removing notifications on admin unpublishing (#18047)

This commit is contained in:
Anna Buianova 2022-07-06 18:35:20 +03:00 committed by GitHub
parent 98514216e7
commit 6a229f00b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -213,13 +213,17 @@ class ArticlesController < ApplicationController
def admin_unpublish
authorize @article
attributes = {}
if @article.has_frontmatter?
@article.body_markdown.sub!(/\npublished:\s*true\s*\n/, "\npublished: false\n")
body_markdown = @article.body_markdown.sub(/\npublished:\s*true\s*\n/, "\npublished: false\n")
attributes[:body_markdown] = body_markdown
else
@article.published = false
attributes[:published] = false
end
if @article.save
result = Articles::Updater.call(current_user, @article, attributes)
if result.success
render json: { message: "success", path: @article.current_state_path }, status: :ok
else
render json: { message: @article.errors.full_messages }, status: :unprocessable_entity

View file

@ -20,4 +20,16 @@ RSpec.describe "ArticlesAdminUnpublish", type: :request do
article.reload
expect(article.published).to be false
end
it "removes the related notifications when unpublishing" do
expect(article.published).to be true
create(:notification, notifiable: article, action: "Published")
expect do
patch "/articles/#{article.id}/admin_unpublish", params: {
id: article.id,
username: user.username,
slug: article.slug
}
end.to change(Notification, :count).by(-1)
end
end