Module: Ohm::Slug

Defined in:
lib/ohm/slug.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



3
4
5
# File 'lib/ohm/slug.rb', line 3

def self.included(model)
  model.extend ClassMethods
end

.slug(str = to_s) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/ohm/slug.rb', line 13

def slug(str = to_s)
  ret = transcode(str.dup)
  ret.gsub!("'", "")
  ret.gsub!(/[^0-9A-Za-z]/u, " ")
  ret.strip!
  ret.gsub!(/\s+/, "-")
  ret.downcase!
  return ret
end

.transcode(str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ohm/slug.rb', line 24

def transcode(str)
  begin
    # TODO: replace with a String#encode version which will
    # contain proper transliteration tables. For now, Iconv
    # still wins because we get that for free.
    v, $VERBOSE = $VERBOSE, nil
    require "iconv"
    $VERBOSE = v

    Iconv.iconv("ascii//translit//ignore", "utf-8", str)[0]
  rescue LoadError
    return str
  end
end

Instance Method Details

#to_paramObject



40
41
42
# File 'lib/ohm/slug.rb', line 40

def to_param
  "#{ id }-#{ slug }"
end