jake3030-sunspot_with_kaminari 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/lib/sunspot_with_kaminari.rb +79 -0
- data/lib/sunspot_with_kaminari/version.rb +3 -0
- data/sunspot_with_kaminari.gemspec +24 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
## sunspot_with_kaminari
|
2
|
+
|
3
|
+
sunspot_with_kaminari extends sunspot to have the required methods for pagination with kaminari.
|
4
|
+
|
5
|
+
jake3030 version* Adding support for kaminari paginatable array in results collection.
|
6
|
+
|
7
|
+
### Installation & Usage
|
8
|
+
|
9
|
+
on `Gemfile`
|
10
|
+
|
11
|
+
gem 'kaminari'
|
12
|
+
gem "sunspot_rails"
|
13
|
+
gem "sunspot_with_kaminari", '~> 0.1'
|
14
|
+
|
15
|
+
on the controller
|
16
|
+
|
17
|
+
@products ||= Product.search do
|
18
|
+
keywords params[:search]
|
19
|
+
paginate :page => params[:page], :per_page => 20
|
20
|
+
end
|
21
|
+
|
22
|
+
on the view you want to do something like this:
|
23
|
+
|
24
|
+
<ul>
|
25
|
+
<% @products.results.each do |product| %>
|
26
|
+
<li><%= product.name %></li>
|
27
|
+
<% end %>
|
28
|
+
</ul>
|
29
|
+
|
30
|
+
<%= paginate @products, :window => 1 %>
|
data/Rakefile
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# module SunspotWithKaminari
|
2
|
+
# end
|
3
|
+
|
4
|
+
module Sunspot
|
5
|
+
module Search
|
6
|
+
class AbstractSearch
|
7
|
+
|
8
|
+
# ==== Returns
|
9
|
+
#
|
10
|
+
# Integer:: Current page number
|
11
|
+
#
|
12
|
+
def current_page
|
13
|
+
@query.page
|
14
|
+
end
|
15
|
+
|
16
|
+
# ==== Returns
|
17
|
+
#
|
18
|
+
# Integer:: Total number of pages for matching documents
|
19
|
+
#
|
20
|
+
def num_pages
|
21
|
+
(total.to_f / @query.per_page).ceil
|
22
|
+
end
|
23
|
+
|
24
|
+
# ==== Returns
|
25
|
+
#
|
26
|
+
# Integer:: Number of records displayed per page
|
27
|
+
#
|
28
|
+
def limit_value
|
29
|
+
@query.per_page
|
30
|
+
end
|
31
|
+
|
32
|
+
def empty?
|
33
|
+
total == 0
|
34
|
+
end
|
35
|
+
|
36
|
+
def any?
|
37
|
+
total > 0
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def maybe_will_paginate(collection)
|
44
|
+
if defined?(WillPaginate::Collection)
|
45
|
+
WillPaginate::Collection.create(@query.page, @query.per_page, total) do |pager|
|
46
|
+
pager.replace(collection)
|
47
|
+
end
|
48
|
+
elsif defined?(Kaminari::PaginatableArray)
|
49
|
+
PaginatableArray.initialize_paginated_array(collection, @query.per_page, @query.page, total)
|
50
|
+
else
|
51
|
+
collection
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module Kaminari
|
59
|
+
# Kind of Array that can paginate
|
60
|
+
class PaginatableArray < Array
|
61
|
+
|
62
|
+
def set_total(total)
|
63
|
+
@_total = total
|
64
|
+
end
|
65
|
+
|
66
|
+
# total item numbers of the original array
|
67
|
+
def total_count
|
68
|
+
@_total || @_original_array.count
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Wrap an Array object to make it paginatable
|
73
|
+
def self.initialize_paginated_array(arr, limit_val = default_per_page, offset_val = 0, total = nil) #:nodoc:
|
74
|
+
arr = PaginatableArray.new(arr, limit_val, offset_val)
|
75
|
+
arr.set_total(total)
|
76
|
+
arr
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sunspot_with_kaminari/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "jake3030-sunspot_with_kaminari"
|
7
|
+
s.version = SunspotWithKaminari::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jake Varghese"]
|
10
|
+
s.email = ["jake@flvorful.com"]
|
11
|
+
s.homepage = "https://github.com/jake3030/sunspot_with_kaminari"
|
12
|
+
s.summary = %q{Pagination with kaminari for sunspot with support for paginatablearray}
|
13
|
+
s.description = %q{Extends sunspot to be compatible with kaminari for pagination}
|
14
|
+
|
15
|
+
s.rubyforge_project = "sunspot_with_kaminari"
|
16
|
+
|
17
|
+
s.add_dependency 'sunspot_rails'
|
18
|
+
s.add_dependency 'kaminari'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jake3030-sunspot_with_kaminari
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jake Varghese
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-10 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sunspot_rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: kaminari
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Extends sunspot to be compatible with kaminari for pagination
|
50
|
+
email:
|
51
|
+
- jake@flvorful.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- lib/sunspot_with_kaminari.rb
|
64
|
+
- lib/sunspot_with_kaminari/version.rb
|
65
|
+
- sunspot_with_kaminari.gemspec
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: https://github.com/jake3030/sunspot_with_kaminari
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: sunspot_with_kaminari
|
96
|
+
rubygems_version: 1.3.7
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Pagination with kaminari for sunspot with support for paginatablearray
|
100
|
+
test_files: []
|
101
|
+
|