Sha256: 351f48e78f488e6f1341b52e27b16ab4ccaa7e4bb2fff8a84cb35bd08ce7c882

Contents?: true

Size: 927 Bytes

Versions: 6

Compression:

Stored size: 927 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.dup)
      ret.gsub!("'", "")
      ret.gsub!(/[^0-9A-Za-z]/u, " ")
      ret.strip!
      ret.gsub!(/\s+/, "-")
      ret.downcase!
      return ret
    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

6 entries across 6 versions & 1 rubygems

Version Path
ohm-contrib-3.0.0 lib/ohm/slug.rb
ohm-contrib-2.2.0 lib/ohm/slug.rb
ohm-contrib-2.0.1 lib/ohm/slug.rb
ohm-contrib-2.0.0 lib/ohm/slug.rb
ohm-contrib-2.0.0.rc2 lib/ohm/slug.rb
ohm-contrib-2.0.0.rc1 lib/ohm/slug.rb