Sha256: 72983d24737c8b8203b36cf3f33ecbc530c5605ae159c9a178f84023c08ddf1e

Contents?: true

Size: 902 Bytes

Versions: 3

Compression:

Stored size: 902 Bytes

Contents

module Ohm
  module Slug
    def self.included(model)
      model.extend ClassMethods
    end

    module ClassMethods
      def [](id)
        super(id.to_i)
      end
    end

    def slug(str = to_s)
      ret = transcode(str)
      ret.gsub!("'", "")
      ret.gsub!(/\p{^Alnum}/u, " ")
      ret.strip!
      ret.gsub!(/\s+/, "-")
      ret.downcase
    end
    module_function :slug

    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
    module_function :transcode

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ohm-contrib-1.0.0.rc2 lib/ohm/slug.rb
ohm-contrib-1.0.rc1 lib/ohm/slug.rb
ohm-contrib-1.0.rc0 lib/ohm/slug.rb