Sha256: 951d2a5c17ac5f2e75e1fb9a7cb22fe8bfbe07c2b6ad111797bad83754840fd0

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

# Recreate validations to take into account custom fields and ignore the length limit in proposals
module Decidim
  module DecidimAwesome
    module Proposals
      module ProposalWizardCreateStepFormOverride
        extend ActiveSupport::Concern

        included do
          clear_validators!

          validates :title, presence: true, etiquette: true
          validates :title, proposal_length: {
            minimum: ->(form) { form.minimum_title_length },
            maximum: 150
          }

          validates :body, presence: true, unless: ->(form) { form.override_validations? || form.minimum_body_length.zero? }
          validates :body, etiquette: true, unless: ->(form) { form.override_validations? }
          validates :body, proposal_length: {
            minimum: ->(form) { form.minimum_body_length },
            maximum: ->(form) { form.override_validations? ? 0 : form.component.settings.proposal_length }
          }

          validate :body_is_not_bare_template, unless: ->(form) { form.override_validations? }

          def override_validations?
            return false if context.current_component.settings.participatory_texts_enabled

            custom_fields.present?
          end

          def minimum_title_length
            awesome_config.config[:validate_title_min_length].to_i
          end

          def minimum_body_length
            awesome_config.config[:validate_body_min_length].to_i
          end

          def custom_fields
            @custom_fields ||= awesome_config.collect_sub_configs_values("proposal_custom_field")
          end

          def awesome_config
            @awesome_config ||= begin
              conf = Decidim::DecidimAwesome::Config.new(context.current_organization)
              conf.context_from_component(context.current_component)
              conf
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.11.4 app/forms/concerns/decidim/decidim_awesome/proposals/proposal_wizard_create_step_form_override.rb
decidim-decidim_awesome-0.11.3 app/forms/concerns/decidim/decidim_awesome/proposals/proposal_wizard_create_step_form_override.rb
decidim-decidim_awesome-0.11.2 app/forms/concerns/decidim/decidim_awesome/proposals/proposal_wizard_create_step_form_override.rb
decidim-decidim_awesome-0.11.1 app/forms/concerns/decidim/decidim_awesome/proposals/proposal_wizard_create_step_form_override.rb