Module: SimpleAuth::ActiveRecord::Macro

Defined in:
lib/simple_auth/active_record.rb

Instance Method Summary collapse

Instance Method Details

#authentication(options = {}, &block) ⇒ Object

Set virtual attributes, callbacks and validations. Is called automatically after setting up configuration with ‘SimpleAuth.setup {|config| config.model = :user}`.

class User < ActiveRecord::Base
  authentication
end

Can set configuration when a block is provided.

class User < ActiveRecord::Base
  authentication do |config|
    config.credentials = ["email"]
  end
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple_auth/active_record.rb', line 24

def authentication(options = {}, &block)
  SimpleAuth.setup(&block) if block_given?
  SimpleAuth::Config.model ||= name.underscore.to_sym

  # Possibly multiple calls in a given model.
  # So, just return.
  return if respond_to?(:authenticate)

  macro = method(:has_secure_password)

  if macro.arity.zero?
    has_secure_password
  else
    has_secure_password(options)
  end

  extend  ClassMethods
  include InstanceMethods

  if options.fetch(:validations, true)
    validates_length_of :password, minimum: 4,
      if: -> { password.present? }
  end
end