Class: PandaCms::Slug

Inherits:
Object
  • Object
show all
Defined in:
app/lib/panda_cms/slug.rb

Class Method Summary collapse

Class Method Details

.generate(string) ⇒ Object

Generates a slug from a provided string

See Also:

  • should also implement this logic


9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/lib/panda_cms/slug.rb', line 9

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(/[\!\@\£\$\%\^\&\*\(\)\+\=\{\}\[\]\:\;\"\'\|\\\`\<\>\?\,\.\/]+/, "")
  # Replace any whitespace character with -
  string = string.gsub(/[^\w\s-]/, "-")
  # Replace multiple occurences of _ and - with -
  string.gsub(/[\s_-]+/, "-")
end