Module: MongoMapper::Plugins::Sluggable::InstanceMethods

Defined in:
lib/mm-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#set_slugObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mm-sluggable.rb', line 33

def set_slug
  options = self.class.slug_options
  return unless self.send(options[:key]).blank?

  to_slug = self[options[:to_slug]]
  return if to_slug.blank?

  the_slug = raw_slug = to_slug.send(options[:method]).to_s[0...options[:max_length]]

  conds = {}
  conds[options[:key]]   = the_slug
  conds[options[:scope]] = self.send(options[:scope]) if options[:scope]

  # todo - remove the loop and use regex instead so we can do it in one query
  i = 0
  while self.class.first(conds)
    i += 1
    conds[options[:key]] = the_slug = "#{raw_slug}-#{i}"
  end

  self.send(:"#{options[:key]}=", the_slug)
end