Sha256: 1be136249da8c6d92709673aca2c1c7e6a90ff65fe920ad8c752b94e456955e3

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    module Admin
      class ParticipantForm < Form
        mimic :participant

        attribute :email, String
        attribute :phone, String
        attribute :decidim_action_delegator_ponderation_id, Integer
        attribute :weight, String

        validates :email, presence: true, if: ->(form) { form.authorization_method.in? %w(email both) }
        validates :phone, presence: true, if: ->(form) { form.authorization_method.in? %w(phone both) }
        validate :ponderation_belongs_to_setting

        # When there's a phone number, sanitize it allowing only numbers and +.
        def phone
          return unless super

          super.gsub(/[^+0-9]/, "")
        end

        def setting
          @setting ||= context[:setting]
        end

        def authorization_method
          @authorization_method ||= setting&.authorization_method
        end

        private

        def ponderation_belongs_to_setting
          return if decidim_action_delegator_ponderation_id.blank?
          return if setting.ponderations.where(id: decidim_action_delegator_ponderation_id).any?

          errors.add(:decidim_action_delegator_ponderation_id, :invalid)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-action_delegator-0.8.1 app/forms/decidim/action_delegator/admin/participant_form.rb
decidim-action_delegator-0.7.2 app/forms/decidim/action_delegator/admin/participant_form.rb
decidim-action_delegator-0.7.1 app/forms/decidim/action_delegator/admin/participant_form.rb
decidim-action_delegator-0.7.0 app/forms/decidim/action_delegator/admin/participant_form.rb