Sha256: 189846edc85bd07cde21e6c73b94958ee2365257c7522fe2fc147c6b4b1de5a2

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    # This class holds a Form to save the questionnaire answers from Decidim's public page
    class AnswerForm < Decidim::Form
      include Decidim::TranslationsHelper

      attribute :question_id, String
      attribute :body, String
      attribute :choices, Array[AnswerChoiceForm]

      validates :body, presence: true, if: :mandatory_body?
      validates :selected_choices, presence: true, if: :mandatory_choices?

      validate :max_choices, if: -> { question.max_choices }
      validate :all_choices, if: -> { question.question_type == "sorting" }

      delegate :mandatory_body?, :mandatory_choices?, to: :question

      attr_writer :question

      def question
        @question ||= Decidim::Forms::Question.find(question_id)
      end

      def label(idx)
        base = "#{idx + 1}. #{translated_attribute(question.body)}"
        base += " #{mandatory_label}" if question.mandatory?
        base += " (#{max_choices_label})" if question.max_choices
        base
      end

      # Public: Map the correct fields.
      #
      # Returns nothing.
      def map_model(model)
        self.question_id = model.decidim_question_id
        self.question = model.question

        self.choices = model.choices.map do |choice|
          AnswerChoiceForm.from_model(choice)
        end
      end

      def selected_choices
        choices.select(&:body)
      end

      private

      def max_choices
        errors.add(:choices, :too_many) if selected_choices.size > question.max_choices
      end

      def all_choices
        errors.add(:choices, :missing) if selected_choices.size != question.number_of_options
      end

      def mandatory_label
        "*"
      end

      def max_choices_label
        I18n.t("questionnaires.question.max_choices", scope: "decidim.forms", n: question.max_choices)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-forms-0.21.0 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.20.1 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.20.0 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.19.1 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.18.1 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.19.0 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.17.2 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.18.0 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.17.1 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.16.1 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.17.0 app/forms/decidim/forms/answer_form.rb
decidim-forms-0.16.0 app/forms/decidim/forms/answer_form.rb