Class: FriendlyId::Candidates
- Inherits:
-
Object
- Object
- FriendlyId::Candidates
- Includes:
- Enumerable
- Defined in:
- lib/friendly_id/candidates.rb
Overview
This class provides the slug candidate functionality.
Instance Method Summary (collapse)
-
- (Object) each(*args, &block)
Visits each candidate, calls it, passes it to
normalize_friendly_id
and yields any wanted and unreserved slug candidates. -
- (Candidates) initialize(object, *array)
constructor
A new instance of Candidates.
- - (Boolean) reserved?(slug) private
- - (Object) to_candidate_array(object, array) private
- - (Boolean) wanted?(slug) private
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 candidate, calls it, passes it to normalize_friendly_id
and
yields any wanted and unreserved slug candidates.
18 19 20 21 22 23 24 25 26 27 |
# 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 |
- (Boolean) reserved?(slug) (private)
56 57 58 59 60 61 |
# 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 |
- (Object) to_candidate_array(object, array) (private)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 |
- (Boolean) wanted?(slug) (private)
50 51 52 |
# File 'lib/friendly_id/candidates.rb', line 50 def wanted?(slug) slug.present? end |