Method: AutoPilot::TemplateHelper#parameterize

Defined in:
lib/auto_pilot/template_helper.rb

#parameterize(string, sep = '-') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/auto_pilot/template_helper.rb', line 9

def parameterize(string, sep = '-')
  # replace accented chars with their ascii equivalents
  # parameterized_string = transliterate(string)
  # Turn unwanted chars into the separator
  string.gsub!(/[^a-z0-9\-_]+/i, sep)
  unless sep.nil? || sep.empty?
    re_sep = Regexp.escape(sep)
    # No more than one of the separator in a row.
    string.gsub!(/#{re_sep}{2,}/, sep)
    # Remove leading/trailing separator.
    string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
  end
  string.downcase
end