Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Slug
- module InstanceMethods
- def before_create
- self.slug = create_slug name.dup
- super
- end
- def before_update
- self.slug = create_slug name.dup
- super
- end
- private
- def create_slug ret
- #blow away apostrophes
- ret.gsub! /['`]/,""
- # @ --> at, and & --> and
- ret.gsub! /\s*@\s*/, " at "
- ret.gsub! /\s*&\s*/, " and "
- #replace all non alphanumeric, underscore or periods with hyphen
- ret.gsub! /\s*[^A-Za-z0-9\.]\s*/, '-'
- #convert double hyphens to single
- ret.gsub! /_+/,"-"
- #strip off leading/trailing hyphen
- ret.gsub! /\A[-\.]+|[-\.]+\z/,""
- ret
- end
- end
- end
Add Comment
Please, Sign In to add comment