paginate_alphabetically 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -21,9 +21,7 @@ end
21
21
 
22
22
  h3. View (haml example)
23
23
 
24
- bc. %ul.pagination
25
- - User.pagination_letters.each do |letter|
26
- %li= link_to letter, users_path(:letter => letter)
24
+ bc. = alphabetically_paginate(@users)
27
25
  %ul.users
28
26
  - @users.each do |user|
29
27
  %li= user.surname
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/init.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'lib/paginate_alphabetically'))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib/view_helpers'))
2
3
 
3
4
  ActiveRecord::Base.extend(PaginateAlphabetically)
5
+ ActionView::Base.class_eval { include PaginateAlphabetically::ViewHelpers }
@@ -10,7 +10,7 @@ module PaginateAlphabetically
10
10
  end
11
11
 
12
12
  def first_letter
13
- first_instance = find(:first, :order => @attribute)
13
+ first_instance = find(:first, :order => @attribute, :conditions => ["#{@attribute.to_s} >= ?", 'a'])
14
14
  return 'A' if first_instance.nil?
15
15
  first_instance.send(@attribute)[0].chr.upcase
16
16
  end
@@ -0,0 +1,24 @@
1
+ module PaginateAlphabetically
2
+ module ViewHelpers
3
+ def alphabetically_paginate(collection)
4
+ return "" if collection.empty?
5
+ available_letters = collection.first.class.pagination_letters
6
+ content_tag(:ul, alphabetical_links_to(available_letters),
7
+ :class => "pagination")
8
+ end
9
+
10
+ def alphabetical_links_to(available_letters)
11
+ ('A'..'Z').map do |letter|
12
+ content_tag(:li, paginated_letter(available_letters, letter))
13
+ end.join(" ")
14
+ end
15
+
16
+ def paginated_letter(available_letters, letter)
17
+ if available_letters.include?(letter)
18
+ link_to(letter, "?letter=#{letter}")
19
+ else
20
+ letter
21
+ end
22
+ end
23
+ end
24
+ end
data/test/helper.rb CHANGED
@@ -4,6 +4,8 @@ require 'active_support/test_case'
4
4
  require 'test/unit'
5
5
  require 'sqlite3'
6
6
  require 'active_record'
7
+ require 'action_view'
8
+ include ActionView::Helpers
7
9
  require File.expand_path(File.join(File.dirname(__FILE__), '../init.rb'))
8
10
 
9
11
  ActiveRecord::Base.establish_connection(
@@ -12,3 +14,10 @@ ActiveRecord::Base.establish_connection(
12
14
  )
13
15
 
14
16
  load File.expand_path(File.join(File.dirname(__FILE__), 'db/schema.rb'))
17
+
18
+ class Thing < ActiveRecord::Base
19
+ paginate_alphabetically :by => :name
20
+ end
21
+
22
+ class Numpty < ActiveRecord::Base
23
+ end
@@ -1,39 +1,40 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
- class Thing < ActiveRecord::Base
4
- paginate_alphabetically :by => :name
5
- end
6
-
7
- class Numpty < ActiveRecord::Base
8
- end
9
-
10
3
  class PaginateAlphabeticallyTest < ActiveSupport::TestCase
11
4
  def setup
12
5
  %w(one two Three four Five Six).map {|name| Thing.create!(:name => name)}
13
6
  end
14
7
 
15
- def test_pagination_letters
8
+ test "pagination letters" do
16
9
  assert_equal ['F', 'O', 'S', 'T'], Thing.pagination_letters
17
10
  end
18
11
 
19
- def test_first_letter
12
+ test "first letter" do
13
+ assert_equal 'F', Thing.first_letter
14
+ end
15
+
16
+ test "first letter is alphabetical" do
17
+ Thing.create!(:name => ' o noes a space :(')
18
+ Thing.create!(:name => '1')
19
+ Thing.create!(:name => '®')
20
+ Thing.create!(:name => '$')
20
21
  assert_equal 'F', Thing.first_letter
21
22
  end
22
23
 
23
- def test_first_letter_when_there_are_no_things
24
+ test "first letter when there are no things" do
24
25
  Thing.destroy_all
25
26
  assert_equal 'A', Thing.first_letter
26
27
  end
27
28
 
28
- def test_alphabetical_group_without_specifying_letter
29
+ test "alphabetical group without specifying letter" do
29
30
  assert_equal ['Five', 'four'], Thing.alphabetical_group.map(&:name)
30
31
  end
31
32
 
32
- def test_alphabetical_group_specifying_letter
33
+ test "alphabetical group specifying letter" do
33
34
  assert_equal ['Three', 'two'], Thing.alphabetical_group('t').map(&:name)
34
35
  end
35
36
 
36
- def test_class_without_pagination_has_no_pagination_methods
37
+ test "class without pagination has no pagination methods" do
37
38
  assert_raise NoMethodError do
38
39
  Numpty.alphabetical_group
39
40
  end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ include PaginateAlphabetically::ViewHelpers
3
+
4
+ class ViewHelperTest < ActiveSupport::TestCase
5
+ def setup
6
+ @result = alphabetically_paginate([Thing.create!(:name => 'a')])
7
+ end
8
+
9
+ test "it includes all the letters" do
10
+ ('A'..'Z').each do |letter|
11
+ assert @result.include?(letter)
12
+ end
13
+ end
14
+
15
+ test "it links to the available letters" do
16
+ assert @result.include?('<a href="?letter=A">')
17
+ end
18
+
19
+ test "it returns nothing when the collection is empty" do
20
+ assert_equal "", alphabetically_paginate([])
21
+ end
22
+
23
+ test "it does not link to letters that have no content" do
24
+ assert_equal false, @result.include?('href="?letter=B"')
25
+ end
26
+
27
+ test "it wraps the letters as list items" do
28
+ assert @result.include?('<li>B</li>')
29
+ end
30
+
31
+ test "it wraps the result in a ul" do
32
+ assert @result.include?('<ul class="pagination">')
33
+ end
34
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paginate_alphabetically
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eden Development
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 +01:00
18
+ date: 2010-09-10 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -69,9 +69,11 @@ files:
69
69
  - init.rb
70
70
  - install.rb
71
71
  - lib/paginate_alphabetically.rb
72
+ - lib/view_helpers.rb
72
73
  - test/db/schema.rb
73
74
  - test/helper.rb
74
75
  - test/test_paginate_alphabetically.rb
76
+ - test/test_view_helpers.rb
75
77
  - uninstall.rb
76
78
  has_rdoc: true
77
79
  homepage: http://github.com/edendevelopment/paginate_alphabetically
@@ -111,3 +113,4 @@ test_files:
111
113
  - test/db/schema.rb
112
114
  - test/helper.rb
113
115
  - test/test_paginate_alphabetically.rb
116
+ - test/test_view_helpers.rb