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

Defined in:
lib/mm-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#set_slugObject



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

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

  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



66
67
68
69
# File 'lib/mm-sluggable.rb', line 66

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