Module: Slugable

Extended by:
ActiveSupport::Concern
Defined in:
lib/slugable_taylor.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#confirm_slug_uniqueness(str, other_slugs) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/slugable_taylor.rb', line 20

def confirm_slug_uniqueness(str, other_slugs)
  loop do
    return str unless other_slugs.include?(str)
    str = str.split('-')
    str << (str.pop.to_i + 1).to_s
    str = str.join('-')
  end
end

#generate_slugObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/slugable_taylor.rb', line 9

def generate_slug
  string = self.send(self.class.slug_column.to_sym)
  str = string.strip.gsub(/\W+/, '-').downcase
  other_slugs = self.class.all.select { |obj| obj != self }.map { |obj| obj.slug }
  if other_slugs.include?(str)
    str += str.chars.last == '-' ? "2" : "-2" 
    str = confirm_slug_uniqueness(str, other_slugs)
  end
  self.slug = str
end

#to_paramObject



29
30
31
# File 'lib/slugable_taylor.rb', line 29

def to_param
  self.slug
end