Sha256: 4edea1941fbe97c4aeb1f5fad7f46d892c83ef57a9460466eee2385c4660d5f2

Contents?: true

Size: 1.78 KB

Versions: 37

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Admin
      # This class holds a Form to create/update answers from Decidim's admin panel.
      class AnswerForm < Decidim::Form
        include TranslatableAttributes
        include AttachmentAttributes

        translatable_attribute :title, String
        translatable_attribute :description, String
        attribute :proposal_ids, Array[Integer]
        attribute :attachment, AttachmentForm
        attribute :weight, Integer, default: 0

        attachments_attribute :photos

        validates :title, translatable_presence: true
        validate :notify_missing_attachment_if_errored

        def map_model(model)
          self.proposal_ids = model.linked_resources(:proposals, "related_proposals").pluck(:id)
        end

        def proposals
          @proposals ||= Decidim.find_resource_manifest(:proposals)
                                .try(:resource_scope, current_component)
                                &.where(id: proposal_ids)
                                &.order(title: :asc)
        end

        def election
          @election ||= context[:election]
        end

        def question
          @question ||= context[:question]
        end

        private

        # This method will add an error to the `attachment` field only if there's
        # any error in any other field. This is needed because when the form has
        # an error, the attachment is lost, so we need a way to inform the user of
        # this problem.
        def notify_missing_attachment_if_errored
          errors.add(:attachment, :needs_to_be_reattached) if errors.any? && attachment.present?
          errors.add(:add_photos, :needs_to_be_reattached) if errors.any? && add_photos.present?
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

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