solr_collection 0.1.2
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/README.markdown +29 -0
- data/Rakefile +20 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/solr_collection.rb +74 -0
- data/solr_collection.gemspec +50 -0
- data/spec/solr_collection_spec.rb +115 -0
- data/spec/spec_helper.rb +4 -0
- metadata +72 -0
data/README.markdown
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
SolrCollection is a wrapper for acts_as_solr results sets that behaves/feels like will_paginate collection
|
2
|
+
|
3
|
+
- no need to separate results from facets / spellcheck information
|
4
|
+
- all pages can use the same pagination helpers
|
5
|
+
- simple to mock an empty result when solr is down or throws errors
|
6
|
+
|
7
|
+
Usage
|
8
|
+
=====
|
9
|
+
- As Rails plugin ` script/plugin install git://github.com/grosser/solr_collection.git `
|
10
|
+
- As gem ` sudo gem install solr_collection `
|
11
|
+
|
12
|
+
Example:
|
13
|
+
options = {:limit=>10, :offset=>100, :facets=>{....}, .... }
|
14
|
+
results = MyModel.find_by_solr("abc", options) rescue []
|
15
|
+
results = SolrCollection.new(results, options)
|
16
|
+
|
17
|
+
do_something if results.facets
|
18
|
+
something_else if results.total_entries > results.size #more pages ?
|
19
|
+
will_paginate(results)
|
20
|
+
|
21
|
+
Other features:
|
22
|
+
#facet fields can be nil or [] or {} -> use has_facet_fields? to check
|
23
|
+
puts results.facets['facet_fields']['categories'] if results.has_facet_fields?
|
24
|
+
|
25
|
+
Author
|
26
|
+
======
|
27
|
+
[Michael Grosser](http://pragmatig.wordpress.com)
|
28
|
+
grosser.michael@gmail.com
|
29
|
+
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
task :default => :spec
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
|
4
|
+
|
5
|
+
begin
|
6
|
+
project_name = 'solr_collection'
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = project_name
|
10
|
+
gem.summary = "Wrapper for solr results sets then behaves/feels like will_paginate collection"
|
11
|
+
gem.email = "grosser.michael@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/grosser/#{project_name}"
|
13
|
+
gem.authors = ["Michael Grosser"]
|
14
|
+
gem.add_dependency "will_paginate"
|
15
|
+
end
|
16
|
+
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
20
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.2
|
data/init.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'will_paginate/collection'
|
2
|
+
|
3
|
+
#Proxy that delegates all calls to contained subject
|
4
|
+
# except solr related methods in MAPPED_FIELDS
|
5
|
+
class SolrCollection
|
6
|
+
MAPPED_FIELDS = [:facets, :spellcheck, {:total=>:total_entries}]
|
7
|
+
|
8
|
+
def initialize(solr_result, options={})
|
9
|
+
options = options.dup
|
10
|
+
|
11
|
+
#solr result has results in an array called results
|
12
|
+
results = if solr_result.respond_to?(:results)
|
13
|
+
solr_result.results
|
14
|
+
else
|
15
|
+
solr_result
|
16
|
+
end
|
17
|
+
|
18
|
+
#get fields from solr_result or set them to nil
|
19
|
+
@solr_data = {}
|
20
|
+
MAPPED_FIELDS.each do |fields|
|
21
|
+
if fields.is_a? Hash
|
22
|
+
solr_field = fields.keys.first
|
23
|
+
collection_field = fields.values.first
|
24
|
+
else
|
25
|
+
solr_field = collection_field = fields
|
26
|
+
end
|
27
|
+
|
28
|
+
@solr_data[collection_field] = if solr_result.respond_to?(solr_field)
|
29
|
+
solr_result.send(solr_field)
|
30
|
+
else
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
@solr_data[:total_entries] ||= options[:total_entries] || results.size
|
35
|
+
|
36
|
+
#build will_paginate collection from given options
|
37
|
+
options = fill_page_and_per_page(options)
|
38
|
+
|
39
|
+
@subject = WillPaginate::Collection.new(
|
40
|
+
options[:page],
|
41
|
+
options[:per_page],
|
42
|
+
@solr_data[:total_entries]
|
43
|
+
)
|
44
|
+
@subject.replace(results)
|
45
|
+
end
|
46
|
+
|
47
|
+
def has_facet_fields?
|
48
|
+
# can be a array, when empty reults where returned
|
49
|
+
!!(facets and facets['facet_fields'] and not facets['facet_fields'].empty?)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def fill_page_and_per_page(options)
|
55
|
+
options[:per_page] ||= options[:limit] || 10
|
56
|
+
if options[:page].to_s.empty?
|
57
|
+
options[:page] = if options[:offset]
|
58
|
+
(options[:offset] / options[:per_page]) + 1
|
59
|
+
else
|
60
|
+
1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
options
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(method, *args, &block)
|
67
|
+
method = method.to_sym
|
68
|
+
if @solr_data.key? method
|
69
|
+
@solr_data[method]
|
70
|
+
else
|
71
|
+
@subject.send(method, *args, &block)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{solr_collection}
|
8
|
+
s.version = "0.1.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Grosser"]
|
12
|
+
s.date = %q{2009-11-22}
|
13
|
+
s.email = %q{grosser.michael@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.markdown"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
"README.markdown",
|
19
|
+
"Rakefile",
|
20
|
+
"VERSION",
|
21
|
+
"init.rb",
|
22
|
+
"lib/solr_collection.rb",
|
23
|
+
"solr_collection.gemspec",
|
24
|
+
"spec/solr_collection_spec.rb",
|
25
|
+
"spec/spec_helper.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/grosser/solr_collection}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.5}
|
31
|
+
s.summary = %q{Wrapper for solr results sets then behaves/feels like will_paginate collection}
|
32
|
+
s.test_files = [
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/solr_collection_spec.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<will_paginate>, [">= 0"])
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<will_paginate>, [">= 0"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<will_paginate>, [">= 0"])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe SolrCollection do
|
4
|
+
it "behaves like an array" do
|
5
|
+
s = SolrCollection.new([1,2,3])
|
6
|
+
s.size.should == 3
|
7
|
+
s[1].should == 2
|
8
|
+
end
|
9
|
+
|
10
|
+
it "behaves like a will_paginate collection" do
|
11
|
+
s = SolrCollection.new([1,2,3], :page=>1, :per_page=>2, :total_entries=>20)
|
12
|
+
s.total_pages.should == 10
|
13
|
+
end
|
14
|
+
|
15
|
+
it "does not modify options" do
|
16
|
+
a = {:per_page=>2, :offset=>2}
|
17
|
+
SolrCollection.new([1], a).current_page.should == 2
|
18
|
+
a.should == {:per_page=>2, :offset=>2}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "pages" do
|
22
|
+
it "does not know page" do
|
23
|
+
lambda{ SolrCollection.new([1,2,3]).page }.should raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it "knows current_page" do
|
27
|
+
SolrCollection.new([1,2,3]).current_page.should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "knows per_page" do
|
31
|
+
SolrCollection.new([1,2,3]).per_page.should == 10
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can set page" do
|
35
|
+
SolrCollection.new([1,2,3], :page=>3).current_page.should == 3
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can set per_page" do
|
39
|
+
SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "limit and offset" do
|
44
|
+
it "uses per_page when per_page and limit are given" do
|
45
|
+
SolrCollection.new([1], :per_page=>2, :limit=>20, :offset=>10).current_page.should == 6
|
46
|
+
end
|
47
|
+
|
48
|
+
it "understands limit/offset" do
|
49
|
+
SolrCollection.new([1], :limit=>2, :offset=>10).current_page.should == 6
|
50
|
+
end
|
51
|
+
|
52
|
+
it "understands per_page/offset" do
|
53
|
+
SolrCollection.new([1], :per_page=>2, :offset=>2).current_page.should == 2
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "solr fields" do
|
58
|
+
it "knows factes" do
|
59
|
+
SolrCollection.new([]).facets.should == nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "knows spellcheck" do
|
63
|
+
SolrCollection.new([]).spellcheck.should == nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not know non-solr-method" do
|
67
|
+
lambda{ SolrCollection.new([]).fooo }.should raise_error
|
68
|
+
end
|
69
|
+
|
70
|
+
it "uses results as subject" do
|
71
|
+
a = []
|
72
|
+
def a.results; [1,2,3];end
|
73
|
+
SolrCollection.new(a)[2].should == 3
|
74
|
+
end
|
75
|
+
|
76
|
+
it "knows total_entries" do
|
77
|
+
SolrCollection.new([1,2,3]).total_entries.should == 3
|
78
|
+
end
|
79
|
+
|
80
|
+
it "can get total_entries from an array" do
|
81
|
+
a = [1,2,3]
|
82
|
+
SolrCollection.new(a, :per_page=>2).total_pages.should == 2
|
83
|
+
end
|
84
|
+
|
85
|
+
it "can get total_entries from a solr resultset" do
|
86
|
+
a = []
|
87
|
+
def a.total; 22;end
|
88
|
+
SolrCollection.new(a).total_entries.should == 22
|
89
|
+
end
|
90
|
+
|
91
|
+
it "does not overwrite total from solr resultset with given total_entries" do
|
92
|
+
a = []
|
93
|
+
def a.total; 22;end
|
94
|
+
SolrCollection.new(a, :total_entries=>33).total_entries.should == 22
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe :has_facet_fields do
|
99
|
+
it "is false when facets are nil" do
|
100
|
+
SolrCollection.new([]).has_facet_fields?.should == false
|
101
|
+
end
|
102
|
+
|
103
|
+
it "is false when facet_fieldss are an empty array" do
|
104
|
+
a = []
|
105
|
+
def a.facets; {'facet_fields'=>[]}; end
|
106
|
+
SolrCollection.new(a).has_facet_fields?.should == false
|
107
|
+
end
|
108
|
+
|
109
|
+
it "is true when it is a filled hash" do
|
110
|
+
a = []
|
111
|
+
def a.facets; {'facet_fields'=>{'x'=>'y'}}; end
|
112
|
+
SolrCollection.new(a).has_facet_fields?.should == true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solr_collection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Grosser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-22 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: will_paginate
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: grosser.michael@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.markdown
|
33
|
+
files:
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- init.rb
|
38
|
+
- lib/solr_collection.rb
|
39
|
+
- solr_collection.gemspec
|
40
|
+
- spec/solr_collection_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/grosser/solr_collection
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Wrapper for solr results sets then behaves/feels like will_paginate collection
|
70
|
+
test_files:
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/solr_collection_spec.rb
|