pagination_scope 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +40 -0
- data/README +46 -0
- data/Rakefile +149 -0
- data/lib/pagination_scope.rb +183 -0
- data/rails/init.rb +5 -0
- data/spec/fixtures/groups.csv +3 -0
- data/spec/fixtures/users.csv +6 -0
- data/spec/migrations/1_create_users.rb +12 -0
- data/spec/migrations/2_create_groups.rb +11 -0
- data/spec/models/group.rb +4 -0
- data/spec/models/user.rb +4 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/unit/conditional_spec.rb +57 -0
- data/spec/unit/helper_spec.rb +46 -0
- data/spec/unit/include_spec.rb +23 -0
- data/spec/unit/paginate_spec.rb +62 -0
- metadata +96 -0
data/ChangeLog
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
== 0.0.7 / 2008-09-28
|
2
|
+
|
3
|
+
* Changed spec of count method. Now it works without :group option.
|
4
|
+
If you want the old behaviour, you can use count_ids instead.
|
5
|
+
|
6
|
+
== 0.0.6 / 2008-09-26
|
7
|
+
|
8
|
+
* Applied the patch from maiha. Thanks!
|
9
|
+
|
10
|
+
<モデル>
|
11
|
+
* AR.paginate の第三引数に options を渡せるように変更
|
12
|
+
* AR.paginate の options は第一引数でも可能にした
|
13
|
+
AR.paginate(params[:page], 10, options)
|
14
|
+
以外にも以下のように書ける
|
15
|
+
|
16
|
+
options = {
|
17
|
+
:page => params[:page],
|
18
|
+
:per_page => 10,
|
19
|
+
:order => "id",
|
20
|
+
}
|
21
|
+
AR.paginate(options)
|
22
|
+
|
23
|
+
<ヘルパ>
|
24
|
+
* Helper#paginate のオプション名を変更 (newer->prev, older->next)
|
25
|
+
昇順、降順によって newer/older が逆の意味になってわかり辛かったので
|
26
|
+
* Helper#paginate の各要素にクラス名を付与
|
27
|
+
will_paginate のスタイルシートがそのまま使えるように合わせた
|
28
|
+
* Helper#paginate のリンクに rel 属性を追加
|
29
|
+
AutoPagerize 対応
|
30
|
+
* Helper#paginate のオプションに truncate を追加
|
31
|
+
省略時の '...' を設定可能にした
|
32
|
+
|
33
|
+
== 0.0.5 / 2008-09-24
|
34
|
+
|
35
|
+
* Caches count.
|
36
|
+
|
37
|
+
== 0.0.1 / 2008-08-14
|
38
|
+
|
39
|
+
* initial release
|
40
|
+
|
data/README
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= pagination_scope
|
2
|
+
|
3
|
+
This is a rails plugin module for mixing in pagination function.
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
This library is suitable to use together with complicated named_scope which has
|
8
|
+
:joins options. You can use the WillPaginate for ordinary case of pagination.
|
9
|
+
|
10
|
+
== Installation
|
11
|
+
|
12
|
+
=== Archive Installation
|
13
|
+
|
14
|
+
rake install
|
15
|
+
|
16
|
+
=== Gem Installation
|
17
|
+
|
18
|
+
gem install pagination_scope
|
19
|
+
|
20
|
+
|
21
|
+
== Features/Problems
|
22
|
+
|
23
|
+
|
24
|
+
== Synopsis
|
25
|
+
|
26
|
+
In your model class,
|
27
|
+
|
28
|
+
class Post
|
29
|
+
include PaginationScope
|
30
|
+
|
31
|
+
In your controller class,
|
32
|
+
|
33
|
+
class PostsController < ApplicationController
|
34
|
+
def index
|
35
|
+
@posts = Post.not_deleted.paginate(params[:page], 10)
|
36
|
+
|
37
|
+
In your view html.erb,
|
38
|
+
|
39
|
+
<%= paginate @posts %>
|
40
|
+
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Author:: Genki Takiuchi <genki@s21g.com>
|
45
|
+
Copyright:: Copyright (c) 2008 Genki Takiuchi
|
46
|
+
License::
|
data/Rakefile
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'rake/contrib/sshpublisher'
|
10
|
+
require 'fileutils'
|
11
|
+
require 'lib/pagination_scope'
|
12
|
+
include FileUtils
|
13
|
+
|
14
|
+
NAME = "pagination_scope"
|
15
|
+
AUTHOR = "Genki Takiuchi"
|
16
|
+
EMAIL = "genki@s21g.com"
|
17
|
+
DESCRIPTION = ""
|
18
|
+
RUBYFORGE_PROJECT = "asakusarb"
|
19
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
|
+
BIN_FILES = %w( )
|
21
|
+
|
22
|
+
VERS = PaginationScope::VERSION
|
23
|
+
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
24
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
25
|
+
RDOC_OPTS = [
|
26
|
+
'--title', "#{NAME} documentation",
|
27
|
+
"--charset", "utf-8",
|
28
|
+
"--opname", "index.html",
|
29
|
+
"--line-numbers",
|
30
|
+
"--main", "README",
|
31
|
+
"--inline-source",
|
32
|
+
]
|
33
|
+
|
34
|
+
task :default => [:spec]
|
35
|
+
task :package => [:clean]
|
36
|
+
|
37
|
+
Rake::TestTask.new("test") do |t|
|
38
|
+
t.libs << "spec"
|
39
|
+
t.pattern = "spec/**/*_spec.rb"
|
40
|
+
t.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
spec = Gem::Specification.new do |s|
|
44
|
+
s.name = NAME
|
45
|
+
s.version = VERS
|
46
|
+
s.platform = Gem::Platform::RUBY
|
47
|
+
s.has_rdoc = true
|
48
|
+
s.extra_rdoc_files = ["README", "ChangeLog"]
|
49
|
+
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
|
50
|
+
s.summary = DESCRIPTION
|
51
|
+
s.description = DESCRIPTION
|
52
|
+
s.author = AUTHOR
|
53
|
+
s.email = EMAIL
|
54
|
+
s.homepage = HOMEPATH
|
55
|
+
s.executables = BIN_FILES
|
56
|
+
s.rubyforge_project = RUBYFORGE_PROJECT
|
57
|
+
s.bindir = "bin"
|
58
|
+
s.require_path = "lib"
|
59
|
+
#s.autorequire = ""
|
60
|
+
s.test_files = Dir["spec/unit/*_spec.rb"]
|
61
|
+
|
62
|
+
s.add_dependency('activesupport', '>=1.3.1')
|
63
|
+
#s.required_ruby_version = '>= 1.8.2'
|
64
|
+
|
65
|
+
s.files = %w(README ChangeLog Rakefile) +
|
66
|
+
Dir.glob("{bin,doc,spec,lib,templates,generator,extras,website,script}/**/*") +
|
67
|
+
Dir.glob("ext/**/*.{h,c,rb}") +
|
68
|
+
Dir.glob("examples/**/*.rb") +
|
69
|
+
Dir.glob("tools/*.rb") +
|
70
|
+
Dir.glob("rails/*.rb")
|
71
|
+
|
72
|
+
s.extensions = FileList["ext/**/extconf.rb"].to_a
|
73
|
+
end
|
74
|
+
|
75
|
+
Rake::GemPackageTask.new(spec) do |p|
|
76
|
+
p.need_tar = true
|
77
|
+
p.gem_spec = spec
|
78
|
+
end
|
79
|
+
|
80
|
+
task :install do
|
81
|
+
name = "#{NAME}-#{VERS}.gem"
|
82
|
+
sh %{rake package}
|
83
|
+
sh %{sudo gem install pkg/#{name}}
|
84
|
+
end
|
85
|
+
|
86
|
+
task :uninstall => [:clean] do
|
87
|
+
sh %{sudo gem uninstall #{NAME}}
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
Rake::RDocTask.new do |rdoc|
|
92
|
+
rdoc.rdoc_dir = 'html'
|
93
|
+
rdoc.options += RDOC_OPTS
|
94
|
+
rdoc.template = "resh"
|
95
|
+
#rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
96
|
+
if ENV['DOC_FILES']
|
97
|
+
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
|
98
|
+
else
|
99
|
+
rdoc.rdoc_files.include('README', 'ChangeLog')
|
100
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
101
|
+
rdoc.rdoc_files.include('ext/**/*.c')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "Publish to RubyForge"
|
106
|
+
task :rubyforge => [:rdoc, :package] do
|
107
|
+
require 'rubyforge'
|
108
|
+
Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'takiuchi').upload
|
109
|
+
end
|
110
|
+
|
111
|
+
desc 'Package and upload the release to rubyforge.'
|
112
|
+
task :release => [:clean, :package] do |t|
|
113
|
+
v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
|
114
|
+
abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
|
115
|
+
pkg = "pkg/#{NAME}-#{VERS}"
|
116
|
+
|
117
|
+
require 'rubyforge'
|
118
|
+
rf = RubyForge.new.configure
|
119
|
+
puts "Logging in"
|
120
|
+
rf.login
|
121
|
+
|
122
|
+
c = rf.userconfig
|
123
|
+
# c["release_notes"] = description if description
|
124
|
+
# c["release_changes"] = changes if changes
|
125
|
+
c["preformatted"] = true
|
126
|
+
|
127
|
+
files = [
|
128
|
+
"#{pkg}.tgz",
|
129
|
+
"#{pkg}.gem"
|
130
|
+
].compact
|
131
|
+
|
132
|
+
puts "Releasing #{NAME} v. #{VERS}"
|
133
|
+
rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files
|
134
|
+
end
|
135
|
+
|
136
|
+
desc 'Show information about the gem.'
|
137
|
+
task :debug_gem do
|
138
|
+
puts spec.to_ruby
|
139
|
+
end
|
140
|
+
|
141
|
+
desc 'Update gem spec'
|
142
|
+
task :gemspec do
|
143
|
+
open("#{NAME}.gemspec", 'w').write spec.to_ruby
|
144
|
+
end
|
145
|
+
|
146
|
+
desc 'Run specs'
|
147
|
+
task :spec do
|
148
|
+
sh "spec --color spec"
|
149
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'activesupport'
|
2
|
+
|
3
|
+
module PaginationScope
|
4
|
+
VERSION = '0.0.8'
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def included(base)
|
8
|
+
base.class_eval do
|
9
|
+
named_scope :paginate, (proc do |*args|
|
10
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
11
|
+
page = [(args.shift || options.delete(:page) || 1).to_i - 1, 0].max
|
12
|
+
per_page = args.shift || options.delete(:per_page) || 10
|
13
|
+
options[:extend] = PaginationScope::Extention
|
14
|
+
if per_page < 0
|
15
|
+
per_page = -per_page
|
16
|
+
options[:order] ||= "#{table_name}.id ASC"
|
17
|
+
options[:order].gsub!(/\b(DESC|ASC)\b/i){
|
18
|
+
case $1.upcase
|
19
|
+
when 'DESC'; 'ASC'
|
20
|
+
when 'ASC'; 'DESC'
|
21
|
+
end
|
22
|
+
}
|
23
|
+
end
|
24
|
+
options.merge :offset => per_page*(page), :limit => per_page
|
25
|
+
end)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Extention
|
31
|
+
def size
|
32
|
+
proxy_found.size
|
33
|
+
end
|
34
|
+
|
35
|
+
def count
|
36
|
+
@count ||= with_scope :find => proxy_options.except(:offset, :limit) do
|
37
|
+
proxy_scope.count(:distinct => true, :select => "#{table_name}.#{primary_key}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def num_pages; (count.to_f/proxy_options[:limit]).ceil end
|
42
|
+
def page; proxy_options[:offset]/proxy_options[:limit] + 1 end
|
43
|
+
|
44
|
+
def pages(window, left, right)
|
45
|
+
return [] if num_pages <= 1
|
46
|
+
(1..num_pages).inject([]) do |result, i|
|
47
|
+
i <= left || (num_pages - i) < right || (i-page).abs < window ?
|
48
|
+
result << i : (result.last.nil? ? result : result << nil)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
Pagination = Struct.new(:context, :model, :options, :linker)
|
54
|
+
class Pagination
|
55
|
+
delegate :page, :num_pages, :count, :to=>"model"
|
56
|
+
delegate :link_to, :url_for, :content_tag, :to=>"context"
|
57
|
+
|
58
|
+
def current
|
59
|
+
page
|
60
|
+
end
|
61
|
+
|
62
|
+
def first_item
|
63
|
+
offset + 1
|
64
|
+
end
|
65
|
+
|
66
|
+
def last_item
|
67
|
+
[offset + limit, count].min
|
68
|
+
end
|
69
|
+
|
70
|
+
def first
|
71
|
+
1
|
72
|
+
end
|
73
|
+
|
74
|
+
def last
|
75
|
+
num_pages
|
76
|
+
end
|
77
|
+
|
78
|
+
def first?
|
79
|
+
page == 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def last?
|
83
|
+
page == num_pages
|
84
|
+
end
|
85
|
+
|
86
|
+
def offset
|
87
|
+
model.proxy_options[:offset]
|
88
|
+
end
|
89
|
+
|
90
|
+
def limit
|
91
|
+
model.proxy_options[:limit]
|
92
|
+
end
|
93
|
+
|
94
|
+
def window
|
95
|
+
options[:window] || 5
|
96
|
+
end
|
97
|
+
|
98
|
+
def left
|
99
|
+
options[:left] || 2
|
100
|
+
end
|
101
|
+
|
102
|
+
def right
|
103
|
+
options[:right] || 2
|
104
|
+
end
|
105
|
+
|
106
|
+
def prev_label
|
107
|
+
options[:prev] || '« Prev'
|
108
|
+
end
|
109
|
+
|
110
|
+
def next_label
|
111
|
+
options[:next] || 'Next »'
|
112
|
+
end
|
113
|
+
|
114
|
+
def truncate
|
115
|
+
options[:truncate] || '...'
|
116
|
+
end
|
117
|
+
|
118
|
+
def pages
|
119
|
+
@pages ||= model.pages(window, left, right)
|
120
|
+
end
|
121
|
+
|
122
|
+
def span
|
123
|
+
proc do |*args|
|
124
|
+
content_tag(:span, args[0].to_s, :class => (args[1]||"disabled"))
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def prev_link
|
129
|
+
if page > 1
|
130
|
+
link_to prev_label, url_for(:page => page - 1), :class => :prev, :rel => "prev"
|
131
|
+
else
|
132
|
+
span.call(prev_label)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def next_link
|
137
|
+
if page < num_pages
|
138
|
+
link_to next_label, url_for(:page => page + 1), :class => :older, :rel => "next"
|
139
|
+
else
|
140
|
+
span.call(next_label)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def page_link
|
145
|
+
pages.map{|i| page_link_for(i)}
|
146
|
+
end
|
147
|
+
|
148
|
+
def link(i, *args)
|
149
|
+
if linker
|
150
|
+
linker.call(i, *args)
|
151
|
+
else
|
152
|
+
link_to i, url_for(:page => i)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def page_link_for(i)
|
157
|
+
if i.nil?
|
158
|
+
span.call(truncate, options[:style])
|
159
|
+
elsif i == page
|
160
|
+
span.call(i, "#{options[:style]} current")
|
161
|
+
else
|
162
|
+
span.call(link(i), options[:style])
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_s(*args)
|
167
|
+
args = [:prev, :page, :next] if args.blank?
|
168
|
+
items = args.map{|i| respond_to?("#{i}_link") ? send("#{i}_link") : []}.flatten
|
169
|
+
content_tag(:div, items.join("\n"), :class => "pagination")
|
170
|
+
end
|
171
|
+
|
172
|
+
def inspect
|
173
|
+
keys = %w( page count offset limit first last first_item last_item )
|
174
|
+
"#<Pagination %s>" % keys.map{|key| "#{key}: #{send(key)}"}.join(', ')
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
module ::ApplicationHelper
|
179
|
+
def paginate(model, options = nil, &block)
|
180
|
+
Pagination.new(self, model, options||{}, block)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
data/rails/init.rb
ADDED
data/spec/models/user.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'activerecord'
|
4
|
+
require 'active_record/fixtures'
|
5
|
+
|
6
|
+
gem 'rspec', '~>1.1.11'
|
7
|
+
require 'spec'
|
8
|
+
|
9
|
+
SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
|
10
|
+
require SPEC_ROOT.parent + 'lib/pagination_scope'
|
11
|
+
|
12
|
+
# setup mock adapters
|
13
|
+
def make_connection(clazz, db_file)
|
14
|
+
FileUtils.rm(db_file) if File.exist?(db_file)
|
15
|
+
ActiveRecord::Base.configurations = { clazz.name => { :adapter => 'sqlite3', :database => db_file, :timeout => 5000 } }
|
16
|
+
unless File.exist?(db_file)
|
17
|
+
puts "Building SQLite3 database at #{db_file}."
|
18
|
+
sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"}
|
19
|
+
puts "Executing '#{sqlite_command}'"
|
20
|
+
raise SqliteError.new("Seems that there is no sqlite3 executable available") unless system(sqlite_command)
|
21
|
+
end
|
22
|
+
clazz.establish_connection(clazz.name)
|
23
|
+
end
|
24
|
+
|
25
|
+
sqlite_test_db = "#{SPEC_ROOT}/../test.sqlite3"
|
26
|
+
make_connection(ActiveRecord::Base, sqlite_test_db)
|
27
|
+
|
28
|
+
# ----------------------------------------------------------------------
|
29
|
+
# --- Migration ---
|
30
|
+
# ----------------------------------------------------------------------
|
31
|
+
|
32
|
+
Dir.glob(SPEC_ROOT + 'migrations' + '*.rb').sort.each do |migration|
|
33
|
+
require migration
|
34
|
+
end
|
35
|
+
|
36
|
+
# TODO: should resolv class names automatically
|
37
|
+
CreateUsers.up
|
38
|
+
CreateGroups.up
|
39
|
+
|
40
|
+
Dir.glob(SPEC_ROOT + 'models' + '*.rb').each do |model|
|
41
|
+
require model
|
42
|
+
end
|
43
|
+
|
44
|
+
Dir.glob(SPEC_ROOT + 'fixtures' + '*.{yml,csv}').each do |fixture_file|
|
45
|
+
Fixtures.create_fixtures('fixtures', File.basename(fixture_file, '.*'))
|
46
|
+
end
|
47
|
+
|
48
|
+
Spec::Runner.configure do |config|
|
49
|
+
config.before(:each) do
|
50
|
+
# Item.delete_all
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe PaginationScope do
|
4
|
+
before(:all) do
|
5
|
+
User.destroy_all
|
6
|
+
require 'fastercsv'
|
7
|
+
users_csv = FasterCSV.open(
|
8
|
+
File.join(File.dirname(__FILE__), %w(../fixtures/users.csv))).to_a
|
9
|
+
cols, values = users_csv[0], users_csv[1..-1]
|
10
|
+
values.each do |vals|
|
11
|
+
hash = Hash[cols.zip(vals)]
|
12
|
+
hash.delete("id")
|
13
|
+
User.create!(hash.except("id"))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
@conds = ["id < 4"]
|
19
|
+
@start = 1
|
20
|
+
@limit = 2
|
21
|
+
@page = User.paginate(@start, @limit, :conditions=>@conds)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".paginate" do
|
25
|
+
describe "#size" do
|
26
|
+
it "should be tested on valid fixtures" do
|
27
|
+
User.count.should > 0
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be equal to per_page" do
|
31
|
+
# TODO: ensure count >= limit
|
32
|
+
@page.size.should == @limit
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#count" do
|
37
|
+
it "should be equal to top level count" do
|
38
|
+
count = User.count(:conditions=>@conds)
|
39
|
+
@page.count.should == count
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should ignore :offset option" do
|
43
|
+
count = User.count(:conditions=>@conds)
|
44
|
+
@page = User.paginate(2, @limit, :conditions=>@conds) # :offset 2*@limit
|
45
|
+
@page.count.should == count
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should ignore :limit option" do
|
49
|
+
count = User.count(:conditions=>@conds)
|
50
|
+
@page = User.paginate(1, 0, :conditions=>@conds) # :limit 0
|
51
|
+
@page.count.should == count
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe PaginationScope::Pagination do
|
4
|
+
before(:each) do
|
5
|
+
@conds = ["id < 4"]
|
6
|
+
@limit = 3
|
7
|
+
|
8
|
+
# static values
|
9
|
+
@count = User.count
|
10
|
+
@max_pages = (@count.to_f / @limit).ceil
|
11
|
+
end
|
12
|
+
|
13
|
+
def page_at(page_no)
|
14
|
+
pagenate = User.paginate(page_no, @limit, :conditions=>@conds)
|
15
|
+
context = :dummy_for_view
|
16
|
+
PaginationScope::Pagination.new(context, pagenate)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "provide #current"
|
20
|
+
it "provide #first_item"
|
21
|
+
it "provide #last_item"
|
22
|
+
it "provide #first"
|
23
|
+
it "provide #last"
|
24
|
+
it "provide #first?"
|
25
|
+
it "provide #last?"
|
26
|
+
it "provide #offset"
|
27
|
+
it "provide #limit"
|
28
|
+
it "provide #window"
|
29
|
+
it "provide #left"
|
30
|
+
it "provide #right"
|
31
|
+
it "provide #prev_label"
|
32
|
+
it "provide #next_label"
|
33
|
+
it "provide #truncate"
|
34
|
+
it "provide #pages"
|
35
|
+
it "provide #span"
|
36
|
+
it "provide #prev_link"
|
37
|
+
it "provide #next_link"
|
38
|
+
it "provide #page_link"
|
39
|
+
it "provide #link(i, *args)"
|
40
|
+
it "provide #page_link_for(i)"
|
41
|
+
it "provide #to_s(*args)"
|
42
|
+
it "provide #inspect"
|
43
|
+
it "provide #paginate(model, options = nil, &block)"
|
44
|
+
end
|
45
|
+
|
46
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe PaginationScope do
|
4
|
+
before(:each) do
|
5
|
+
@start = 1
|
6
|
+
@limit = 2
|
7
|
+
@page1 = Group.paginate(@start, @limit)
|
8
|
+
@page2 = Group.paginate(@start, @limit, :include=>:users)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ".paginate(:include)" do
|
12
|
+
describe " should be equal to non-included one" do
|
13
|
+
it "for #count" do
|
14
|
+
@page2.count.should == @page1.count
|
15
|
+
end
|
16
|
+
it "for #size" do
|
17
|
+
@page2.size.should == @page1.size
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe PaginationScope do
|
4
|
+
it 'should grant paginate class method to model' do
|
5
|
+
User.should respond_to(:paginate)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe ".paginate" do
|
9
|
+
it "should accept at most three args" do
|
10
|
+
lambda {
|
11
|
+
User.paginate
|
12
|
+
User.paginate 1
|
13
|
+
User.paginate 1, 10, {}
|
14
|
+
}.should_not raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "should raise ArgumentError" do
|
18
|
+
it "when four args are given"
|
19
|
+
it "when 1st arg is not Integer or Hash"
|
20
|
+
it "when 2nd arg is not Integer or Hash"
|
21
|
+
it "when 3rd arg is not Hash"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return an ActiveRecord::NamedScope::Scope" do
|
25
|
+
User.paginate.class.should == ActiveRecord::NamedScope::Scope
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should provide #count" do
|
29
|
+
User.paginate.should respond_to(:count)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#count" do
|
33
|
+
it "should be equal to top level count" do
|
34
|
+
count = User.count
|
35
|
+
User.paginate.count.should == count
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "provide #num_pages"
|
40
|
+
it "provide #page"
|
41
|
+
it "provide #pages(window, left, right)"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should provide #proxy_options" do
|
45
|
+
User.paginate.class.should == ActiveRecord::NamedScope::Scope
|
46
|
+
pending
|
47
|
+
User.paginate.should be_respond_to(:proxy_options)
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#proxy_options" do
|
51
|
+
it "should return an Hash" do
|
52
|
+
User.paginate.proxy_options.class.should == Hash
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "should set default value" do
|
56
|
+
it "0 to offset"
|
57
|
+
it "10 to limit"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pagination_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Takiuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-21 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.3.1
|
24
|
+
version:
|
25
|
+
description: ""
|
26
|
+
email: genki@s21g.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
- ChangeLog
|
34
|
+
files:
|
35
|
+
- README
|
36
|
+
- ChangeLog
|
37
|
+
- Rakefile
|
38
|
+
- spec/fixtures
|
39
|
+
- spec/fixtures/groups.csv
|
40
|
+
- spec/fixtures/users.csv
|
41
|
+
- spec/migrations
|
42
|
+
- spec/migrations/1_create_users.rb
|
43
|
+
- spec/migrations/2_create_groups.rb
|
44
|
+
- spec/models
|
45
|
+
- spec/models/group.rb
|
46
|
+
- spec/models/user.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- spec/unit
|
49
|
+
- spec/unit/conditional_spec.rb
|
50
|
+
- spec/unit/helper_spec.rb
|
51
|
+
- spec/unit/include_spec.rb
|
52
|
+
- spec/unit/paginate_spec.rb
|
53
|
+
- lib/pagination_scope.rb
|
54
|
+
- rails/init.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://asakusarb.rubyforge.org
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --title
|
60
|
+
- pagination_scope documentation
|
61
|
+
- --charset
|
62
|
+
- utf-8
|
63
|
+
- --opname
|
64
|
+
- index.html
|
65
|
+
- --line-numbers
|
66
|
+
- --main
|
67
|
+
- README
|
68
|
+
- --inline-source
|
69
|
+
- --exclude
|
70
|
+
- ^(examples|extras)/
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: asakusarb
|
88
|
+
rubygems_version: 1.3.1
|
89
|
+
signing_key:
|
90
|
+
specification_version: 2
|
91
|
+
summary: ""
|
92
|
+
test_files:
|
93
|
+
- spec/unit/conditional_spec.rb
|
94
|
+
- spec/unit/helper_spec.rb
|
95
|
+
- spec/unit/include_spec.rb
|
96
|
+
- spec/unit/paginate_spec.rb
|