Module: Gluttonberg::Content::SlugManagement::ClassMethods

Defined in:
lib/gluttonberg/content/slug_management.rb

Class Method Summary collapse

Class Method Details

.check_for_duplication(slug, object, potential_duplicates) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gluttonberg/content/slug_management.rb', line 22

def self.check_for_duplication(slug, object, potential_duplicates)
  unless potential_duplicates.blank?
    if potential_duplicates.length > 1 || (potential_duplicates.length == 1 && potential_duplicates.first.id != object.id )
      number = potential_duplicates.length+1
      begin
        slug = "#{self.slug_without_postfix(slug)}-#{number}"
        number += 1
      end while object.find_potential_duplicates(slug).map{|o| o.slug}.include?(slug)
    end
  end
  slug
end

.slug_without_postfix(slug) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gluttonberg/content/slug_management.rb', line 35

def self.slug_without_postfix(slug)
  temp_slug = slug

  unless temp_slug.blank?
    slug_tokens = temp_slug.split("-")
    if slug_tokens.last.to_i.to_s == slug_tokens.last && slug_tokens.length > 1
      slug_tokens = slug_tokens[0..-2]
      temp_slug = slug_tokens.join("-")
    end
  end
  temp_slug
end