Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#append_suffix(text, count) ⇒ Object



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

def append_suffix(text, count)
  if text.split('-').last.to_i != 0
    return text.split('-').slice(0...-1).join('-') + "-" + count.to_s
  else
    return text + "-" + count.to_s
  end
end

#generate_slug!Object



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

def generate_slug!
  the_slug = to_slug(self.send(self.class.slug_column.to_sym))
  object = self.class.find_by(slug: the_slug)
  count = 2
  if object && object != self
    the_slug = append_suffix(the_slug, count)
    object = self.class.find_by(slug: the_slug)
    count +=1
  end
  self.slug = the_slug.downcase
end

#to_paramObject



36
37
38
# File 'lib/sluggable_cano.rb', line 36

def to_param
  self.slug
end

#to_slug(name) ⇒ Object



29
30
31
32
33
34
# File 'lib/sluggable_cano.rb', line 29

def to_slug(name)
  text = name.strip
  text.gsub!(/\s*[^A-Za-z0-9]\s*/, '-')
  text.gsub!(/-+/,'-')
  text.downcase
end