Module: Motr::Mods::Sluggable

Extended by:
Motr::Modding
Defined in:
lib/motr/mods/sluggable.rb

Overview

Sluggable generates a url-friendly “slug” from a particular attribute.

Examples:

Slugify a “title” attribute into the “slug” attribute when new instances are saved

modded_with :sluggable, :fields => :title, :on => :create, :as => :slug

Slugify a “title” as a “slug” and a “subtitle” into itself

modded_with :sluggable, :fields => [:title, :subtitle], :as => [:slug]

Instance Method Summary collapse

Methods included from Motr::Modding

append_features, apply, configure, options

Instance Method Details

#_create_slugged_fieldsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/motr/mods/sluggable.rb', line 27

def _create_slugged_fields

  slug_opts   = self.class.sluggable_mod_options
  slug_fields = slug_opts[:fields]
  slug_as     = [slug_opts[:as]].flatten
  
  [slug_fields].flatten.map(&:to_sym).each_with_index do |atr, i|

    next if self.send(atr).nil?          
    unless slug_as.empty? || slug_as[i].nil?
      self.send(:"#{slug_as[i]}=", self.send(atr).to_slug)
    else
      self.send(:"#{atr.to_s}=", self.send(atr).to_slug)
    end          
  end
  
end