Guest User

Untitled

a guest
Jun 4th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. # Monkey patch for datamapper version 0.9.10
  2. #
  3. # Allow the validates_present validator
  4. # to check for blank BCryptHash passwords.
  5. #
  6. # class User
  7. # include DataMapper::Resource
  8. # property :id, Serial
  9. # property :password, BCryptHash
  10. # validates_present :password
  11. # end
  12. #
  13. # user = User.new
  14. # user.password = nil; user.valid? => false
  15. # user.password = ""; user.valid? => false
  16. # user.password = " "; user.valid? => false
  17. # user.password = "x"; user.valid? => true
  18. # user.password = " x "; user.valid? => true
  19. # user.password == " x " => true
  20. # user.password == "x" => false
  21. #
  22.  
  23. require 'dm-types/bcrypt_hash'
  24. module DataMapper
  25. module Types
  26. class BCryptHash
  27. class << self
  28. alias_method :old_non_blank_check_self_typecast, :typecast
  29. end
  30. def self.typecast(value, property)
  31. value = nil if value.respond_to?(:blank?) && value.blank?
  32. old_non_blank_check_self_typecast(value, property)
  33. end
  34. end
  35. end
  36. end
Add Comment
Please, Sign In to add comment