Browse files

only concerning pagination with the letters available within the coll…

…ection provided, not every entry in database
  • Loading branch information...
1 parent a604bbc commit a7211d27c6db73de9374af96274b2275e60b6032 @zakgrant committed Mar 22, 2013
View
5 lib/paginate_alphabetically/active_record.rb
@@ -8,9 +8,10 @@ def paginate_alphabetically(params)
end
module ClassMethods
- def pagination_letters
+ def pagination_letters(collection=nil)
return ALL_LETTERS if @paginate_alphabetically__show_all_letters
- all.sort_by{|obj| obj.send(@attribute).upcase}.group_by {|group| group.send(@attribute)[0].chr.upcase}.keys
+ collection = collection.nil? ? all : collection
+ collection.sort_by{|obj| obj.send(@attribute).upcase}.group_by {|group| group.send(@attribute)[0].chr.upcase}.keys
end
def first_letter
View
2 lib/paginate_alphabetically/view_helpers.rb
@@ -5,7 +5,7 @@ def alphabetically_paginate(collection, options = {})
available_letters = PaginateAlphabetically::ALL_LETTERS
else
return "" if collection.empty?
- available_letters = collection.first.class.pagination_letters
+ available_letters = collection.first.class.pagination_letters collection
end
content_tag(:ul, safe(alphabetical_links_to(available_letters)),
:class => options[:class] || "pagination")
View
31 spec/paginate_alphabetically_spec.rb
@@ -11,13 +11,34 @@
end
context '#pagination_letters' do
- it 'picks out the correct letters from the set' do
- Thing.pagination_letters.should == ['F', 'O', 'S', 'T']
+ before :each do
+ Thing.paginate_alphabetically :by => :name, :show_all_letters => false
end
- it 'shows all letters always when asked' do
- Thing.paginate_alphabetically :by => :name, :show_all_letters => true
- Thing.pagination_letters.should == ('A'..'Z').to_a
+ context 'collection provided' do
+ before :each do
+ @collection = %w(seven Eight Nine).map {|name| Thing.create!(:name => name)}
+ end
+
+ it 'picks out the correct letters from the set' do
+ Thing.pagination_letters(@collection).should == %w{E N S}
+ end
+
+ it 'shows all letters always when asked' do
+ Thing.paginate_alphabetically :by => :name, :show_all_letters => true
+ Thing.pagination_letters(@collection).should == ('A'..'Z').to_a
+ end
+ end
+
+ context 'no collection provided' do
+ it 'picks out the correct letters from the set' do
+ Thing.pagination_letters.should == ['F', 'O', 'S', 'T']
+ end
+
+ it 'shows all letters always when asked' do
+ Thing.paginate_alphabetically :by => :name, :show_all_letters => true
+ Thing.pagination_letters.should == ('A'..'Z').to_a
+ end
end
end

0 comments on commit a7211d2

Please sign in to comment.