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]
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
|