Created
June 20, 2011 15:22
-
-
Save ckdake/1035814 to your computer and use it in GitHub Desktop.
make find_or_create_by methods work right with validations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This and the next method are unfortunately 'magic' | |
# They set default paramaters for these two find_or_create_by_ method_missing methods | |
def self.find_or_create_by_email(params) | |
params = { email: params } unless params.is_a?(Hash) | |
super(params.reverse_merge( | |
password: Digest::MD5.hexdigest("#{rand(1024)}Time.now"), | |
username: Digest::MD5.hexdigest("#{rand(1024)}Time.now") | |
)) | |
end | |
def self.find_or_create_by_username(params) | |
params = { username: params } unless params.is_a?(Hash) | |
super(params.reverse_merge( | |
email: Digest::MD5.hexdigest("#{rand(1024)}Time.now") + "@example.com", | |
password: Digest::MD5.hexdigest("#{rand(1024)}Time.now") | |
)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment