sunspot_kaminari_support 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/lib/sunspot_kaminari_support.rb +80 -0
- data/lib/sunspot_kaminari_support/version.rb +3 -0
- data/spec/lib/sunspot_kaminari_support_spec.rb +75 -0
- data/spec/spec_helper.rb +28 -0
- data/sunspot_kaminari_support.gemspec +25 -0
- metadata +140 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrey Rozhkovsky
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# SunspotKaminariSupport
|
2
|
+
|
3
|
+
Add methods required for Kaminari helper methods (ex:
|
4
|
+
helper.paginate(result))
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'sunspot_kaminari_support'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install sunspot_kaminari_support
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Gem includes module with helper method into Sunspot::Search::AbstractSearch.
|
23
|
+
So now you are able to use solr results in kaminari helpers:
|
24
|
+
|
25
|
+
@result = Post.solr_search{}
|
26
|
+
paginate(@result) # This would output several pagination links such as « First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »
|
27
|
+
page_entries_info(@result) # This renders a helpful message with numbers of displayed vs. total entries.
|
28
|
+
|
29
|
+
See [kaminari](https://github.com/amatsuda/kaminari) page for detailed explanation of these helpers usage
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "sunspot_kaminari_support/version"
|
2
|
+
|
3
|
+
module SunspotKaminariSupport
|
4
|
+
# Returns:
|
5
|
+
# => Current page number
|
6
|
+
#
|
7
|
+
def current_page
|
8
|
+
query.page
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns:
|
12
|
+
# => Total number of pages
|
13
|
+
#
|
14
|
+
def total_pages
|
15
|
+
(total.to_f / query.per_page).ceil
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns:
|
19
|
+
# => Number of records displayed per page
|
20
|
+
#
|
21
|
+
def limit_value
|
22
|
+
query.per_page || Kaminari.config.default_per_page
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# Returns:
|
27
|
+
# => true if result is blank
|
28
|
+
#
|
29
|
+
def empty?
|
30
|
+
total == 0
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns:
|
34
|
+
# => true if there are any results
|
35
|
+
def any?
|
36
|
+
total > 0
|
37
|
+
end
|
38
|
+
|
39
|
+
# Changes page query param
|
40
|
+
def page(new_page)
|
41
|
+
query.paginate(new_page, nil)
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns:
|
46
|
+
# => true if current_page is last or bigger
|
47
|
+
def last_page?
|
48
|
+
current_page >= total_pages
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns:
|
52
|
+
# => true if current_page equals 1
|
53
|
+
def first_page?
|
54
|
+
current_page == 1
|
55
|
+
end
|
56
|
+
|
57
|
+
# Methods for page_entries_info helper
|
58
|
+
#
|
59
|
+
|
60
|
+
# Returns:
|
61
|
+
# => Total number of records before current page
|
62
|
+
#
|
63
|
+
def offset_value
|
64
|
+
query.per_page * (query.page - 1)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Returns:
|
69
|
+
# => Total number of records found
|
70
|
+
#
|
71
|
+
def total_count
|
72
|
+
total
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
if defined?(Sunspot)
|
77
|
+
Sunspot::Search::AbstractSearch.send(:include, SunspotKaminariSupport)
|
78
|
+
else
|
79
|
+
puts "[SunspotKaminariSupport] WARNING: Sunspot not found"
|
80
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SunspotKaminariSupport do
|
4
|
+
subject {SunspotStub.new(:query => query, :total => total)}
|
5
|
+
let(:query) {double('query')}
|
6
|
+
let(:total) {nil}
|
7
|
+
it "#current_page delegates to query" do
|
8
|
+
query.should_receive :page
|
9
|
+
subject.current_page
|
10
|
+
end
|
11
|
+
context "#total_pages" do
|
12
|
+
it "uses query.per_page" do
|
13
|
+
query.should_receive(:per_page).and_return(3)
|
14
|
+
subject.total_pages
|
15
|
+
end
|
16
|
+
it "returns correct number" do
|
17
|
+
query.stub(:per_page => 3)
|
18
|
+
expect(subject.total_pages).to eq(2)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
context '#limit_value' do
|
22
|
+
it "returns per_page" do
|
23
|
+
query.should_receive(:per_page).and_return(3)
|
24
|
+
expect(subject.limit_value).to eq(3)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
context '#empty?' do
|
28
|
+
it "returns true when total eq 0" do
|
29
|
+
subject.stub(:total => 0)
|
30
|
+
expect(subject.empty?).to be_true
|
31
|
+
end
|
32
|
+
it "returns false when total > 0" do
|
33
|
+
subject.stub(:total => 1)
|
34
|
+
expect(subject.empty?).to be_false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context '#any?' do
|
39
|
+
it "returns false when total eq 0" do
|
40
|
+
subject.stub(:total => 0)
|
41
|
+
expect(subject.any?).to be_false
|
42
|
+
end
|
43
|
+
it "returns true when total > 0" do
|
44
|
+
subject.stub(:total => 1)
|
45
|
+
expect(subject.any?).to be_true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context '#last_page?' do
|
50
|
+
it "returns true when current_page is last" do
|
51
|
+
subject.stub(:total_pages => 1)
|
52
|
+
query.stub(:page => 1)
|
53
|
+
expect(subject.last_page?).to be_true
|
54
|
+
end
|
55
|
+
it "returns false when current page isn't last" do
|
56
|
+
subject.stub(:total_pages => 2)
|
57
|
+
query.stub(:page => 1)
|
58
|
+
expect(subject.last_page?).to be_false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '#offset_value' do
|
63
|
+
it "returns numbre of records before current page" do
|
64
|
+
query.stub(:per_page => 3, :page => 2)
|
65
|
+
expect(subject.offset_value).to eq(3)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context '#total_count' do
|
70
|
+
it "returns total" do
|
71
|
+
subject.should_receive(:total).and_return(6)
|
72
|
+
expect(subject.total_count).to eq 6
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require "pry"
|
8
|
+
require File.expand_path('../../lib/sunspot_kaminari_support', __FILE__)
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
|
14
|
+
# Run specs in random order to surface order dependencies. If you find an
|
15
|
+
# order dependency and want to debug it, you can fix the order by providing
|
16
|
+
# the seed, which is printed after each run.
|
17
|
+
# --seed 1234
|
18
|
+
config.order = 'random'
|
19
|
+
end
|
20
|
+
|
21
|
+
class SunspotStub
|
22
|
+
include SunspotKaminariSupport
|
23
|
+
attr_reader :query, :total
|
24
|
+
def initialize(options = {})
|
25
|
+
@query = options[:query]
|
26
|
+
@total = options[:total] || 5
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sunspot_kaminari_support/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sunspot_kaminari_support"
|
8
|
+
gem.version = SunspotKaminariSupport::VERSION
|
9
|
+
gem.authors = ["Andrey Rozhkovsky"]
|
10
|
+
gem.email = ["a.rozhkovsky@kandasoft.com"]
|
11
|
+
gem.description = %q{Adds methods required for kaminari helpers in views to sunspot result}
|
12
|
+
gem.summary = %q{Includes module with methods required for kaminari helpers in views to sunspot result}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "pry"
|
22
|
+
gem.add_runtime_dependency 'sunspot'
|
23
|
+
gem.add_runtime_dependency 'sunspot_rails'
|
24
|
+
gem.add_runtime_dependency 'kaminari', '>= 0.14.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sunspot_kaminari_support
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrey Rozhkovsky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sunspot
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sunspot_rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: kaminari
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.14.0
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.14.0
|
94
|
+
description: Adds methods required for kaminari helpers in views to sunspot result
|
95
|
+
email:
|
96
|
+
- a.rozhkovsky@kandasoft.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- lib/sunspot_kaminari_support.rb
|
109
|
+
- lib/sunspot_kaminari_support/version.rb
|
110
|
+
- spec/lib/sunspot_kaminari_support_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- sunspot_kaminari_support.gemspec
|
113
|
+
homepage: ''
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.25
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: Includes module with methods required for kaminari helpers in views to sunspot
|
137
|
+
result
|
138
|
+
test_files:
|
139
|
+
- spec/lib/sunspot_kaminari_support_spec.rb
|
140
|
+
- spec/spec_helper.rb
|