Module: SluggableAms

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_slugObject

Makes sure that the slug is unique, if it’s not it appends a number to it.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sluggable_ams.rb', line 10

def generate_slug
  slug = sluggify(self.send(self.class.sluggable_column))
  count = 2
  old_slugs = self.class.all.map(&:slug)
  new_slug = slug
  loop do
    if old_slugs.include?(new_slug)
      new_slug = "#{slug}-#{count}"
      count += 1
    else
      self.slug = new_slug
      break
    end
  end
end

#sluggify(str) ⇒ Object



26
27
28
# File 'lib/sluggable_ams.rb', line 26

def sluggify(str)
  str.gsub(' ', '-').gsub(/[^0-9a-z\-]/i, '').downcase
end

#to_paramObject



30
31
32
# File 'lib/sluggable_ams.rb', line 30

def to_param
  slug
end