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) ⇒ Object

This method does the actual permalink escaping.



21
22
23
24
25
26
27
28
29
# File 'lib/permalink_fu.rb', line 21

def escape(str)
  s = ClassMethods.decode(str)#.force_encoding("UTF-8")
  s.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
  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.size == 0 ? ClassMethods.random_permalink(str) : s
end

Instance Method Details



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

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}.update(options)
  end
  
  include InstanceMethods
end