Sha256: ed15108cbd1a9430523a0fe2ebdac6cd24838f50fd90fcda07557f794afd5a5a

Contents?: true

Size: 771 Bytes

Versions: 3

Compression:

Stored size: 771 Bytes

Contents

class Invite < ActiveRecord::Base
  include PagesCore::HasRoles

  belongs_to :user
  has_many :roles, class_name: "InviteRole", dependent: :destroy

  before_validation :ensure_token

  validates :user_id, presence: true

  validates :email,
            presence: true,
            format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i },
            uniqueness: { case_sensitive: false }

  validates :token, presence: true

  def create_user(attributes)
    user = User.create(attributes.merge(
                         role_names: role_names,
                         creator: self.user,
                         activated: true
    ))
    destroy if user.valid?
    user
  end

  private

  def ensure_token
    self.token ||= SecureRandom.hex(32)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/models/invite.rb
pages_core-3.5.1 app/models/invite.rb
pages_core-3.4.2 app/models/invite.rb