Support pagination in mounted sub apps in padrino #251

Closed
wants to merge 7 commits into
from
View
@@ -8,5 +8,6 @@ gem 'sinatra-contrib', '>= 1.3'
gem 'nokogiri'
gem 'xpath'
gem 'mime-types'
+gem 'padrino-core'
@yuki24

yuki24 Aug 7, 2013

Owner

Is this mandatory? Why not just using Sinatra? If you add support for Sinatra, Padrino will also probably work as you expect.

gemspec :path => '../'
@@ -84,7 +84,7 @@ module HelperMethods
# * <tt>:remote</tt> - Ajax? (false by default)
# * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
def paginate(scope, options = {}, &block)
- current_path = env['PATH_INFO'] rescue nil
+ current_path = request.path rescue nil
current_params = Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {}
paginator = Kaminari::Helpers::Paginator.new(
ActionViewTemplateProxy.new(:current_params => current_params, :current_path => current_path, :param_name => options[:param_name] || Kaminari.config.param_name),
@@ -116,7 +116,7 @@ def link_to_next_page(scope, name, options = {})
placeholder = options.delete(:placeholder)
query = params.merge(param_name => (scope.current_page + 1))
unless scope.last_page?
- link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'next')
+ link_to name, request.path + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'next')
else
placeholder
end
@@ -41,6 +41,63 @@
end
end
+ context "mounted padrino app" do
+ before do
+ Padrino.clear!
+
+ class BaseApp < Padrino::Application; end
+ class MountedApp < Padrino::Application
+
+ register Kaminari::Helpers::SinatraHelpers
+
+ controllers :users do
+ get :index do
+ @page = params[:page] || 1
+ @users = User.page(@page)
+ @options = {}
+
+ erb ERB_TEMPLATE_FOR_PAGINATE
+ end
+ end
+
+ end
+
+ Padrino.mount('base_app').to('/')
+ Padrino.mount('mounted_app').to('/admin')
+ def app
+ Rack::Lint.new(Padrino.application)
+ end
+ end
+
+ it "should do ???" do
@zzak

zzak Aug 7, 2013

Member

Can you implement this spec?

+ # get '/admin/users'
+ end
+ end
+
+ context "namespaced sinatra app" do
+ before do
+ mock_app do
+ register Kaminari::Helpers::SinatraHelpers
+ register Sinatra::Namespace
+
+ namespace '/admin' do
+ get '/users' do
+ @page = params[:page] || 1
+ @users = User.page(@page).per(5)
+ @options = {}
+ erb ERB_TEMPLATE_FOR_PAGINATE
+ end
+ end
+ end
+ get '/admin/users'
+ end
+
+ it "should get the current path correctly" do
+ current_path = last_document.search('.page a').map(&:attributes).map{|a| a['href']}.map(&:value).map{|x| x.split('?').first}.uniq.first
+ current_path.should eq '/admin/users'
+ end
+ end
+
context 'normal paginations with Sinatra' do
before { get '/users' }