Module: Acts::Permalink::Conversion

Defined in:
lib/conversion.rb

Class Method Summary collapse

Class Method Details

.convert(string, max_length: nil, separator: "-") ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/conversion.rb', line 5

def convert(string, max_length: nil, separator: "-")
  string = ActiveSupport::Inflector.transliterate(string)  # convert to simplified characters
  string = string.downcase.strip                           # make the string lowercase and scrub white space on either side
  string = string.gsub(/[^a-z0-9]/, separator)             # make any character that is not nupermic or alphabetic into a special character
  string = string.squeeze(separator)                       # removes any consecutive duplicates of the special character
  string = string.sub(Regexp.new("^#{ separator }+"), "")  # remove leading special characters
  string = string.sub(Regexp.new("#{ separator }+$"), "")  # remove trailing special characters
  string = string[0...max_length] if max_length            # trim to length

  string
end