Module: Slugreate

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#append_suffix(str, count) ⇒ Object



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

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

#generate_slugObject



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

def generate_slug
  self.slug = self.send(self.class.slug_column.to_sym).parameterize
  cl = self.class.find_by(slug: self.slug)
  count = 2
  while cl && cl != self
    self.slug = append_suffix(self.slug, count)
    cl = self.class.find_by(slug: self.slug)
    count += 1
  end
end

#to_paramObject



28
29
30
# File 'lib/slugreate.rb', line 28

def to_param
  self.slug
end