Sha256: d75f105c1fb4527f13702dfbee4e60aeeff9398500adcecae038b0ad0dd47af8

Contents?: true

Size: 1.02 KB

Versions: 51

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Decidim
  module Consultations
    # This form validates a MultiVote Question
    class MultiVoteForm < Form
      mimic :responses

      attribute :responses, Array[Integer]

      validate :valid_num_of_votes
      validate :valid_responses

      def vote_forms
        @vote_forms ||= responses.map do |response_id|
          VoteForm.from_params(decidim_consultations_response_id: response_id)
        end
      end

      private

      def valid_num_of_votes
        return if responses.count.between?(context.current_question.min_votes, context.current_question.max_votes)

        errors.add(
          :responses,
          I18n.t("activerecord.errors.models.decidim/consultations/vote.attributes.question.invalid_num_votes")
        )
      end

      def valid_responses
        return if vote_forms.all?(&:valid?)

        errors.add(
          :responses,
          I18n.t("decidim_consultations_response_id.not_found", scope: "activemodel.errors.vote")
        )
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
decidim-consultations-0.27.9 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.8 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.7 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.6 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.10 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.9 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.5 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.8 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.4 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.3 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.7 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.5 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.4 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.0 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.3 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.0.rc2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.27.0.rc1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.2 app/forms/decidim/consultations/multi_vote_form.rb