Unverified Commit 655fe2a9 authored by Benjamin Neff's avatar Benjamin Neff
Browse files

Cleanup invalid polls without status message

closes #7614
parent 00296ffd
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
* Prevent users from zooming in IE Mobile [#7589](https://github.com/diaspora/diaspora/pull/7589)
* Fix recipient prefill on contacts and profile page [#7599](https://github.com/diaspora/diaspora/pull/7599)
* Display likes and reshares without login [#7583](https://github.com/diaspora/diaspora/pull/7583)
* Fix invalid data in the database for user data export [#7614](https://github.com/diaspora/diaspora/pull/7614)

## Features
* Ask for confirmation when leaving a submittable comment field [#7530](https://github.com/diaspora/diaspora/pull/7530)
+2 −2
Original line number Diff line number Diff line
@@ -5,8 +5,8 @@ class Poll < ApplicationRecord
  include Diaspora::Fields::Guid

  belongs_to :status_message
  has_many :poll_answers, -> { order 'id ASC' }
  has_many :poll_participations
  has_many :poll_answers, -> { order "id ASC" }, dependent: :destroy
  has_many :poll_participations, dependent: :destroy
  has_one :author, through: :status_message

  #forward some requests to status message, because a poll is just attached to a status message and is not sharable itself
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ class StatusMessage < Post
  has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid

  has_one :location
  has_one :poll, autosave: true
  has_one :poll, autosave: true, dependent: :destroy
  has_many :poll_participations, through: :poll

  attr_accessor :oembed_url
+19 −0
Original line number Diff line number Diff line
class CleanupInvalidPolls < ActiveRecord::Migration[5.1]
  class Poll < ApplicationRecord
    has_many :poll_answers, dependent: :destroy
    has_many :poll_participations, dependent: :destroy
  end
  class PollAnswer < ApplicationRecord
    belongs_to :poll
    has_many :poll_participations
  end
  class PollParticipation < ApplicationRecord
    belongs_to :poll
    belongs_to :poll_answer
  end

  def up
    Poll.joins("LEFT OUTER JOIN posts ON posts.id = polls.status_message_id")
        .where("posts.id IS NULL").destroy_all
  end
end
+17 −0
Original line number Diff line number Diff line
@@ -233,6 +233,23 @@ describe StatusMessage, type: :model do
    end
  end

  describe "poll" do
    it "destroys the poll (with all answers and participations) when the status message is destroyed" do
      poll = FactoryGirl.create(:poll_participation).poll
      status_message = poll.status_message

      poll_id = poll.id
      poll_answers = poll.poll_answers.map(&:id)
      poll_participations = poll.poll_participations.map(&:id)

      status_message.destroy

      expect(Poll.where(id: poll_id)).not_to exist
      poll_answers.each {|id| expect(PollAnswer.where(id: id)).not_to exist }
      poll_participations.each {|id| expect(PollParticipation.where(id: id)).not_to exist }
    end
  end

  describe "validation" do
    let(:status_message) { build(:status_message, text: @message_text) }