Module: Sluggable

Defined in:
lib/ckb-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#generate_slug!(attribute) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ckb-sluggable.rb', line 6

def generate_slug!(attribute)
  temp_slug = to_slug(self[attribute])

  counter = 0
  while self.class.find_by(slug: temp_slug)
    counter += 1

    # if conditional makes sure we only remove a number we added
    temp_slug.gsub!(/-\d\z/, "") if counter > 1
    temp_slug = "#{temp_slug}-#{counter}"
  end

  self.slug = temp_slug
end

#to_paramObject



21
22
23
# File 'lib/ckb-sluggable.rb', line 21

def to_param
  slug
end

#to_slug(text) ⇒ Object



2
3
4
# File 'lib/ckb-sluggable.rb', line 2

def to_slug(text)
  text.strip.downcase.gsub(/\s+/, "-").gsub(/[^\w-]/, "")
end