Sha256: f4e9190e98ced96311e612ee809b025dfd0f0a58b0a60e6d7da650b595db4856

Contents?: true

Size: 1.78 KB

Versions: 9

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

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

        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

9 entries across 9 versions & 1 rubygems

Version Path
decidim-elections-0.23.6 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.5 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.4 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.3 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.2 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.1 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.1.rc1 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.23.0 app/forms/decidim/elections/admin/answer_form.rb
decidim-elections-0.22.0 app/forms/decidim/elections/admin/answer_form.rb