From 157586b5b3c827ac53f2a2b9598d216f708d6c8c Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Wed, 21 May 2014 17:07:29 -0700 Subject: [PATCH] Fix a bug where the friendly url for the first page is broken fixes #554 --- lib/kaminari/helpers/tags.rb | 6 +++--- spec/fake_app/rails_app.rb | 3 +++ spec/helpers/tags_spec.rb | 32 ++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/kaminari/helpers/tags.rb b/lib/kaminari/helpers/tags.rb index e312799..2d30e08 100644 --- a/lib/kaminari/helpers/tags.rb +++ b/lib/kaminari/helpers/tags.rb @@ -40,13 +40,13 @@ module Kaminari if page <= 1 # This converts a hash: # from: {other: "params", page: 1} - # to: {other: "params"} + # to: {other: "params", page: nil} # (when @param_name == "page") # # from: {other: "params", user: {name: "yuki", page: 1}} - # to: {other: "params", user: {name: "yuki"}} + # to: {other: "params", user: {name: "yuki", page: nil}} # (when @param_name == "user[page]") - @param_name.to_s.scan(/\w+/)[0..-2].inject(page_params){|h, k| h[k] }.delete($&) + @param_name.to_s.scan(/\w+/)[0..-2].inject(page_params){|h, k| h[k] }[$&] = nil end page_params diff --git a/spec/fake_app/rails_app.rb b/spec/fake_app/rails_app.rb index 6180331..8880db6 100644 --- a/spec/fake_app/rails_app.rb +++ b/spec/fake_app/rails_app.rb @@ -20,6 +20,9 @@ app.initialize! # routes app.routes.draw do resources :users + resources :addresses do + get 'page/:page', :action => :index, :on => :collection + end end #models diff --git a/spec/helpers/tags_spec.rb b/spec/helpers/tags_spec.rb index 734a2a0..cbb280e 100644 --- a/spec/helpers/tags_spec.rb +++ b/spec/helpers/tags_spec.rb @@ -4,27 +4,35 @@ include Kaminari::Helpers describe 'Kaminari::Helpers' do describe 'Tag' do describe '#page_url_for', :if => defined?(Rails) do + context "with a friendly route setting" do + before do + helper.request.assign_parameters(_routes, "addresses", "index", :page => 3) + end + + context "for first page" do + subject { Tag.new(helper).page_url_for(1) } + it { should == "/addresses" } + end + + context "for other page" do + subject { Tag.new(helper).page_url_for(5) } + it { should == "/addresses/page/5" } + end + end + context "with param_name = 'user[page]' option" do before do - stub(helper).params do - { - :controller => 'users', - :action => 'index', - :user => { - :scope => "active", - :page => 3 - } - }.with_indifferent_access - end + helper.request.assign_parameters(_routes, "users", "index") + helper.params.merge!(:user => {:page => "3", :scope => "active"}) end context "for first page" do subject { Tag.new(helper, :param_name => "user[page]").page_url_for(1) } if ActiveSupport::VERSION::STRING < "3.1.0" - it { should_not match(/user\[page\]/) } + it { should_not match(/user\[page\]=\d+/) } it { should match(/user\[scope\]=active/) } else - it { should_not match(/user%5Bpage%5D/) } # not match user[page] + it { should_not match(/user%5Bpage%5D=\d+/) } # not match user[page]=\d+ it { should match(/user%5Bscope%5D=active/) } # match user[scope]=active end end