Sha256: 0a95361cf962a1a627dc17e16e3ce4355664fc760e5f1fb2f7553ed44ed89dde

Contents?: true

Size: 779 Bytes

Versions: 8

Compression:

Stored size: 779 Bytes

Contents

module PandaCms
  class Slug
    # Generates a slug from a provided string
    #
    # @param string [String] The provided string to turn into a slug
    # @return string Generated slug
    # @see text_field_update_controller.js should also implement this logic
    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
panda_cms-0.2.7 app/lib/panda_cms/slug.rb
panda_cms-0.2.6 app/lib/panda_cms/slug.rb
panda_cms-0.2.5 app/lib/panda_cms/slug.rb
panda_cms-0.2.4 app/lib/panda_cms/slug.rb
panda_cms-0.2.3 app/lib/panda_cms/slug.rb
panda_cms-0.2.2 app/lib/panda_cms/slug.rb
panda_cms-0.2.1 app/lib/panda_cms/slug.rb
panda_cms-0.2.0 app/lib/panda_cms/slug.rb