Module: DataMapper::Is::Slug
- Included in:
- Resource::ClassMethods
- Defined in:
- lib/dm-is-slug/is/slug.rb,
lib/dm-is-slug/is/version.rb
Defined Under Namespace
Modules: AliasMethods, ClassMethods, InstanceMethods Classes: InvalidSlugSource
Constant Summary collapse
- DEFAULT_SLUG_SIZE =
50
- DEFAULT_SLUG_OPTIONS =
{ :mutable => true, :separator => '_' }
- VERSION =
"0.9.11"
Class Method Summary collapse
-
.escape(string) ⇒ String
An URL-safe string.
Instance Method Summary collapse
-
#is_slug(options) ⇒ Object
Defines a
slug
property on your model with the same size as your source property.
Class Method Details
.escape(string) ⇒ String
Returns an URL-safe string.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dm-is-slug/is/slug.rb', line 22 def self.escape(string) separator = DEFAULT_SLUG_OPTIONS[:separator] # swap accented characters with their counterparts string.gsub!(/[èÈééÉêÊëË]/,'e') string.gsub!(/[àÀáÁâÂãÃäÄåÅ]/,'a') string.gsub!(/[ìÌíÍîÎïÏ]/,'i') string.gsub!(/[òÒóÓöÖôÔõÕøØ]/,'o') string.gsub!(/[ùÙúÚûÛüÜ]/,'u') string.gsub!(/[ýÝÿ]/,'y') string.gsub!(/[çÇ]/,'c') string.gsub!(/[ñÑ]/,'n') string.gsub!(/[Ð]/,'d') result = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). result.strip! result.downcase! result.gsub!(/\b&\b/, 'and') # Change & to a more slug-friendly character. result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars. result.gsub!(/\ +/, separator) # contract multiple spaces. result.gsub!(Regexp.new("#{separator}+"), separator) # No more than one separator in a row. # result.gsub!(/_+/i, separator) result.gsub!(Regexp.new("^#{separator}|#{separator}$"), separator) # Remove leading/trailing separators. # result.gsub!(/^_|_$/i, '') result end |
Instance Method Details
#is_slug(options) ⇒ Object
Defines a slug
property on your model with the same size as your source property. This property is Unicode escaped, and treated so as to be fit for use in URLs.
Example
Suppose your source attribute was the following string: “Hot deals on Boxing Day”. This string would be escaped to “hot_deals_on_boxing_day”.
Non-ASCII characters are attempted to be converted to their nearest approximate.
Parameters
mutable
-
If a slug is mutable it will be updated as the source field changes. Setting this to false will make the slug immutable (permanent)
source
-
The property on the model to use as the source of the generated slug, or an instance method defined in the model, the method must return a string or nil.
size
-
The length of the
slug
property
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dm-is-slug/is/slug.rb', line 78 def is_slug() extend DataMapper::Is::Slug::ClassMethods include DataMapper::Is::Slug::InstanceMethods @slug_options = DEFAULT_SLUG_OPTIONS.merge() raise InvalidSlugSource('You must specify a :source to generate slug.') unless slug_source [:size] ||= get_slug_size property(:slug, String, :size => [:size], :unique => true) unless slug_property before :save, :generate_slug # add alternate slug names for nested resources # e.g. /forums/:forum_slug/topics/:topic_slug/ class_eval <<-SLUG def #{self.new.class.to_s.snake_case}_slug slug end def #{self.new.class.to_s.snake_case}_slug=(str) self.slug = str end SLUG end |