Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_slugObject



9
10
11
# File 'lib/sluggable_eduardo.rb', line 9

def generate_slug
  self.slug = sluggable_to_slug(self.send(self.class.slug_column.to_sym))
end

#remove_ending_dash(string) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/sluggable_eduardo.rb', line 40

def remove_ending_dash(string)
  if string.last.match(/\w/)
    string
  else
    remove_ending_dash(string[0..-2])
  end
end

#sluggable_to_slug(sluggable) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sluggable_eduardo.rb', line 13

def sluggable_to_slug(sluggable)
  if sluggable.match(/\W/)
    slug = ''

    sluggable.each_char do |char|
      if char.match(/\W/)
        slug += '-'
      else
        slug += char
      end
    end
    slug = remove_ending_dash(slug).downcase
  else
    slug = sluggable.downcase
  end
  
  counter = 1
  possible_slug = slug

  while self.class.find_by_slug(possible_slug)
    possible_slug = slug + "-#{counter}"
    counter += 1
  end

  slug = possible_slug
end

#to_paramObject



48
49
50
# File 'lib/sluggable_eduardo.rb', line 48

def to_param
  slug
end