diaspora/app/models/poll.rb
cmrd Senya f85f167f50 Implement archive import backend
This implements archive import feature.

The feature is divided in two main subfeatures: archive validation and archive import.

Archive validation performs different validation on input user archive. This can be
used without actually running import, e.g. when user wants to check the archive
before import from the frontend. Validators may add messages and modify the archive.

Validators are separated in two types: critical validators and non-critical validators.

If validations by critical validators fail it means we can't import archive.

If non-critical validations fail, we can import archive, but some warning messages
are rendered.

Also validators may change archive contents, e.g. when some entity can't be
imported it may be removed from the archive.

Validators' job is to take away complexity from the importer and perform the validations
which are not implemented in other parts of the system, e.g. DB validations or
diaspora_federation entity validations.

Archive importer then takes the modified archive from the validator and imports it.

In order to incapsulate high-level migration logic a MigrationService is
introduced. MigrationService links ArchiveValidator, ArchiveImporter and
AccountMigration.

Also here is introduced a rake task which may be used by podmins to run archive
import.
2019-04-26 18:41:27 +03:00

44 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class Poll < ApplicationRecord
include Diaspora::Federated::Base
include Diaspora::Fields::Guid
include Diaspora::Federated::Fetchable
belongs_to :status_message
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
delegate :author_id, :diaspora_handle, :public?, :subscribers, to: :status_message
validate :enough_poll_answers
validates :question, presence: true
scope :all_public, -> { joins(:status_message).where(posts: {public: true}) }
self.include_root_in_json = false
def enough_poll_answers
errors.add(:poll_answers, I18n.t("activerecord.errors.models.poll.attributes.poll_answers.not_enough_poll_answers")) if poll_answers.size < 2
end
def as_json(options={})
{
poll_id: id,
post_id: status_message.id,
question: question,
poll_answers: poll_answers,
participation_count: participation_count
}
end
def participation_answer(user)
poll_participations.find_by(author_id: user.person.id)
end
def participation_count
poll_answers.sum("vote_count")
end
end