lib/rsolr/pagination.rb in rsolr-1.0.0.beta5 vs lib/rsolr/pagination.rb in rsolr-1.0.0
- old
+ new
@@ -1,11 +1,11 @@
module RSolr::Pagination
# Calculates the "start" and "rows" Solr params
# by inspecting the :per_page and :page params.
def self.calculate_start_and_rows page, per_page
- per_page ||= 10
+ per_page = per_page.to_s.to_i
page = page.to_s.to_i-1
page = page < 1 ? 0 : page
start = page * per_page
[start, per_page]
end
@@ -14,20 +14,20 @@
# -- note, this must mixed-in via
# "extend" on a RSolr::Client instance.
module Client
# A paginated request method.
- def paginate page, per_page, path, opts = {}
- request_context = build_paginated_request page, per_page, path, opts = {}
- puts request_context.inspect
+ def paginate page, per_page, path, opts = nil
+ request_context = build_paginated_request page, per_page, path, opts
execute request_context
end
# Just like RSolr::Client #build_request
# but converts the page and per_page
# arguments into :rows and :start.
- def build_paginated_request page, per_page, path, opts = {}
+ def build_paginated_request page, per_page, path, opts = nil
+ opts ||= {}
opts[:page] = page
opts[:per_page] = per_page
opts[:params] ||= {}
values = RSolr::Pagination.calculate_start_and_rows(page, per_page)
opts[:params][:start] = values[0]
@@ -46,42 +46,33 @@
# start with "paginate_",
# the original/super
# RSolr::Client #method_missing
# method is called.
def method_missing name, *args
- if name.to_s =~ /^paginate_(.+)$/
+ if name.to_s =~ /^paginated?_(.+)$/
paginate args[0], args[1], $1, *args[2..-1]
else
super name, *args
end
end
# Overrides the RSolr::Client #evaluate_ruby_response method.
# Calls the original/super
# RSolr::Client #evaluate_ruby_response method.
- # Mixes in the PaginatedResponse if
+ # Mixes in the PaginatedDocSet if
# the request[:page] and request[:per_page]
# opts are set.
def evaluate_ruby_response request, response
result = super request, response
- result.extend(PaginatedResponse) if request[:page] && request[:per_page]
+ if request[:page] && request[:per_page] && result["response"] && result["response"]["docs"]
+ d = result['response']['docs'].extend PaginatedDocSet
+ d.per_page = request[:per_page]
+ d.start = request[:params][:start]
+ d.total = result["response"]["numFound"].to_s.to_i
+ end
result
end
- end
-
- module PaginatedResponse
- # TODO: self["responseHeader"]["params"]["rows"]
- # will not be available if omitHeader is false...
- # so, a simple "extend" probably isn't going to cut it.
- def self.extended base
- return unless base["response"] && base["response"]["docs"]
- d = base['response']['docs']
- d.extend PaginatedDocSet
- d.per_page = self["responseHeader"]["params"]["rows"].to_s.to_i rescue 10
- d.start = base["response"]["start"].to_s.to_i
- d.total = base["response"]["numFound"].to_s.to_i
- end
end
# A response module which gets mixed into the solr ["response"]["docs"] array.
module PaginatedDocSet
\ No newline at end of file