Sha256: baec0831fb1ec74441b134e86e4ccb57911c7e94a6896db19c62ce553cb386e7

Contents?: true

Size: 1.98 KB

Versions: 13

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Decidim
  # The form object that handles the data behind updating a user's
  # account in her profile page.
  class AccountForm < Form
    mimic :user

    attribute :name
    attribute :nickname
    attribute :email
    attribute :password
    attribute :password_confirmation
    attribute :avatar
    attribute :remove_avatar
    attribute :personal_url
    attribute :about

    validates :name, presence: true
    validates :email, presence: true
    validates :nickname, presence: true

    validates :nickname, length: { maximum: Decidim::User.nickname_max_length, allow_blank: true }
    validates :password, confirmation: true
    validates :password, length: { in: Decidim::User.password_length, allow_blank: true }
    validates :password_confirmation, presence: true, if: :password_present
    validates :avatar, file_size: { less_than_or_equal_to: ->(_record) { Decidim.maximum_avatar_size } }

    validate :unique_email
    validate :unique_nickname
    validate :personal_url_format

    def personal_url
      return if super.blank?

      return "http://" + super unless super.match?(%r{\A(http|https)://}i)

      super
    end

    private

    def password_present
      password.present?
    end

    def unique_email
      return true if Decidim::User.where(
        organization: context.current_organization,
        email: email
      ).where.not(id: context.current_user.id).empty?

      errors.add :email, :taken
      false
    end

    def unique_nickname
      return true if Decidim::User.where(
        organization: context.current_organization,
        nickname: nickname
      ).where.not(id: context.current_user.id).empty?

      errors.add :nickname, :taken
      false
    end

    def personal_url_format
      return if personal_url.blank?

      uri = URI.parse(personal_url)
      errors.add :personal_url, :invalid if !uri.is_a?(URI::HTTP) || uri.host.nil?
    rescue URI::InvalidURIError
      errors.add :personal_url, :invalid
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.12.2 app/forms/decidim/account_form.rb
decidim-core-0.12.1 app/forms/decidim/account_form.rb
decidim-core-0.12.0 app/forms/decidim/account_form.rb
decidim-core-0.11.2 app/forms/decidim/account_form.rb
decidim-core-0.12.0.pre app/forms/decidim/account_form.rb
decidim-core-0.11.1 app/forms/decidim/account_form.rb
decidim-core-0.11.0.pre1 app/forms/decidim/account_form.rb
decidim-core-0.10.1 app/forms/decidim/account_form.rb
decidim-core-0.10.0 app/forms/decidim/account_form.rb
decidim-core-0.9.3 app/forms/decidim/account_form.rb
decidim-core-0.9.2 app/forms/decidim/account_form.rb
decidim-core-0.9.1 app/forms/decidim/account_form.rb
decidim-core-0.9.0 app/forms/decidim/account_form.rb