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.26.1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.0 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.0.rc2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.26.0.rc1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.0 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.0.rc4 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.0.rc3 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.0.rc2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.25.0.rc1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.3 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.23.6 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.23.5 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.1 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.0 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.0.rc2 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.23.4 app/forms/decidim/consultations/multi_vote_form.rb
decidim-consultations-0.24.0.rc1 app/forms/decidim/consultations/multi_vote_form.rb