Sha256: 061a4e33c3a38ca583b04c9015340086ab33102aa4ba14cd294a617d3d49edf4

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module LatoCore
  # This class is the model for superusers.
  class Superuser < ActiveRecord::Base

    # Validations
    ############################################################################

    validates :name, presence: true, length: { maximum: 50 }

    validates :username, presence: true, length: { maximum: 50 },
    uniqueness: { case_sensitive: false }

    validates :email, presence: true, length: { maximum: 255 },
    format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
    uniqueness: { case_sensitive: false }

    validates :permission, presence: true, length: { minimum: 1, maximum: 10 }

    validates :password, presence: true, length: { minimum: 6, maximum: 50 },  on: :create

    has_secure_password

    # Before db update
    ############################################################################

    before_create do
      username.downcase!
      email.downcase!

      set_admin_permission
    end

    before_update do
      username.downcase!
      email.downcase!

      set_admin_permission
    end

    # Private functions
    ############################################################################

    # This function set permission to 10 for first superuser.
    private def set_admin_permission
      first_user = LatoCore::Superuser.first
      self.permission = 10 if first_user && self.id === first_user.id
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lato_core-1.1.4 app/models/lato_core/superuser.rb
lato_core-1.1.3 app/models/lato_core/superuser.rb