Module: PermalinkFu
- Defined in:
- lib/permalink_fu.rb,
lib/permalink_fu/railtie.rb,
lib/permalink_fu/active_record.rb
Defined Under Namespace
Modules: ActiveRecord Classes: Railtie
Class Method Summary collapse
-
.escape(string) ⇒ Object
This method does the actual permalink escaping.
- .iconvify(string) ⇒ Object
- .random_permalink ⇒ Object
Class Method Details
.escape(string) ⇒ Object
This method does the actual permalink escaping.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/permalink_fu.rb', line 15 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 |
.iconvify(string) ⇒ Object
32 33 34 35 |
# File 'lib/permalink_fu.rb', line 32 def iconvify(string) return string unless defined?(Iconv) return Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s end |
.random_permalink ⇒ Object
28 29 30 |
# File 'lib/permalink_fu.rb', line 28 def random_permalink ::ActiveSupport::SecureRandom.hex(16) end |