Module: PermalinkFu

Defined in:
lib/permalink_fu.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(str, klass = nil) ⇒ Object

This method does the actual permalink escaping.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/permalink_fu.rb', line 20

def escape(str, klass = nil)
  if Gem::Version.new("#{RUBY_VERSION}") < Gem::Version.new("1.9")
    require 'iconv'
    s = ClassMethods.decode(str)#.force_encoding("UTF-8")
    s = Iconv.iconv('ascii//ignore//translit', 'utf-8', s).to_s
  else
    s = ClassMethods.decode(str).encode("ascii")
  end
  s.gsub!(/[^\w_ \-]+/i,   '') # Remove unwanted chars.
  s.gsub!(/[ \-]+/i,      '-') # No more than one of the separator in a row.
  s.gsub!(/^\-|\-$/i,      '') # Remove leading/trailing separator.
  s.downcase!
  s = "#{klass}-#{s}" if klass && s.match(/\A\d+\z/)
  s = s.size < klass.classify.constantize.permalink_options[:min_length] ? ClassMethods.random_permalink : s
end

Instance Method Details



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/permalink_fu.rb', line 4

def has_permalink(attr_names = [], permalink_field = nil, options = {})
  if permalink_field.is_a?(Hash)
    options = permalink_field
    permalink_field = nil
  end
  ClassMethods.setup_permalink_fu_on self do
    self.permalink_attributes = Array(attr_names)
    self.permalink_field      = (permalink_field || 'permalink').to_s
    self.permalink_options    = {:unique => true, :min_length => 1}.update(options)
  end

  include InstanceMethods
end