Class: FriendlyId::SlugGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_id/slug_generator.rb

Overview

The default slug generator offers functionality to check slug strings for uniqueness and, if necessary, appends a sequence to guarantee it.

Instance Method Summary (collapse)

Constructor Details

- (SlugGenerator) initialize(scope)

Returns a new instance of SlugGenerator



6
7
8
# File 'lib/friendly_id/slug_generator.rb', line 6

def initialize(scope)
  @scope = scope
end

Instance Method Details

- (Object) add(slug)



14
15
16
# File 'lib/friendly_id/slug_generator.rb', line 14

def add(slug)
  slug
end

- (Boolean) available?(slug)

Returns:

  • (Boolean)


10
11
12
# File 'lib/friendly_id/slug_generator.rb', line 10

def available?(slug)
  !@scope.exists_by_friendly_id?(slug)
end

- (Object) generate(candidates)



18
19
20
21
# File 'lib/friendly_id/slug_generator.rb', line 18

def generate(candidates)
  candidates.each {|c| return add c if available?(c)}
  nil
end