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
- #candidates ⇒ Object private
- #each(*args, &block) ⇒ Object
- #filter(candidates) ⇒ Object private
-
#initialize(object, *array) ⇒ Candidates
constructor
A new instance of Candidates.
- #normalize(candidates) ⇒ Object private
- #reserved?(slug) ⇒ Boolean private
- #to_candidate_array(object, array) ⇒ Object private
- #wanted?(slug) ⇒ Boolean private
Constructor Details
#initialize(object, *array) ⇒ Candidates
Returns a new instance of Candidates.
9 10 11 12 |
# File 'lib/friendly_id/candidates.rb', line 9 def initialize(object, *array) @object = object @raw_candidates = to_candidate_array(object, array.flatten(1)) end |
Instance Method Details
#candidates ⇒ Object (private)
21 22 23 24 25 26 |
# File 'lib/friendly_id/candidates.rb', line 21 def candidates @candidates ||= begin candidates = normalize(@raw_candidates) filter(candidates) end end |
#each(*args, &block) ⇒ Object
14 15 16 17 |
# File 'lib/friendly_id/candidates.rb', line 14 def each(*args, &block) return candidates unless block candidates.each { |candidate| yield candidate } end |
#filter(candidates) ⇒ Object (private)
34 35 36 37 38 39 |
# File 'lib/friendly_id/candidates.rb', line 34 def filter(candidates) unless candidates.all? { |x| reserved?(x) } candidates.reject! { |x| reserved?(x) } end candidates end |
#normalize(candidates) ⇒ Object (private)
28 29 30 31 32 |
# File 'lib/friendly_id/candidates.rb', line 28 def normalize(candidates) candidates.map do |candidate| @object.normalize_friendly_id(candidate.map(&:call).join(" ")) end.select { |x| wanted?(x) } end |
#reserved?(slug) ⇒ Boolean (private)
64 65 66 67 68 69 |
# File 'lib/friendly_id/candidates.rb', line 64 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) ⇒ Object (private)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/friendly_id/candidates.rb', line 41 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) ⇒ Boolean (private)
60 61 62 |
# File 'lib/friendly_id/candidates.rb', line 60 def wanted?(slug) slug.present? end |