Sha256: 9e0bc6dbe6fd05831e318cceb31cf0092be0dd6b4a109d16fecb9e059b234f87

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Decidim
  module Meetings
    # The data store for an Answer in the Decidim::Meetings
    class Answer < Meetings::ApplicationRecord
      include Decidim::DataPortability

      belongs_to :user, class_name: "Decidim::User", foreign_key: "decidim_user_id", optional: true
      belongs_to :questionnaire, class_name: "Decidim::Meetings::Questionnaire", foreign_key: "decidim_questionnaire_id"
      belongs_to :question, class_name: "Question", foreign_key: "decidim_question_id"

      has_many :choices,
               class_name: "AnswerChoice",
               foreign_key: "decidim_answer_id",
               dependent: :destroy,
               inverse_of: :answer

      validate :user_questionnaire_same_organization
      validate :question_belongs_to_questionnaire

      def self.user_collection(user)
        where(decidim_user_id: user.id)
      end

      def self.export_serializer
        Decidim::Meetings::DataPortabilityUserAnswersSerializer
      end

      def organization
        user.organization if user.present?
        questionnaire&.questionnaire_for.try(:organization)
      end

      private

      def user_questionnaire_same_organization
        return if user.nil? || user&.organization == questionnaire.questionnaire_for&.organization

        errors.add(:user, :invalid)
      end

      def question_belongs_to_questionnaire
        errors.add(:questionnaire, :invalid) if question&.questionnaire != questionnaire
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-meetings-0.26.10 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.9 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.8 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.7 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.5 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.4 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.3 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.2 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.1 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.0 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.0.rc2 app/models/decidim/meetings/answer.rb
decidim-meetings-0.26.0.rc1 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.2 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.1 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.0 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.0.rc4 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.0.rc3 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.0.rc2 app/models/decidim/meetings/answer.rb
decidim-meetings-0.25.0.rc1 app/models/decidim/meetings/answer.rb