Sha256: 4d56b25ff96671b6e55cd11ef283b15a93a88dbf502101dc8eef921aec4d748b

Contents?: true

Size: 905 Bytes

Versions: 11

Compression:

Stored size: 905 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!(/[^0-9A-Za-z]/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

11 entries across 11 versions & 1 rubygems

Version Path
ohm-contrib-2.0.0.alpha5 lib/ohm/slug.rb
ohm-contrib-2.0.0.alpha4 lib/ohm/slug.rb
ohm-contrib-2.0.0.alpha3 lib/ohm/slug.rb
ohm-contrib-2.0.0.alpha2 lib/ohm/slug.rb
ohm-contrib-1.2 lib/ohm/slug.rb
ohm-contrib-1.1.0 lib/ohm/slug.rb
ohm-contrib-1.0.1 lib/ohm/slug.rb
ohm-contrib-1.0.0 lib/ohm/slug.rb
ohm-contrib-1.0.0.rc5 lib/ohm/slug.rb
ohm-contrib-1.0.0.rc4 lib/ohm/slug.rb
ohm-contrib-1.0.0.rc3 lib/ohm/slug.rb