Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_digit_to_end_of_slugObject



41
42
43
44
45
46
47
# File 'lib/sluggable_rickpeyton.rb', line 41

def add_digit_to_end_of_slug
  counter = 2
  while self.class.exists?(slug: "#{self.slug}-#{counter}")
    counter += 1
  end
  self.slug = self.slug + "-#{counter}"
end

#check_for_slug_uniquenessObject



33
34
35
36
37
38
39
# File 'lib/sluggable_rickpeyton.rb', line 33

def check_for_slug_uniqueness
  if self.class.exists?(slug: self.slug)
    self.add_digit_to_end_of_slug
  else
    return self
  end
end

#create_slugObject



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

def create_slug
  self.slug = self.send(self.class.slug_column.to_sym).downcase
  trim_non_url_characters
  trim_repeat_hyphens
  trim_leading_and_trailing_hyphen
  check_for_slug_uniqueness
end

#to_paramObject



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

def to_param
  "#{self.slug}"
end

#trim_leading_and_trailing_hyphenObject



29
30
31
# File 'lib/sluggable_rickpeyton.rb', line 29

def trim_leading_and_trailing_hyphen
  self.slug.gsub!(/(^-)|(-$)/, '')
end

#trim_non_url_charactersObject



21
22
23
# File 'lib/sluggable_rickpeyton.rb', line 21

def trim_non_url_characters
  self.slug.gsub!(/[^a-z0-9\-]/, '-')
end

#trim_repeat_hyphensObject



25
26
27
# File 'lib/sluggable_rickpeyton.rb', line 25

def trim_repeat_hyphens
  self.slug.gsub!(/-{2,}/, '-')
end