Sha256: 53c59dd79eb7a1a34bf9b977874e39e24c262cd9bc1005b581249efd670a030f

Contents?: true

Size: 1.45 KB

Versions: 7

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
        include AttachmentAttributes

        translatable_attribute :title, String
        translatable_attribute :description, String
        attribute :start_time, Decidim::Attributes::TimeWithZone
        attribute :end_time, Decidim::Attributes::TimeWithZone
        attribute :attachment, AttachmentForm

        attachments_attribute :photos

        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 is
        # 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

7 entries across 7 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.3 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.2 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.1 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.0 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.0.rc5 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.28.0.rc4 app/forms/decidim/elections/admin/election_form.rb