Class: Panda::CMS::Slug

Inherits:
Object
  • Object
show all
Defined in:
lib/panda/cms/slug.rb

Class Method Summary collapse

Class Method Details

.generate(string) ⇒ Object

Generates a slug from a provided string

Parameters:

  • string (String)

    The provided string to turn into a slug

Returns:

  • string Generated slug

See Also:

  • should also implement this logic


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/panda/cms/slug.rb', line 12

def self.generate(string)
  # Trim whitespace and downcase the string
  string = string.to_s.strip.downcase
  # Replace & with "and"
  string = string.gsub("&", "and")
  # Remove special characters
  string = string.gsub(%r{[!@£\\\^\[\]/]+}, "")
  # Replace any whitespace character with -
  string = string.gsub(/[^\w\s-]/, "-")
  # Replace multiple occurences of _ and - with -
  string.gsub(/[\s_-]+/, "-")
end