class FoundersToolkit::Auth::Securable::Validations::ProtectedValidator

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/founders_toolkit/auth/securable/validations/protected_validator.rb, line 5
def initialize(options)
  super({ case_sensitive: true }.merge!(options))
  setup! options[:class]
end

Public Instance Methods

validate_each(record, attribute, _value) click to toggle source
# File lib/founders_toolkit/auth/securable/validations/protected_validator.rb, line 10
def validate_each(record, attribute, _value)
  return if record.new_record?
  return unless attribute_changed?(record, attribute)
  return if authenticate?(record)

  human_attribute_name = record.class.human_attribute_name(attribute)
  record.errors.add(
    :current_password,
    "Your current password is required to update your #{human_attribute_name}"
  )
end

Private Instance Methods

attribute_changed?(record, attribute) click to toggle source
# File lib/founders_toolkit/auth/securable/validations/protected_validator.rb, line 28
def attribute_changed?(record, attribute)
  attribute = "#{attribute}_digest" if options[:secure]
  record.public_send("#{attribute}_changed?")
end
authenticate?(record) click to toggle source
# File lib/founders_toolkit/auth/securable/validations/protected_validator.rb, line 33
def authenticate?(record)
  return true if record.try(:reset_password_token?)

  fresh_record = record.class.find(record.id)
  fresh_record.authenticate(record.current_password)
end
setup!(klass) click to toggle source
# File lib/founders_toolkit/auth/securable/validations/protected_validator.rb, line 24
def setup!(klass)
  klass.attr_accessor :current_password
end