Sha256: c94c5cdb46c67981967c88c3e654e3149db5fe6a61edfd6c3fd26d4a52017694

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    module Admin
      # This class holds a Form to create/update debates from Decidim's admin panel.
      class DebateForm < Decidim::Form
        include TranslatableAttributes

        translatable_attribute :title, String
        translatable_attribute :description, String
        translatable_attribute :instructions, String
        translatable_attribute :information_updates, String
        attribute :start_time, Decidim::Attributes::TimeWithZone
        attribute :end_time, Decidim::Attributes::TimeWithZone
        attribute :decidim_category_id, Integer

        validates :title, translatable_presence: true
        validates :description, translatable_presence: true
        validates :instructions, translatable_presence: true
        validates :start_time, presence: { if: ->(object) { object.end_time.present? } }, date: { before: :end_time, allow_blank: true }
        validates :end_time, presence: { if: ->(object) { object.start_time.present? } }, date: { after: :start_time, allow_blank: true }

        validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? }

        def map_model(model)
          self.decidim_category_id = model.category.try(:id)
        end

        def category
          return unless current_component

          @category ||= current_component.categories.find_by(id: decidim_category_id)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-debates-0.22.0 app/forms/decidim/debates/admin/debate_form.rb
decidim-debates-0.21.0 app/forms/decidim/debates/admin/debate_form.rb
decidim-debates-0.20.1 app/forms/decidim/debates/admin/debate_form.rb
decidim-debates-0.20.0 app/forms/decidim/debates/admin/debate_form.rb
decidim-debates-0.19.1 app/forms/decidim/debates/admin/debate_form.rb
decidim-debates-0.19.0 app/forms/decidim/debates/admin/debate_form.rb