From ef292d1fed0009d7c6b096c5af66564d813b7906 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Thu, 29 Feb 2024 12:57:53 +0800 Subject: [PATCH] DEV: Shorten a condition (#25951) --- plugins/poll/lib/polls_updater.rb | 9 +++------ .../spec/controllers/posts_controller_spec.rb | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/plugins/poll/lib/polls_updater.rb b/plugins/poll/lib/polls_updater.rb index 6c9a370b675..261e925c712 100644 --- a/plugins/poll/lib/polls_updater.rb +++ b/plugins/poll/lib/polls_updater.rb @@ -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 diff --git a/plugins/poll/spec/controllers/posts_controller_spec.rb b/plugins/poll/spec/controllers/posts_controller_spec.rb index 2f6da9f31d6..cf1051e8054 100644 --- a/plugins/poll/spec/controllers/posts_controller_spec.rb +++ b/plugins/poll/spec/controllers/posts_controller_spec.rb @@ -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,