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 candidates for availability.

Instance Method Summary collapse

Constructor Details

#initialize(scope, config) ⇒ SlugGenerator

Returns a new instance of SlugGenerator.


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

def initialize(scope, config)
  @scope = scope
  @config = config
end

Instance Method Details

#available?(slug) ⇒ Boolean

Returns:

  • (Boolean)

11
12
13
14
15
16
17
# File 'lib/friendly_id/slug_generator.rb', line 11

def available?(slug)
  if @config.uses?(::FriendlyId::Reserved) && @config.reserved_words.present? && @config.treat_reserved_as_conflict
    return false if @config.reserved_words.include?(slug)
  end

  !@scope.exists_by_friendly_id?(slug)
end

#generate(candidates) ⇒ Object


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

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