FIX: Avoid exception when rendering a poll in a trashed post

Maintain the poll belongs_to post relation when a post is trashed
This commit is contained in:
David Taylor 2020-04-28 14:43:09 +01:00
parent ba616ffb50
commit 84be92c067
No known key found for this signature in database
GPG Key ID: 46904C18B1D3F434
2 changed files with 16 additions and 2 deletions

View File

@ -4,7 +4,7 @@ class Poll < ActiveRecord::Base
# because we want to use the 'type' column and don't want to use STI
self.inheritance_column = nil
belongs_to :post
belongs_to :post, -> { unscope(:where) }
has_many :poll_options, -> { order(:id) }, dependent: :destroy
has_many :poll_votes
@ -51,7 +51,7 @@ class Poll < ActiveRecord::Base
end
def is_me?(user)
user && post.user&.id == user&.id
user && post && post.user&.id == user&.id
end
def has_voted?(user)

View File

@ -69,4 +69,18 @@ describe ::DiscoursePoll::Poll do
expect(poll.can_see_results?(user)).to eq(true)
end
end
describe 'when post is trashed' do
it "maintains the association" do
user = Fabricate(:user)
post = Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]", user: user)
poll = post.polls.first
post.trash!
poll.reload
expect(poll.post).to eq(post)
end
end
end