Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/active_blog/ext/string.rb
Instance Method Summary collapse
-
#to_slug ⇒ Object
Generate a slug for the string
value
.
Instance Method Details
#to_slug ⇒ Object
Generate a slug for the string value
.
A slug should consist of numbers (0-9), lowercase letters (a-z) and dashes (-). Any other characters should be filtered.
Example
"The World is Beautiful!".to_slug # => "the-world-is-beautiful"
Returns
- String
-
A ‘sluggified’ version of this string
–
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_blog/ext/string.rb', line 16 def to_slug # Perform transliteration to replace non-ascii characters with an ascii # character value = self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s # Remove single quotes from input value.gsub!(/[']+/, '') # Replace any non-word character (\W) with a space value.gsub!(/\W+/, ' ') # Remove any whitespace before and after the string value.strip! # All characters should be downcased value.downcase! # Replace spaces with dashes value.gsub!(' ', '-') # Return the resulting slug value end |