Sha256: 6d1978bb3939237ad8ee2bbbeafcd1e82328c0461b472701f556731e66800aac

Contents?: true

Size: 1.96 KB

Versions: 25

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A form object used to attach a component to a participatory process from the
    # admin panel.
    #
    class ComponentForm < Decidim::Form
      include TranslatableAttributes

      mimic :component

      translatable_attribute :name, String
      validates :name, translatable_presence: true

      attribute :weight, Integer, default: 0

      attribute :manifest, Object
      attribute :participatory_space, Object
      validates :manifest, :participatory_space, presence: true

      attribute :settings, Object
      attribute :default_step_settings, Object
      attribute(:step_settings, { String => Object })

      attribute :share_tokens, Array[ShareToken]

      validate :validate_settings, :validate_step_settings

      def settings?
        settings.manifest.attributes.any?
      end

      def default_step_settings?
        default_step_settings.manifest.attributes.any?
      end

      def map_model(model)
        self.share_tokens = model.share_tokens
      end

      private

      def validate_settings
        return unless errors.empty? && settings_errors_empty? # Preserves errors from custom validation methods

        attributes.each do |key, value|
          next unless value.respond_to?(:valid?)

          errors.add(key, :invalid) unless value.valid?
        end
      end

      def validate_step_settings
        return unless step_settings.respond_to?(:attributes)

        errors.add(:step_settings, :invalid) unless step_settings.attributes.values.all? { |v| !v.respond_to?(:valid?) || v.valid? }
      end

      def settings_errors_empty?
        validations = [settings.errors.empty?]
        validations << if default_step_settings.present?
                         default_step_settings.errors.empty?
                       else
                         step_settings.each_value.map(&:errors).all?(&:empty?)
                       end
        validations.all?
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.4 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.9 app/forms/decidim/admin/component_form.rb
decidim-admin-0.29.0 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.3 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.8 app/forms/decidim/admin/component_form.rb
decidim-admin-0.29.0.rc4 app/forms/decidim/admin/component_form.rb
decidim-admin-0.29.0.rc3 app/forms/decidim/admin/component_form.rb
decidim-admin-0.29.0.rc2 app/forms/decidim/admin/component_form.rb
decidim-admin-0.29.0.rc1 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.2 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.7 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.1 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.6 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.0 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.5 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.0.rc5 app/forms/decidim/admin/component_form.rb
decidim-admin-0.28.0.rc4 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.4 app/forms/decidim/admin/component_form.rb
decidim-admin-0.27.3 app/forms/decidim/admin/component_form.rb