mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
parent
0c354253a3
commit
baf4704978
5 changed files with 41 additions and 3 deletions
|
@ -25,6 +25,7 @@ module Kaminari
|
||||||
config_accessor :right
|
config_accessor :right
|
||||||
config_accessor :page_method_name
|
config_accessor :page_method_name
|
||||||
config_accessor :max_pages
|
config_accessor :max_pages
|
||||||
|
config_accessor :params_on_first_page
|
||||||
|
|
||||||
def param_name
|
def param_name
|
||||||
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
||||||
|
@ -47,5 +48,6 @@ module Kaminari
|
||||||
config.page_method_name = :page
|
config.page_method_name = :page
|
||||||
config.param_name = :page
|
config.param_name = :page
|
||||||
config.max_pages = nil
|
config.max_pages = nil
|
||||||
|
config.params_on_first_page = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module Kaminari::Helpers
|
||||||
|
|
||||||
def url_for(params)
|
def url_for(params)
|
||||||
extra_params = {}
|
extra_params = {}
|
||||||
if page = params[@param_name] and page != 1
|
if (page = params[@param_name]) && (Kaminari.config.params_on_first_page || page != 1)
|
||||||
extra_params[@param_name] = page
|
extra_params[@param_name] = page
|
||||||
end
|
end
|
||||||
query = @current_params.merge(extra_params)
|
query = @current_params.merge(extra_params)
|
||||||
|
|
|
@ -37,7 +37,7 @@ module Kaminari
|
||||||
page_params = Rack::Utils.parse_nested_query("#{@param_name}=#{page}")
|
page_params = Rack::Utils.parse_nested_query("#{@param_name}=#{page}")
|
||||||
page_params = @params.with_indifferent_access.deep_merge(page_params)
|
page_params = @params.with_indifferent_access.deep_merge(page_params)
|
||||||
|
|
||||||
if page <= 1
|
if !Kaminari.config.params_on_first_page && page <= 1
|
||||||
# This converts a hash:
|
# This converts a hash:
|
||||||
# from: {other: "params", page: 1}
|
# from: {other: "params", page: 1}
|
||||||
# to: {other: "params", page: nil}
|
# to: {other: "params", page: nil}
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'spec_helper'
|
||||||
|
|
||||||
describe Kaminari::Configuration do
|
describe Kaminari::Configuration do
|
||||||
subject { Kaminari.config }
|
subject { Kaminari.config }
|
||||||
|
|
||||||
describe 'default_per_page' do
|
describe 'default_per_page' do
|
||||||
context 'by default' do
|
context 'by default' do
|
||||||
its(:default_per_page) { should == 25 }
|
its(:default_per_page) { should == 25 }
|
||||||
|
@ -88,4 +89,21 @@ describe Kaminari::Configuration do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'params_on_first_page' do
|
||||||
|
context 'by default' do
|
||||||
|
its(:params_on_first_page) { should be_false }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'configure via config block' do
|
||||||
|
before do
|
||||||
|
Kaminari.configure {|c| c.params_on_first_page = true }
|
||||||
|
end
|
||||||
|
after do
|
||||||
|
Kaminari.configure {|c| c.params_on_first_page = false }
|
||||||
|
end
|
||||||
|
|
||||||
|
its(:params_on_first_page) { should be_true }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,19 @@ include Kaminari::Helpers
|
||||||
describe 'Kaminari::Helpers' do
|
describe 'Kaminari::Helpers' do
|
||||||
describe 'Tag' do
|
describe 'Tag' do
|
||||||
describe '#page_url_for', :if => defined?(Rails) do
|
describe '#page_url_for', :if => defined?(Rails) do
|
||||||
|
before { helper.request.assign_parameters(_routes, "users", "index") }
|
||||||
|
|
||||||
|
context "for first page" do
|
||||||
|
subject { Tag.new(helper).page_url_for(1) }
|
||||||
|
it { should == "/users" }
|
||||||
|
|
||||||
|
context 'config.params_on_first_page == false' do
|
||||||
|
before { Kaminari.config.params_on_first_page = true }
|
||||||
|
after { Kaminari.config.params_on_first_page = false }
|
||||||
|
it { should == "/users?page=1" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "with a friendly route setting" do
|
context "with a friendly route setting" do
|
||||||
before do
|
before do
|
||||||
helper.request.assign_parameters(_routes, "addresses", "index", :page => 3)
|
helper.request.assign_parameters(_routes, "addresses", "index", :page => 3)
|
||||||
|
@ -12,6 +25,12 @@ describe 'Kaminari::Helpers' do
|
||||||
context "for first page" do
|
context "for first page" do
|
||||||
subject { Tag.new(helper).page_url_for(1) }
|
subject { Tag.new(helper).page_url_for(1) }
|
||||||
it { should == "/addresses" }
|
it { should == "/addresses" }
|
||||||
|
|
||||||
|
context 'config.params_on_first_page == false' do
|
||||||
|
before { Kaminari.config.params_on_first_page = true }
|
||||||
|
after { Kaminari.config.params_on_first_page = false }
|
||||||
|
it { should match("/addresses/page/1") }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "for other page" do
|
context "for other page" do
|
||||||
|
@ -22,7 +41,6 @@ describe 'Kaminari::Helpers' do
|
||||||
|
|
||||||
context "with param_name = 'user[page]' option" do
|
context "with param_name = 'user[page]' option" do
|
||||||
before do
|
before do
|
||||||
helper.request.assign_parameters(_routes, "users", "index")
|
|
||||||
helper.params.merge!(:user => {:page => "3", :scope => "active"})
|
helper.params.merge!(:user => {:page => "3", :scope => "active"})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue