Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added show_all_letters option to skip slow letter finding code

  • Loading branch information...
commit fd52967e2b6038a7885e59fde9d16eba994f228f 1 parent 8b20331
@edendevelopment authored
View
7 README.textile
@@ -33,9 +33,14 @@ The tests use a sqlite3 in-memory database to be able to run separately from you
bc. rake test
-
h2. Options
+The initial model code will take a show_all_letters option, in order to skip the (slow) letter finding code:
+
+bc. class User < ActiveRecord::Base
+ paginate_alphabetically :by => :surname, :show_all_letters => true
+end
+
Thanks to "hoverlover":http://github.com/hoverlover the alphabetically_paginate view helper can take an options hash which allows you to override the class if you don't want it to be 'pagination'. For example:
bc. = alphabetically_paginate(@users, :class => 'user-pagination')
View
3  lib/paginate_alphabetically/active_record.rb
@@ -2,11 +2,14 @@ module PaginateAlphabetically
module ActiveRecord
def paginate_alphabetically(params)
@attribute = params[:by]
+ @paginate_alphabetically__show_all_letters = params[:show_all_letters] || false
self.extend ClassMethods
end
module ClassMethods
+ ALL_LETTERS = ('A'..'Z').to_a
def pagination_letters
+ 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
end
View
11 spec/paginate_alphabetically_spec.rb
@@ -10,8 +10,15 @@
Thing.destroy_all
end
- it '#pagination_letters picks out the correct letters from the set' do
- Thing.pagination_letters.should == ['F', 'O', 'S', 'T']
+ context '#pagination_letters' 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
context "#first_letter" do
View
1  spec/view_helpers_spec.rb
@@ -12,6 +12,7 @@ def request
describe 'View helpers' do
before do
+ Thing.paginate_alphabetically :by => :name, :show_all_letters => false
@result = alphabetically_paginate([Thing.create!(:name => 'a')])
end
Please sign in to comment.
Something went wrong with that request. Please try again.