Sha256: c7fd3aa18a1a6e3da09304f1badead4026437b9258afde0362c033e5218e1e22

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

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

        translatable_attribute :title, String
        translatable_attribute :description, String
        attribute :start_time, Decidim::Attributes::TimeWithZone
        attribute :end_time, Decidim::Attributes::TimeWithZone
        attribute :attachment, AttachmentForm
        attribute :photos, Array[String]
        attribute :add_photos, Array

        validates :title, translatable_presence: true
        validates :description, translatable_presence: true
        validates :start_time, presence: true, date: { before: :end_time }
        validates :end_time, presence: true, date: { after: :start_time }
        validate :notify_missing_attachment_if_errored

        private

        # This method will add an error to the `photos` 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

8 entries across 8 versions & 1 rubygems

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