Class: Slug
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Slug
- Defined in:
- lib/friendly_id/slug.rb
Overview
A Slug is a unique, human-friendly identifier for an ActiveRecord.
Constant Summary collapse
- ASCII_APPROXIMATIONS =
{ 198 => "AE", 208 => "D", 216 => "O", 222 => "Th", 223 => "ss", 230 => "ae", 240 => "d", 248 => "o", 254 => "th" }.freeze
Class Method Summary collapse
-
.normalize(slug_text) ⇒ Object
Sanitizes and dasherizes string to make it safe for URL’s.
- .parse(friendly_id) ⇒ Object
- .postnormalize(string) ⇒ Object
-
.strip_diacritics(string) ⇒ Object
Remove diacritics (accents, umlauts, etc.) from the string.
-
.strip_non_ascii(string) ⇒ Object
Remove non-ascii characters from the string.
Instance Method Summary collapse
-
#is_most_recent? ⇒ Boolean
Whether or not this slug is the most recent of its owner’s slugs.
- #to_friendly_id ⇒ Object
Class Method Details
.normalize(slug_text) ⇒ Object
Sanitizes and dasherizes string to make it safe for URL’s.
Example:
slug.normalize('This... is an example!') # => "this-is-an-example"
Note that the Unicode handling in ActiveSupport may fail to process some characters from Polish, Icelandic and other languages. If your application uses these languages, check out this article for information on how to get better urls in your application.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/friendly_id/slug.rb', line 35 def normalize(slug_text) return "" if slug_text.nil? || slug_text == "" ActiveSupport::Multibyte.proxy_class.new(slug_text.to_s).normalize(:kc). # For some reason Spanish ¡ and ¿ are not detected as non-word # characters. Bug in Ruby? gsub(/[\W|¡|¿]/u, ' '). strip. gsub(/\s+/u, '-'). gsub(/-\z/u, ''). downcase. to_s end |
.parse(friendly_id) ⇒ Object
52 53 54 55 56 |
# File 'lib/friendly_id/slug.rb', line 52 def parse(friendly_id) name, sequence = friendly_id.split('--') sequence ||= "1" return name, sequence end |
.postnormalize(string) ⇒ Object
48 49 50 |
# File 'lib/friendly_id/slug.rb', line 48 def postnormalize(string) string.gs end |
.strip_diacritics(string) ⇒ Object
Remove diacritics (accents, umlauts, etc.) from the string. Borrowed from “The Ruby Way.”
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/friendly_id/slug.rb', line 60 def strip_diacritics(string) ActiveSupport::Multibyte.proxy_class.new(string).normalize(:kd).unpack('U*').inject([]) { |a, u| if ASCII_APPROXIMATIONS[u] a += ASCII_APPROXIMATIONS[u].unpack('U*') elsif (u < 0x300 || u > 0x036F) a << u end a }.pack('U*') end |
.strip_non_ascii(string) ⇒ Object
Remove non-ascii characters from the string.
74 75 76 |
# File 'lib/friendly_id/slug.rb', line 74 def strip_non_ascii(string) strip_diacritics(string).gsub(/[^a-z0-9]+/i, ' ') end |
Instance Method Details
#is_most_recent? ⇒ Boolean
Whether or not this slug is the most recent of its owner’s slugs.
87 88 89 |
# File 'lib/friendly_id/slug.rb', line 87 def is_most_recent? sluggable.slug == self end |
#to_friendly_id ⇒ Object
91 92 93 |
# File 'lib/friendly_id/slug.rb', line 91 def to_friendly_id sequence > 1 ? "#{name}--#{sequence}" : name end |