Sha256: fa3d625b83410aa5e9f31fd756cb168d4b50290b177fa2781a00186817e5f855

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require File.join(File.dirname(__FILE__), 'permalink_fu', 'railtie')
require File.join(File.dirname(__FILE__), 'permalink_fu', 'active_record')

begin
  require 'iconv'
rescue Object
  puts "No Iconv found, you might want to look into it."
end

module PermalinkFu

  class << self

    # This method does the actual permalink escaping.
    def escape(string)
      result = ::ActiveSupport::Inflector.transliterate(string.to_s)
      result = self.iconvify(result)
      result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
      result.gsub!(/[^\w_ \-]+/i,   '') # Remove unwanted chars.
      result.gsub!(/[ \-]+/i,      '-') # No more than one of the separator in a row.
      result.gsub!(/^\-|\-$/i,      '') # Remove leading/trailing separator.
      result.downcase!
      result.size.zero? ? random_permalink : result
    rescue
      random_permalink
    end

    def random_permalink
      ::ActiveSupport::SecureRandom.hex(16)
    end

    def iconvify(string)
      return string unless defined?(Iconv)
      return Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sayso-permalink_fu-0.0.1.001 lib/permalink_fu.rb