Permalink
Browse files

Sinatra has taken the stage

  • Loading branch information...
1 parent 095dbb0 commit e8205b505bdf6978696905b5e66ac7dc33c389fe @mislav committed Aug 2, 2011
Showing with 45 additions and 0 deletions.
  1. +4 −0 lib/will_paginate.rb
  2. +41 −0 lib/will_paginate/view_helpers/sinatra.rb
View
4 lib/will_paginate.rb
@@ -17,3 +17,7 @@ module WillPaginate
end
end
end
+
+if defined?(Sinatra) and Sinatra.respond_to? :register
+ require 'will_paginate/view_helpers/sinatra'
+end
View
41 lib/will_paginate/view_helpers/sinatra.rb
@@ -0,0 +1,41 @@
+require 'sinatra/base'
+require 'will_paginate/view_helpers'
+require 'will_paginate/view_helpers/link_renderer'
+
+module WillPaginate
+ module Sinatra
+ module Helpers
+ include ViewHelpers
+
+ def will_paginate(collection, options = {}) #:nodoc:
+ options = options.merge(:renderer => LinkRenderer) unless options[:renderer]
+ super(collection, options)
+ end
+ end
+
+ class LinkRenderer < ViewHelpers::LinkRenderer
+ protected
+
+ def url(page)
+ str = File.join(request.script_name.to_s, request.path_info)
+ params = request.GET.merge(param_name.to_s => page.to_s)
+ params.update @options[:params] if @options[:params]
+ str << '?' << build_query(params)
+ end
+
+ def request
+ @template.request
+ end
+
+ def build_query(params)
+ Rack::Utils.build_nested_query params
+ end
+ end
+
+ def self.registered(app)
+ app.helpers Helpers
+ end
+
+ ::Sinatra.register self
+ end
+end

0 comments on commit e8205b5

Please sign in to comment.