Sha256: 7e218908a880554f9069f4f702a42bf135303656a7ded5e64857352418f774cf

Contents?: true

Size: 828 Bytes

Versions: 3

Compression:

Stored size: 828 Bytes

Contents

module Panda
  module CMS
    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 slug_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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panda-cms-0.7.3 lib/panda/cms/slug.rb
panda-cms-0.7.2 lib/panda/cms/slug.rb
panda-cms-0.7.0 lib/panda/cms/slug.rb