Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created July 14, 2010 17:51
Show Gist options
  • Save RStankov/475751 to your computer and use it in GitHub Desktop.
Save RStankov/475751 to your computer and use it in GitHub Desktop.
# fix for send_reset_password_instructions with different authentication_keys than email
module Devise
# lib/devise/models/authenticatable.rb
module Models
def find_or_initialize_with_errors(attributes, error=:invalid)
attributes = attributes.slice(*authentication_keys)
attributes.delete_if { |k, v| !v.present? }
if attributes.size == authentication_keys.size
record = find(:first, :conditions => attributes)
end
unless record
record = new
record.send(:attributes=, attributes, false)
if attributes.size == authentication_keys.size
record.errors.add_to_base(error)
else
authentication_keys.reject { |k| attributes[k].present? }.each do |attribute|
add_error_on(record, attribute, :blank, false)
end
end
end
record
end
# lib/devise/models/recoverable.rb
module Recoverable
module ClassMethods
def send_reset_password_instructions(attributes={})
recoverable = find_or_initialize_with_errors(attributes, I18n.t('devise.record.invalid'))
recoverable.send_reset_password_instructions unless recoverable.new_record?
recoverable
end
end
end
# lib/devise/models/locable.rb
module Lockable
module ClassMethods
def send_unlock_instructions(attributes={})
lockable = find_or_initialize_with_errors(attributes, I18n.t('devise.record.invalid'))
lockable.resend_unlock_token unless lockable.new_record?
lockable
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment