Module: Negroni::Models::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/negroni/models/base.rb

Overview

The ‘Base` module contains methods that should be included in all Negroni models. It holds common settings for all authentication, as well as some shared behavior across all modules.

## Options

Base adds the following options:

* `authentication_keys`: parameters used for authentication.
   Default [:email].

* `strip_whitespace_keys`: keys from which whitespace should be
   stripped prior to validation.  Default [:email].

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SERIALIZATION_BLACKLIST =

Blacklisted attributes for serialization.

[
  :password_digest, :reset_password_token, :reset_password_sent_at,
  :password_salt, :confirmation_token, :confirmed_at,
  :confirmation_sent_at, :failed_attempts, :unlock_token, :locked_at
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(_klass) ⇒ Object

Required fields for this module (none).



38
39
40
# File 'lib/negroni/models/base.rb', line 38

def self.required_fields(_klass)
  []
end

Instance Method Details

#active_for_auth?Boolean

Returns whether or not the including class is active for authentication. This method is primarily intended to be overriden by other modules, including ‘Lockable`.

Returns:

  • (Boolean)


67
68
69
# File 'lib/negroni/models/base.rb', line 67

def active_for_auth?
  true
end

#inactive_messageSymbol

The message that will be populated if an inactive record attempts to authenticate.

Returns:

  • (Symbol)

    the key of the translation to look up



75
76
77
# File 'lib/negroni/models/base.rb', line 75

def inactive_message
  :inactive
end

#inspectObject

Redefine inspect using serializable_hash, to ensure we don’t accidentally leak passwords into exceptions.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/negroni/models/base.rb', line 100

def inspect
  inspection = serializable_hash.collect do |k, v|
    value = if respond_to?(:attribute_for_inspect)
              attribute_for_inspect(k)
            else
              v.inspect
            end

    "#{k}: #{value}"
  end

  "#<#{self.class} #{inspection.join(', ')}>"
end

#serializable_hash(options = nil) ⇒ Object

Redefine serializable_hash in models for more secure defaults. By default, it removes from the serializable model all attributes that are __not__ accessible. You can remove this default by using :force_except and passing a new list of attributes you want to exempt. All attributes given to :except will simply add names to exempt to Negroni internal list.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/negroni/models/base.rb', line 85

def serializable_hash(options = nil)
  options ||= {}
  options[:except] = Array(options[:except])

  if options[:force_except]
    options[:except].concat Array(options[:force_except])
  else
    options[:except].concat SERIALIZATION_BLACKLIST
  end

  super(options)
end

#unauthenticated_messageSymbol

The message that will be populated if an invalid record attempts to authenticate.

Returns:

  • (Symbol)

    the key of the translation to look up



58
59
60
# File 'lib/negroni/models/base.rb', line 58

def unauthenticated_message
  :invalid
end

#valid_for_auth?Boolean

Returns whether or not the including class is active for authentication. This method is primarily intended to be overriden by other modules, including ‘Lockable`.

Yields and returns the return value of the given block, if a block is given. Otherwise returns true.

Returns:

  • (Boolean)


50
51
52
# File 'lib/negroni/models/base.rb', line 50

def valid_for_auth?
  block_given? ? yield : true
end