Sha256: 47a74b324c0c3ddea046c57b5f2b0f8c7cd3b19caa6717c3c68fe3de50a71c45

Contents?: true

Size: 1.09 KB

Versions: 29

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module SlashAdmin
  class Admin < ApplicationRecord
    include CanCan::Ability

    has_secure_password

    attr_accessor :login

    EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
    USERNAME_REGEX = /^[a-zA-Z0-9_\.]*$/
    validates :username, presence: true, uniqueness: true, length: {in: 3..20}
    validates :email, presence: true, uniqueness: true
    validates_length_of :password, in: 6..20, on: :create
    validates_format_of :email, with: EMAIL_REGEX, multiline: true
    validates_format_of :username, with: USERNAME_REGEX, multiline: true

    before_create :handle_default_role

    serialize :roles, JSON

    def has_role?(role)
      if roles.blank?
        false
      else
        roles.include?(role)
      end
    end

    def handle_default_role
      self.roles = "superadmin" unless roles.present?
    end

    attr_writer :login

    def login
      @login || username || email
    end

    def identicon
      RubyIdenticon.create_base64(email, grid_size: 5, border_size: 150, square_size: 50, background_color: 0xf0f0f0ff)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
slash_admin-1.5.11 app/models/slash_admin/admin.rb
slash_admin-1.5.10 app/models/slash_admin/admin.rb
slash_admin-1.5.9 app/models/slash_admin/admin.rb
slash_admin-1.5.8 app/models/slash_admin/admin.rb
slash_admin-1.5.7 app/models/slash_admin/admin.rb
slash_admin-1.5.6 app/models/slash_admin/admin.rb
slash_admin-1.5.5 app/models/slash_admin/admin.rb
slash_admin-1.5.4 app/models/slash_admin/admin.rb
slash_admin-1.5.3 app/models/slash_admin/admin.rb
slash_admin-1.5.2 app/models/slash_admin/admin.rb
slash_admin-1.5.1 app/models/slash_admin/admin.rb
slash_admin-1.5 app/models/slash_admin/admin.rb
slash_admin-1.4.1 app/models/slash_admin/admin.rb
slash_admin-1.4 app/models/slash_admin/admin.rb
slash_admin-1.3.5 app/models/slash_admin/admin.rb
slash_admin-1.3.4 app/models/slash_admin/admin.rb
slash_admin-1.3.3 app/models/slash_admin/admin.rb
slash_admin-1.3.2 app/models/slash_admin/admin.rb
slash_admin-1.3.1 app/models/slash_admin/admin.rb
slash_admin-1.3.0 app/models/slash_admin/admin.rb