DEV: Shorten a condition (#25951)

This commit is contained in:
Natalie Tay 2024-02-29 12:57:53 +08:00 committed by GitHub
parent 5f119c57e8
commit ef292d1fed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 7 deletions

View File

@ -131,13 +131,10 @@ module DiscoursePoll
return true if old_poll.public_send(attr) != new_poll.public_send(attr)
end
# an option was changed?
if old_poll.poll_options.map { |o| o.digest }.sort != new_options.map { |o| o["id"] }.sort
return true
end
sorted_old_options = old_poll.poll_options.map { |o| o.digest }.sort
sorted_new_options = new_options.map { |o| o["id"] }.sort
# it's the same!
false
sorted_old_options != sorted_new_options
end
end
end

View File

@ -206,7 +206,25 @@ RSpec.describe PostsController do
expect(json["post"]["polls"][0]["options"][2]["html"]).to eq("C")
end
it "resets the votes" do
it "does not clear votes when poll has no change" do
DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"])
put :update,
params: {
id: post_id,
post: {
raw: "[poll]\n- A\n- B\n[/poll]\n This poll has no change, but the raw does.",
},
},
format: :json
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post"]["polls_votes"]["poll"]).to match_array(
"5c24fc1df56d764b550ceae1b9319125",
)
end
it "resets the votes when poll is changed" do
DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"])
put :update,