Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. module Slug
  2. module InstanceMethods
  3. def before_create
  4. self.slug = create_slug name.dup
  5. super
  6. end
  7.  
  8. def before_update
  9. self.slug = create_slug name.dup
  10. super
  11. end
  12.  
  13. private
  14.  
  15. def create_slug ret
  16. #blow away apostrophes
  17. ret.gsub! /['`]/,""
  18.  
  19. # @ --> at, and & --> and
  20. ret.gsub! /\s*@\s*/, " at "
  21. ret.gsub! /\s*&\s*/, " and "
  22.  
  23. #replace all non alphanumeric, underscore or periods with hyphen
  24. ret.gsub! /\s*[^A-Za-z0-9\.]\s*/, '-'
  25.  
  26. #convert double hyphens to single
  27. ret.gsub! /_+/,"-"
  28.  
  29. #strip off leading/trailing hyphen
  30. ret.gsub! /\A[-\.]+|[-\.]+\z/,""
  31.  
  32. ret
  33. end
  34. end
  35. end
Add Comment
Please, Sign In to add comment