Module: Cadmus::Slugs

Defined in:
lib/cadmus/slugs.rb

Defined Under Namespace

Modules: HasSlug

Constant Summary collapse

SLUG_REGEX =

Tests whether a string is a valid Cadmus slug or not. A valid Cadmus slug consists of one or more valid slug parts separated by forward slashes. A valid slug part consists of a lower-case letter followed by any combination of lower-case letters, digits, and hyphens.

For example, +about-us/people+, +special-deals+, and +winter-2012+ are all valid slugs, but +3-things+, +123+, +nobody-lives-here!+, and +/root-page+ aren't.

/\A([a-z][a-z0-9\-]*\/)*[a-z][a-z0-9\-]*\z/

Class Method Summary collapse

Class Method Details

.slugify(string) ⇒ Object

Converts a string to a valid slug part by changing all whitespace to hyphens, converting all upper-case letters to lower-case, removing all remaining non-alphanumeric, non-hyphen characters, and removing any non-alphabetical characters at the beginning of the string.

For example:

  • "Katniss Everdeen" becomes "katniss-everdeen"
  • "21 guns" becomes "guns"
  • "We love you, Conrad!!!1" becomes "we-love-you-conrad1"


21
22
23
# File 'lib/cadmus/slugs.rb', line 21

def self.slugify(string)
  string.to_s.downcase.gsub(/\s+/, '-').gsub(/[^a-z0-9\-]/, '').sub(/\A[^a-z]+/, '')
end