Skip to content

Commit

Permalink
Add will_paginate fixes and and refactored integration specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sutto committed Jun 9, 2012
1 parent 838078b commit 3ec4970
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 56 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ end
# 2.10 for the moment changes some exceptions.
gem 'rspec', '~> 2.9.0'

if (wp_version = ENV['WILL_PAGINATE_VERSION'])
gem 'will_paginate', wp_version
end

gemspec
21 changes: 20 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@ desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec)

namespace :spec do

namespace :integration do

desc "Run the will_paginate integrate specs"
RSpec::Core::RakeTask.new(:will_paginate) do |t|
t.rspec_opts = "--tag integration"
t.pattern = "./spec/integration/will_paginate_spec.rb"
end

desc "Run the will_paginate integrate specs"
RSpec::Core::RakeTask.new(:kaminari) do |t|
t.rspec_opts = "--tag integration"
t.pattern = "./spec/integration/kaminari_spec.rb"
end

end

desc "Run all specs with rcov"
RSpec::Core::RakeTask.new(:rcov) do |t|
t.rcov = true
t.pattern = "./spec/**/*_spec.rb"
t.rcov_opts = '--exclude spec/,/gems/,/Library/,/usr/,lib/tasks,.bundle,config,/lib/rspec/,/lib/rspec-'
end
end
end

task :default => ["spec:integration:will_paginate", "spec:integration:kaminari"]
2 changes: 1 addition & 1 deletion lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Respondable
extend ActiveSupport::Concern

def self.pagination_type(object)
if defined?(WillPaginate::Collection) && object.is_a?(WillPaginate::Collection)
if object.respond_to?(:total_entries)
:will_paginate
elsif object.respond_to?(:num_pages) && object.respond_to?(:current_page)
:kaminari
Expand Down
69 changes: 69 additions & 0 deletions spec/integration/kaminari_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe RocketPants::Base, 'kaminari integration', :integration => true, :target => 'kaminari' do
include ControllerHelpers

before :all do
begin
stderr, $stderr = $stderr, StringIO.new
require 'kaminari'
ensure
$stderr = stderr
end
end

describe 'on models' do

let(:table_manager) { ReversibleData.manager_for(:users) }

before :all do
table_manager.up!
25.times { |i| User.create :age => (18 + i) }
end

after(:all) do
User.delete_all
table_manager.down!
end

it 'should let you expose a kaminari-paginated collection' do
mock(TestController).test_data { User.page(1).per(5) }
get :test_data
content[:response].should be_present
content[:count].should == 5
content[:pagination].should be_present
content[:pagination][:count].should == 25
end

it 'should not expose non-paginated as paginated' do
mock(TestController).test_data { User.all }
get :test_data
content[:response].should be_present
content[:count].should == 25
content[:pagination].should_not be_present
end

end

describe 'on arrays' do

it 'should correctly convert a kaminari array' do
pager = Kaminari::PaginatableArray.new((1..200).to_a, :limit => 10, :offset => 10)
mock(TestController).test_data { pager }
get :test_data
content.should have_key(:pagination)
content[:pagination].should == {
:next => 3,
:current => 2,
:previous => 1,
:pages => 20,
:count => 200,
:per_page => 10
}.stringify_keys
content.should have_key(:count)
content[:count].should == 10
end

end

end
74 changes: 74 additions & 0 deletions spec/integration/will_paginate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'spec_helper'

describe RocketPants::Base, 'will_paginate integration', :integration => true, :target => 'will_paginate' do
include ControllerHelpers

before :all do
require 'will_paginate/active_record'
require 'will_paginate/collection'
end

describe 'on models' do

let(:table_manager) { ReversibleData.manager_for(:users) }

before :all do
table_manager.up!
25.times { |i| User.create :age => (18 + i) }
end

after(:all) do
User.delete_all
table_manager.down!
end

it 'should let you expose a classically paginated collection' do
mock(TestController).test_data { User.paginate :per_page => 5, :page => 1 }
get :test_data
content[:response].should be_present
content[:count].should == 5
content[:pagination].should be_present
content[:pagination][:count].should == 25
end

it 'should not expose non-paginated as paginated' do
mock(TestController).test_data { User.all }
get :test_data
content[:response].should be_present
content[:count].should == 25
content[:pagination].should_not be_present
end

it 'should let you expose a relational collection' do
mock(TestController).test_data { User.page(1).limit(5).all }
get :test_data
content[:response].should be_present
content[:count].should == 5
content[:pagination].should be_present
content[:pagination][:count].should == 25
end

end

describe 'on arrays' do

it 'should correctly convert a will paginate collection' do
pager = WillPaginate::Collection.create(2, 10) { |p| p.replace %w(a b c d e f g h i j); p.total_entries = 200 }
mock(TestController).test_data { pager }
get :test_data
content.should have_key(:pagination)
content[:pagination].should == {
:next => 3,
:current => 2,
:previous => 1,
:pages => 20,
:count => 200,
:per_page => 10
}.stringify_keys
content.should have_key(:count)
content[:count].should == 10
end

end

end
54 changes: 0 additions & 54 deletions spec/rocket_pants/controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
require 'spec_helper'
require 'logger'
require 'stringio'

require 'will_paginate/collection'
# Replace stderr because kaminari complains about not having a framework.
begin
stderr, $stderr = $stderr, StringIO.new
require 'kaminari'
ensure
$stderr = stderr
end

describe RocketPants::Base do
include ControllerHelpers
Expand Down Expand Up @@ -47,18 +39,6 @@
content[:count].should == 5
end

it 'should let you expose a kaminari-paginated collection' do
1.upto(5) do |offset|
User.create :age => (18 + offset)
end
mock(TestController).test_data { User.page(1).per(2) }
get :test_data
content[:response].should be_present
content[:count].should == 2
content[:pagination].should be_present
content[:pagination][:count].should == 5
end

end

end
Expand Down Expand Up @@ -93,40 +73,6 @@
response.status.should == 422
end

it 'should correctly convert a kaminari array' do
pager = Kaminari::PaginatableArray.new((1..200).to_a, :limit => 10, :offset => 10)
mock(TestController).test_data { pager }
get :test_data
content.should have_key(:pagination)
content[:pagination].should == {
:next => 3,
:current => 2,
:previous => 1,
:pages => 20,
:count => 200,
:per_page => 10
}.stringify_keys
content.should have_key(:count)
content[:count].should == 10
end

it 'should correctly convert a will paginate collection' do
pager = WillPaginate::Collection.create(2, 10) { |p| p.replace %w(a b c d e f g h i j); p.total_entries = 200 }
mock(TestController).test_data { pager }
get :test_data
content.should have_key(:pagination)
content[:pagination].should == {
:next => 3,
:current => 2,
:previous => 1,
:pages => 20,
:count => 200,
:per_page => 10
}.stringify_keys
content.should have_key(:count)
content[:count].should == 10
end

it 'should correctly convert a normal collection' do
mock(TestController).test_data { %w(a b c d) }
get :test_data
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
config.include I18nSpecHelper
config.include ConfigHelper
config.include WebmockResponses
config.filter_run_excluding :integration => true
end

0 comments on commit 3ec4970

Please sign in to comment.