Sha256: c9821a37b0c28a8182fed285133ca21c5a5209e5be54be388dd634f65ecf6504

Contents?: true

Size: 1.84 KB

Versions: 8

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Decidim
  # A form object used to handle user registrations
  class RegistrationForm < Form
    mimic :user

    attribute :name, String
    attribute :nickname, String
    attribute :email, String
    attribute :password, String
    attribute :password_confirmation, String
    attribute :newsletter, Boolean
    attribute :tos_agreement, Boolean
    attribute :current_locale, String

    validates :name, presence: true, format: { with: Decidim::User::REGEXP_NAME }
    validates :nickname, presence: true, format: { with: Decidim::User::REGEXP_NICKNAME }, length: { maximum: Decidim::User.nickname_max_length }
    validates :email, presence: true, "valid_email_2/email": { disposable: true }
    validates :password, confirmation: true
    validates :password, password: { name: :name, email: :email, username: :nickname }
    validates :password_confirmation, presence: true
    validates :tos_agreement, allow_nil: false, acceptance: true

    validate :email_unique_in_organization
    validate :nickname_unique_in_organization
    validate :no_pending_invitations_exist

    def newsletter_at
      return nil unless newsletter?

      Time.current
    end

    private

    def email_unique_in_organization
      errors.add :email, :taken if valid_users.find_by(email: email, organization: current_organization).present?
    end

    def nickname_unique_in_organization
      return false unless nickname

      errors.add :nickname, :taken if valid_users.find_by("LOWER(nickname)= ? AND decidim_organization_id = ?", nickname.downcase, current_organization.id).present?
    end

    def valid_users
      UserBaseEntity.where(invitation_token: nil)
    end

    def no_pending_invitations_exist
      errors.add :base, I18n.t("devise.failure.invited") if User.has_pending_invitations?(current_organization.id, email)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.26.8 app/forms/decidim/registration_form.rb
decidim-core-0.27.4 app/forms/decidim/registration_form.rb
decidim-core-0.27.3 app/forms/decidim/registration_form.rb
decidim-core-0.26.7 app/forms/decidim/registration_form.rb
decidim-core-0.26.5 app/forms/decidim/registration_form.rb
decidim-core-0.27.2 app/forms/decidim/registration_form.rb
decidim-core-0.27.1 app/forms/decidim/registration_form.rb
decidim-core-0.26.4 app/forms/decidim/registration_form.rb