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

Defined in:
lib/mm-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#set_slugObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mm-sluggable.rb', line 44

def set_slug
  options = self.class.slug_options
  need_set_slug = self.send(options[:key]).blank? || (options[:force] && self.send(:"#{options[:to_slug]}_changed?"))
  return unless need_set_slug

  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

#to_paramObject



68
69
70
71
# File 'lib/mm-sluggable.rb', line 68

def to_param
  options = self.class.slug_options
  ( self.send(options[:key]) || self.id ).to_s
end