paginate_alphabetically 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.textile +40 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/init.rb +3 -0
- data/install.rb +1 -0
- data/lib/paginate_alphabetically.rb +22 -0
- data/test/db/schema.rb +5 -0
- data/test/helper.rb +14 -0
- data/test/test_paginate_alphabetically.rb +45 -0
- data/uninstall.rb +1 -0
- metadata +104 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Eden Development
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
h2. PaginateAlphabetically
|
2
|
+
|
3
|
+
An easy way to paginate a list of ActiveRecord objects alphabetically by any given attribute.
|
4
|
+
|
5
|
+
|
6
|
+
h2. Example
|
7
|
+
|
8
|
+
h3. Model
|
9
|
+
|
10
|
+
bc. class User < ActiveRecord::Base
|
11
|
+
paginate_alphabetically :by => :surname
|
12
|
+
end
|
13
|
+
|
14
|
+
h3. Controller
|
15
|
+
|
16
|
+
bc. class UsersController < ApplicationController
|
17
|
+
def index
|
18
|
+
@users = User.alphabetical_group(params[:letter])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
h3. View (haml example)
|
23
|
+
|
24
|
+
bc. %ul.pagination
|
25
|
+
- User.pagination_letters.each do |letter|
|
26
|
+
%li= link_to letter, users_path(:letter => letter)
|
27
|
+
%ul.users
|
28
|
+
- @users.each do |user|
|
29
|
+
%li= user.surname
|
30
|
+
|
31
|
+
|
32
|
+
h2. Testing
|
33
|
+
|
34
|
+
The tests use a sqlite3 in-memory database to be able to run separately from your application.
|
35
|
+
|
36
|
+
bc. rake test
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
Copyright (c) 2010 Eden Development, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "paginate_alphabetically"
|
8
|
+
gem.summary = %Q{An easy way to paginate a list of ActiveRecord objects alphabetically by any given attribute.}
|
9
|
+
gem.description = %Q{Provides a list of available letters, and a way of fetching the records for each letter.}
|
10
|
+
gem.email = "dev+gems@edendevelopment.co.uk"
|
11
|
+
gem.homepage = "http://github.com/edendevelopment/paginate_alphabetically"
|
12
|
+
gem.authors = ["Eden Development"]
|
13
|
+
gem.add_development_dependency "activerecord", ">= 2.3.5"
|
14
|
+
gem.add_development_dependency "sqlite3-ruby", "1.2.5"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "paginate_alphabetically #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module PaginateAlphabetically
|
2
|
+
def paginate_alphabetically(params)
|
3
|
+
@attribute = params[:by]
|
4
|
+
self.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def pagination_letters
|
9
|
+
all.sort_by{|obj| obj.send(@attribute)}.group_by {|group| group.send(@attribute)[0].chr.upcase}.keys
|
10
|
+
end
|
11
|
+
|
12
|
+
def first_letter
|
13
|
+
first_instance = find(:first, :order => @attribute)
|
14
|
+
return 'A' if first_instance.nil?
|
15
|
+
first_instance.send(@attribute)[0].chr.upcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def alphabetical_group(letter = nil)
|
19
|
+
find(:all, :conditions => ["#{@attribute.to_s} LIKE ?", "#{letter || first_letter}%"], :order => @attribute)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/db/schema.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/test_case'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'sqlite3'
|
6
|
+
require 'active_record'
|
7
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../init.rb'))
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(
|
10
|
+
:adapter => "sqlite3",
|
11
|
+
:database => ":memory:"
|
12
|
+
)
|
13
|
+
|
14
|
+
load File.expand_path(File.join(File.dirname(__FILE__), 'db/schema.rb'))
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
+
|
3
|
+
class Thing < ActiveRecord::Base
|
4
|
+
paginate_alphabetically :by => :name
|
5
|
+
end
|
6
|
+
|
7
|
+
class Numpty < ActiveRecord::Base
|
8
|
+
end
|
9
|
+
|
10
|
+
class PaginateAlphabeticallyTest < ActiveSupport::TestCase
|
11
|
+
def setup
|
12
|
+
%w(One Two Three Four Five Six).map {|name| Thing.create!(:name => name)}
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_pagination_letters
|
16
|
+
assert_equal ['F', 'O', 'S', 'T'], Thing.pagination_letters
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_first_letter
|
20
|
+
assert_equal 'F', Thing.first_letter
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_first_letter_when_there_are_no_things
|
24
|
+
Thing.destroy_all
|
25
|
+
assert_equal 'A', Thing.first_letter
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_alphabetical_group_without_specifying_letter
|
29
|
+
assert_equal ['Five', 'Four'], Thing.alphabetical_group.map(&:name)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_alphabetical_group_specifying_letter
|
33
|
+
assert_equal ['Three', 'Two'], Thing.alphabetical_group('t').map(&:name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_class_without_pagination_has_no_pagination_methods
|
37
|
+
assert_raise NoMethodError do
|
38
|
+
Numpty.alphabetical_group
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def teardown
|
43
|
+
Thing.destroy_all
|
44
|
+
end
|
45
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paginate_alphabetically
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Eden Development
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-25 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 3
|
30
|
+
- 5
|
31
|
+
version: 2.3.5
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: sqlite3-ruby
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 2
|
44
|
+
- 5
|
45
|
+
version: 1.2.5
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Provides a list of available letters, and a way of fetching the records for each letter.
|
49
|
+
email: dev+gems@edendevelopment.co.uk
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.textile
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.textile
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- init.rb
|
65
|
+
- install.rb
|
66
|
+
- lib/paginate_alphabetically.rb
|
67
|
+
- test/db/schema.rb
|
68
|
+
- test/helper.rb
|
69
|
+
- test/test_paginate_alphabetically.rb
|
70
|
+
- uninstall.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/edendevelopment/paginate_alphabetically
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.3.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: An easy way to paginate a list of ActiveRecord objects alphabetically by any given attribute.
|
101
|
+
test_files:
|
102
|
+
- test/db/schema.rb
|
103
|
+
- test/helper.rb
|
104
|
+
- test/test_paginate_alphabetically.rb
|