Sha256: 12adec556ff1879a4542f335c2cdda3c913b2c5b70be07009929d16b9ea3abe8

Contents?: true

Size: 1.13 KB

Versions: 37

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Decidim
  # A form object used to invite users to an organization.
  #
  class InviteUserForm < Form
    mimic :user

    attribute :email, String
    attribute :name, String
    attribute :invitation_instructions, String
    attribute :organization, Decidim::Organization
    attribute :invited_by, Decidim::User
    attribute :role, String

    validates :email, :name, :organization, :invitation_instructions, presence: true
    validates :role, inclusion: { in: Decidim::User::Roles.all }

    validates :name, format: { with: /\A(?!.*[<>?%&\^*#@()\[\]=+:;"{}\\|])/ }
    validate :admin_uniqueness

    def email
      super&.downcase
    end

    def organization
      super || current_organization
    end

    def invited_by
      super || current_user
    end

    def available_roles_for_select
      Decidim::User::Roles.all.map do |role|
        [
          I18n.t("models.user.fields.roles.#{role}", scope: "decidim.admin"),
          role
        ]
      end
    end

    private

    def admin_uniqueness
      errors.add(:email, :taken) if organization.admins.exists?(email: email)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.8 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.7 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.6 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.10 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.9 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.5 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.8 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.4 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.3 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.7 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.5 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.2 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.1 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.4 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.0 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.3 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.0.rc2 app/forms/decidim/invite_user_form.rb
decidim-core-0.27.0.rc1 app/forms/decidim/invite_user_form.rb
decidim-core-0.26.2 app/forms/decidim/invite_user_form.rb