Sha256: 1752120bc614a7458bf00369642f1b343b314a4bc42ef44b644bb4a0abceba6b

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

module Challah
  module UserAttributeable
    extend ActiveSupport::Concern

    included do
      attr_reader :password
      attr_reader :password_confirmation
      attr_reader :password_updated

      before_save :ensure_user_tokens
    end

    # Returns true if this user is active, and should be able to log in. If
    # the active column is false, the user will not be able to authenticate
    def active?
      !!self.active
    end

    # First name and last name together
    def name
      "#{ first_name } #{ last_name }".strip
    end

    # shortened name, just includes the first name and last initial
    def small_name
      "#{ first_name.to_s.titleize } #{ last_name.to_s.first.upcase }."
    end

    # Is this user valid and ready for a user session?
    #
    # Override this method if you need to check for a particular configuration on each page request.
    def valid_session?
      self.active?
    end

    protected

    # Ensure that all system-generated columns aren't blank on each save
    def ensure_user_tokens
      ensure_api_key_presence
      ensure_email_hash_presence
      ensure_persistence_token_presence
    end

    # Store a random seed for this user's api key
    def ensure_api_key_presence
      if respond_to?("api_key=")
        if self.api_key.to_s.blank?
          self.api_key = Random.token(50)
        end
      end
    end

    # Store a hashed email if the column exists
    def ensure_email_hash_presence
      if respond_to?("email_hash=")
        if email_changed?
          self.email_hash = Encrypter.md5(email.to_s.downcase.strip)
        end
      end
    end

    # Store a random token to identify user in persisted objects
    def ensure_persistence_token_presence
      if respond_to?("persistence_token=")
        if self.persistence_token.to_s.blank?
          self.persistence_token = Random.token(125)
        end
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
challah-1.2.5 lib/challah/concerns/user/attributeable.rb
challah-1.2.5.pre lib/challah/concerns/user/attributeable.rb
challah-1.2.4 lib/challah/concerns/user/attributeable.rb
challah-1.2.3 lib/challah/concerns/user/attributeable.rb
challah-1.2.2 lib/challah/concerns/user/attributeable.rb
challah-1.2.1 lib/challah/concerns/user/attributeable.rb
challah-1.2.0 lib/challah/concerns/user/attributeable.rb
challah-1.2.0.rc lib/challah/concerns/user/attributeable.rb