Skip to content

Commit

Permalink
Merge pull request #651 from ignat-zakrevsky/minor-candidates-each-up…
Browse files Browse the repository at this point in the history
…date

Make Candidates#each iterable without block.
  • Loading branch information
norman committed May 24, 2015
2 parents be25dfb + c9668dd commit 7391ac1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/friendly_id/candidates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def each(*args, &block)
unless pre_candidates.all? {|x| reserved?(x)}
pre_candidates.reject! {|x| reserved?(x)}
end

return pre_candidates unless block_given?

pre_candidates.each {|x| yield x}
end

Expand Down
26 changes: 26 additions & 0 deletions test/candidates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,30 @@ def slug_candidates
end
end

test "allows to iterate through candidates without passing block" do
klass = Class.new model_class do
def slug_candidates
:name
end
end
with_instances_of klass do |_, city|
candidates = FriendlyId::Candidates.new(city, city.slug_candidates)
assert_equal candidates.each, ['new-york']
end
end

test "iterates through candidates with passed block" do
klass = Class.new model_class do
def slug_candidates
:name
end
end
with_instances_of klass do |_, city|
collected_candidates = []
candidates = FriendlyId::Candidates.new(city, city.slug_candidates)
candidates.each { |candidate| collected_candidates << candidate }
assert_equal collected_candidates, ['new-york']
end
end

end

0 comments on commit 7391ac1

Please sign in to comment.