Permalink
Browse files

refactor search pagination using latest will_paginate

  • Loading branch information...
1 parent d667b6e commit ccf772a3d13dda52ce36778d6d1cd7d759342904 @mislav committed Aug 30, 2011
Showing with 19 additions and 19 deletions.
  1. +1 −1 Gemfile
  2. +2 −2 Gemfile.lock
  3. +16 −16 models.rb
View
@@ -15,7 +15,7 @@ gem 'activesupport'
gem 'builder'
gem 'i18n'
gem 'indextank'
-gem 'will_paginate', '~> 3.0.pre2'
+gem 'will_paginate', '~> 3.0.0'
gem 'mingo'
gem 'activemodel'
gem 'mongo_ext'
View
@@ -66,7 +66,7 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.2)
- will_paginate (3.0.pre2)
+ will_paginate (3.0.0)
yajl-ruby (0.8.2)
PLATFORMS
@@ -91,4 +91,4 @@ DEPENDENCIES
shotgun
sinatra
thin
- will_paginate (~> 3.0.pre2)
+ will_paginate (~> 3.0.0)
View
@@ -1,7 +1,7 @@
require 'mingo'
require 'active_support/memoizable'
require 'indextank'
-require 'will_paginate/finders/base'
+require 'will_paginate/collection'
require 'hashie/mash'
require 'net/http'
@@ -101,30 +101,30 @@ def self.get_url(url)
class IndexedPhoto < Struct.new(:id, :caption, :thumbnail_url, :large_url, :username, :taken_at, :filter_name)
Fields = 'text,thumbnail_url,username,timestamp,big,filter'
- class << self
- include WillPaginate::Finders::Base
+ extend WillPaginate::PerPage
- protected
+ self.per_page = 32
- def wp_query(query_options, pager, args, &block)
- query = args.first
- filter = query_options.delete(:filter)
- query = "#{query} AND filter:#{filter}" if filter
+ def self.paginate(query, options)
+ options = options.dup
+ page = WillPaginate::PageNumber(options.fetch(:page) || 1)
+ per_page = options.delete(:per_page) || self.per_page
+ filter = options.delete(:filter)
+ query = "#{query} AND filter:#{filter}" if filter
- params = {:len => pager.per_page, :start => pager.offset, :fetch => Fields}.update(query_options)
+ WillPaginate::Collection.create(page, per_page) do |col|
+ params = {:len => col.per_page, :start => col.offset, :fetch => Fields}.update(options)
data = ActiveSupport::Notifications.instrument('search.indextank', {:query => query}.update(params)) do
search_index.search(query, params)
end
- pager.total_entries = data['matches']
- pager.replace data['results'].map { |item| new item }
- end
-
- def search_index
- Sinatra::Application.settings.search_index
+ col.total_entries = data['matches']
+ col.replace data['results'].map { |item| new item }
end
end
- self.per_page = 32
+ def self.search_index
+ Sinatra::Application.settings.search_index
+ end
def initialize(hash)
super hash['docid'], hash['text'], hash['thumbnail_url'], hash['big'],

0 comments on commit ccf772a

Please sign in to comment.