Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#append_suffix(slug) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/sluggable_riggs.rb', line 13

def append_suffix(slug)
  if slug.include?('_')
    slug = slug.succ
  else
    slug += '_1'
  end
end

#render_unique_slug!Object



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

def render_unique_slug!
  new_slug = to_slug(self.send(self.class.slugged_column.to_sym))
  duplicate = self.class.find_by slug: new_slug
  while duplicate && duplicate != self
    new_slug = append_suffix(new_slug)
    duplicate = self.class.find_by slug: new_slug
  end
  self.slug = new_slug
end

#to_paramObject



31
32
33
# File 'lib/sluggable_riggs.rb', line 31

def to_param
  self.slug
end

#to_slug(string) ⇒ Object



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

def to_slug(string)
  string.downcase.gsub(/[\W_]/, '-').gsub(/-+/, '-')
end