Class: FriendlyId::Candidates

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/friendly_id/candidates.rb

Overview

This class provides the slug candidate functionality.

See Also:

Instance Method Summary (collapse)

Constructor Details

- (Candidates) initialize(object, *array)

Returns a new instance of Candidates



11
12
13
14
# File 'lib/friendly_id/candidates.rb', line 11

def initialize(object, *array)
  @object = object
  @candidates = to_candidate_array(object, array.flatten(1))
end

Instance Method Details

- (Object) each(*args, &block)

Visits each slug candidate, calls it, passes it to normalize_friendly_id and yields the result.



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

def each(*args, &block)
  @candidates.each(*args) do |candidate|
    yield @object.normalize_friendly_id(candidate.map(&:call).join(' '))
  end
end

- (Object) to_candidate_array(object, array) (private)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/friendly_id/candidates.rb', line 26

def to_candidate_array(object, array)
  array.map do |candidate|
    case candidate
    when String
      [->{candidate}]
    when Array
      to_candidate_array(object, candidate).flatten
    when Symbol
      [object.method(candidate)]
    else
      if candidate.respond_to?(:call)
        [candidate]
      else
        [->{candidate.to_s}]
      end
    end
  end
end