Sha256: 1fafd79cba68a090875589e6cbdd00ad44ac56735c20955af0b5ebde0684d998

Contents?: true

Size: 1.45 KB

Versions: 37

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'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/election_form.rb
decidim-elections-0.27.8 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.7 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.6 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.10 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.9 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.5 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.8 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.4 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.3 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.7 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.5 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.2 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.1 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.4 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.0 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.3 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.0.rc2 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.27.0.rc1 app/forms/decidim/elections/admin/election_form.rb
decidim-elections-0.26.2 app/forms/decidim/elections/admin/election_form.rb