From 3ec4970d0e9eec94e93f18a7c01c030cd752f26b Mon Sep 17 00:00:00 2001 From: Darcy Laycock Date: Sat, 9 Jun 2012 13:38:48 +0800 Subject: [PATCH] Add will_paginate fixes and and refactored integration specs --- Gemfile | 4 ++ Rakefile | 21 +++++- lib/rocket_pants/controller/respondable.rb | 2 +- spec/integration/kaminari_spec.rb | 69 ++++++++++++++++++++ spec/integration/will_paginate_spec.rb | 74 ++++++++++++++++++++++ spec/rocket_pants/controller_spec.rb | 54 ---------------- spec/spec_helper.rb | 1 + 7 files changed, 169 insertions(+), 56 deletions(-) create mode 100644 spec/integration/kaminari_spec.rb create mode 100644 spec/integration/will_paginate_spec.rb diff --git a/Gemfile b/Gemfile index def9dc7..48f9c9a 100644 --- a/Gemfile +++ b/Gemfile @@ -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 \ No newline at end of file diff --git a/Rakefile b/Rakefile index 3b1ff9b..4ec635f 100644 --- a/Rakefile +++ b/Rakefile @@ -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 \ No newline at end of file +end + +task :default => ["spec:integration:will_paginate", "spec:integration:kaminari"] \ No newline at end of file diff --git a/lib/rocket_pants/controller/respondable.rb b/lib/rocket_pants/controller/respondable.rb index c1166f6..3319b4e 100644 --- a/lib/rocket_pants/controller/respondable.rb +++ b/lib/rocket_pants/controller/respondable.rb @@ -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 diff --git a/spec/integration/kaminari_spec.rb b/spec/integration/kaminari_spec.rb new file mode 100644 index 0000000..e6b6bb6 --- /dev/null +++ b/spec/integration/kaminari_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/integration/will_paginate_spec.rb b/spec/integration/will_paginate_spec.rb new file mode 100644 index 0000000..3e8cbc5 --- /dev/null +++ b/spec/integration/will_paginate_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/rocket_pants/controller_spec.rb b/spec/rocket_pants/controller_spec.rb index c1ca874..e0bf4cd 100644 --- a/spec/rocket_pants/controller_spec.rb +++ b/spec/rocket_pants/controller_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5c2b55f..c23f554 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,4 +16,5 @@ config.include I18nSpecHelper config.include ConfigHelper config.include WebmockResponses + config.filter_run_excluding :integration => true end \ No newline at end of file