Sha256: ae93c3bed7dd806c45b4f951e31a05da7d3c798c937c7114b81dd3fe623292e2

Contents?: true

Size: 912 Bytes

Versions: 54

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Surveys
    # The data store for a SurveyAnswer in the Decidim::Surveys component.
    class SurveyAnswer < Surveys::ApplicationRecord
      belongs_to :user, class_name: "Decidim::User", foreign_key: "decidim_user_id"
      belongs_to :survey, class_name: "Survey", foreign_key: "decidim_survey_id"
      belongs_to :question, class_name: "SurveyQuestion", foreign_key: "decidim_survey_question_id"

      validates :body, presence: true, if: -> { question.mandatory? }
      validate :user_survey_same_organization
      validate :question_belongs_to_survey

      private

      def user_survey_same_organization
        return if user&.organization == survey&.organization
        errors.add(:user, :invalid)
      end

      def question_belongs_to_survey
        errors.add(:survey, :invalid) if question&.survey != survey
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
decidim-surveys-0.6.6 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.6 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.5 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.5 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.4 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.4 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.3 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.3 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.2 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.2 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.1 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.1 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.6.0 app/models/decidim/surveys/survey_answer.rb
decidim-0.6.0 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.5.1 app/models/decidim/surveys/survey_answer.rb
decidim-0.5.1 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.5.0 app/models/decidim/surveys/survey_answer.rb
decidim-0.5.0 decidim-surveys/app/models/decidim/surveys/survey_answer.rb
decidim-surveys-0.4.4 app/models/decidim/surveys/survey_answer.rb
decidim-0.4.4 decidim-surveys/app/models/decidim/surveys/survey_answer.rb