Sha256: ea1cd22feafc5ff2f9d4350a3239e054ae156a80a3ed49a66835c6cbee4c3fb7

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Admin
      # A form object used to show the initiative data in the administration
      # panel.
      class InitiativeForm < Form
        include TranslatableAttributes

        mimic :initiative

        translatable_attribute :title, String
        translatable_attribute :description, String
        attribute :type_id, Integer
        attribute :decidim_scope_id, Integer
        attribute :signature_type, String
        attribute :signature_start_time, Date
        attribute :signature_end_time, Date
        attribute :hashtag, String
        attribute :offline_votes, Integer

        translatable_attribute :answer, String
        attribute :answer_url, String

        validates :title, :description, presence: true
        validates :signature_type, presence: true
        validates :signature_start_time, presence: true, if: ->(form) { form.context.initiative.published? }
        validates :signature_end_time, presence: true, if: ->(form) { form.context.initiative.published? }
        validates :signature_end_time, date: { after: :signature_start_time }, if: lambda { |form|
          form.signature_start_time.present? && form.signature_end_time.present?
        }

        validates :answer, translatable_presence: true, if: ->(form) { form.context.initiative.accepted? }
        validates :answer_url, presence: true, if: ->(form) { form.context.initiative.accepted? }

        validates :offline_votes,
                  numericality: {
                    only_integer: true,
                    greater_than: 0
                  }, allow_blank: true

        def map_model(model)
          self.type_id = model.type.id
          self.decidim_scope_id = model.scope.id
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-initiatives-0.13.1 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.12.2 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.13.0 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.12.1 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.13.0.pre1 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.12.0 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.11.2 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.12.0.pre app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.11.1 app/forms/decidim/initiatives/admin/initiative_form.rb
decidim-initiatives-0.11.0.pre1 app/forms/decidim/initiatives/admin/initiative_form.rb