class FriendlyId::Candidates

This class provides the slug candidate functionality. @see FriendlyId::Slugged

Public Class Methods

new(object, *array) click to toggle source
# File lib/friendly_id/candidates.rb, line 11
def initialize(object, *array)
  @object = object
  @candidates = to_candidate_array(object, array.flatten(1))
end

Public Instance Methods

each(*args) { |x| ... } click to toggle source

Visits each candidate, calls it, passes it to `normalize_friendly_id` and yields any wanted and unreserved slug candidates.

# File lib/friendly_id/candidates.rb, line 18
def each(*args, &block)
  pre_candidates = @candidates.map do |candidate|
    @object.normalize_friendly_id(candidate.map(&:call).join(' '))
  end.select {|x| wanted?(x)}

  unless pre_candidates.all? {|x| reserved?(x)}
    pre_candidates.reject! {|x| reserved?(x)}
  end
  pre_candidates.each {|x| yield x}
end

Private Instance Methods

reserved?(slug) click to toggle source
# File lib/friendly_id/candidates.rb, line 56
def reserved?(slug)
  config = @object.friendly_id_config
  return false unless config.uses? ::FriendlyId::Reserved
  return false unless config.reserved_words
  config.reserved_words.include?(slug)
end
to_candidate_array(object, array) click to toggle source
# File lib/friendly_id/candidates.rb, line 31
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
wanted?(slug) click to toggle source
# File lib/friendly_id/candidates.rb, line 50
def wanted?(slug)
  slug.present?
end