Module: FlexPass::FlexSecurePassword::ClassMethods

Defined in:
lib/flex_pass/flex_secure_password.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#coderObject

Returns the value of attribute coder.



7
8
9
# File 'lib/flex_pass/flex_secure_password.rb', line 7

def coder
  @coder
end

#digest_columnObject

Returns the value of attribute digest_column.



8
9
10
# File 'lib/flex_pass/flex_secure_password.rb', line 8

def digest_column
  @digest_column
end

Instance Method Details

#has_flexible_secure_password(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flex_pass/flex_secure_password.rb', line 10

def has_flexible_secure_password(options = {})

  attr_reader :password
  
  include InstanceMethodsOnActivation

  if options.fetch(:validations, true)
    validates_confirmation_of :password, if: lambda { |m| m.password.present? }
    validates_presence_of     :password, on: :create
    validates_presence_of     :password_confirmation, if: lambda { |m| m.password.present? }

    before_create { raise "Password digest missing on new record" if password_digest.blank? }
  end
  
  if (options.has_key? :coder) && (options[:coder].respond_to? 'create')
    self.coder = options[:coder]
  else
    begin
      gem 'bcrypt-ruby', '~> 3.0.0'
      require 'bcrypt'
    rescue LoadError
      $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install"
      raise
    end
    self.coder = BCrypt::Password
  end
  
  self.digest_column = options.fetch(:digest_column, 'password_digest')
  
  unless self.instance_methods.include? self.digest_column.to_s.to_sym
    unless (self.ancestors.include? ActiveRecord::Base) && (self.attribute_names.include? self.digest_column.to_s)
      raise RuntimeError, "\'#{self.digest_column}\' column is not defined, please select an existing column to store the password digest"
    end
  end

  if respond_to?(:attributes_protected_by_default)
    def self.attributes_protected_by_default #:nodoc:
      super + [self.digest_column]
    end
  end
end