Sha256: a3fb6b7c779c276799a1b38f49e44133a70ad44c5f9c74d676986ee57a7dd226

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Archangel
  ##
  # User model
  #
  class User < ApplicationRecord
    acts_as_paranoid

    mount_uploader :avatar, Archangel::AvatarUploader

    before_validation :parameterize_username

    after_initialize :column_default

    after_destroy :column_reset

    devise :confirmable, :database_authenticatable, :invitable, :lockable,
           :recoverable, :registerable, :rememberable, :timeoutable, :trackable,
           :validatable

    validates :avatar, file_size: {
      less_than_or_equal_to: Archangel.config.image_maximum_file_size
    }
    validates :email, presence: true, uniqueness: true, email: true
    validates :name, presence: true
    validates :password, presence: true, length: { minimum: 8 }, on: :create
    validates :password, allow_blank: true, length: { minimum: 8 }, on: :update
    validates :role, presence: true, inclusion: { in: Archangel::ROLES }
    validates :username, presence: true, uniqueness: true

    belongs_to :site

    ##
    # Overwrite resource id to `username`
    #
    # @return [String] the aliased resource param
    #
    def to_param
      username
    end

    protected

    def parameterize_username
      self.username = username.to_s.downcase.parameterize
    end

    def column_default
      self.role ||= Archangel::ROLE_DEFAULT
    end

    def column_reset
      now = Time.current.to_i

      self.email = "#{now}_#{email}"
      self.username = "#{now}_#{username}"

      save
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
archangel-0.0.5 app/models/archangel/user.rb
archangel-0.0.4 app/models/archangel/user.rb
archangel-0.0.3 app/models/archangel/user.rb
archangel-0.0.2 app/models/archangel/user.rb