From a821a9ecd79e0d1a2ed259783fec4e0a07f7c21e Mon Sep 17 00:00:00 2001 From: Bryan Ricker Date: Thu, 17 Oct 2013 23:46:53 -0700 Subject: [PATCH 1/7] Fix arity problem with rails 4.1.0 Fix specs for airty check Don't check arity, just check the AR version --- .../models/active_record_relation_methods.rb | 8 +++++-- .../active_record_relation_methods_spec.rb | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/kaminari/models/active_record_relation_methods.rb b/lib/kaminari/models/active_record_relation_methods.rb index 972072d15..3cbe842ec 100644 --- a/lib/kaminari/models/active_record_relation_methods.rb +++ b/lib/kaminari/models/active_record_relation_methods.rb @@ -16,12 +16,16 @@ def total_count(column_name = :all, options = {}) #:nodoc: # Remove includes only if they are irrelevant c = c.except(:includes) unless references_eager_loaded_tables? + # Rails 4.1 removes the `options` argument from AR::Relation#count + args = [column_name] + args << options if ActiveRecord::VERSION::STRING < '4.1.0' + # .group returns an OrderdHash that responds to #count - c = c.count(column_name, options) + c = c.count(*args) if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash) c.count else - c.respond_to?(:count) ? c.count(column_name, options) : c + c.respond_to?(:count) ? c.count(*args) : c end end end diff --git a/spec/models/active_record/active_record_relation_methods_spec.rb b/spec/models/active_record/active_record_relation_methods_spec.rb index f75dd6f52..548d7c99f 100644 --- a/spec/models/active_record/active_record_relation_methods_spec.rb +++ b/spec/models/active_record/active_record_relation_methods_spec.rb @@ -23,7 +23,11 @@ context "when the scope use conditions on includes" do it "should keep includes and successfully count the results" do # Only @author and @author2 have books titled with the title00x partern - User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2 + if ActiveRecord::VERSION::STRING >= "4.1.0" + User.includes(:books_authored).references(:books).where("books.title LIKE 'title00%'").page(1).total_count.should == 2 + else + User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2 + end end end @@ -34,14 +38,24 @@ end context "when total_count receives options" do - it "should return a distinct total count" do - User.page(1).total_count(:name, :distinct => true).should == 4 + it "should return a distinct total count for rails ~> 4.0.0" do + if ActiveRecord::VERSION::STRING < "4.1.0" + User.page(1).total_count(:name, :distinct => true).should == 4 + end + end + + it "should ignore the options for rails 4.1+" do + if ActiveRecord::VERSION::STRING >= "4.1.0" + User.page(1).total_count(:name, :distinct => true).should == 7 + end end end context "when count receives options" do - it "should return a distinct set by column" do - User.page(1).count(:name, :distinct => true).should == 4 + it "should return a distinct set by column for rails ~> 4.0.0" do + if ActiveRecord::VERSION::STRING < "4.1.0" + User.page(1).count(:name, :distinct => true).should == 4 + end end end From 98018da222b0738bac558348390b142b04bd2358 Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 12:44:05 -0300 Subject: [PATCH 2/7] Clean with truncation at the start of each suite. This keeps things working properly for mongo which does not support transactions. The strategy is still properly configured to use transactions where supported, though, so this should not affect the speed of the test suite for other ORMs. --- spec/support/database_cleaner.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index c75f50014..05a5c13be 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -5,8 +5,6 @@ RSpec.configure do |config| config.before :suite do - #DatabaseCleaner.clean_with :truncation - DatabaseCleaner.clean_with :truncation if defined? ActiveRecord DatabaseCleaner.clean_with :truncation if defined? DataMapper DatabaseCleaner.clean_with :truncation if defined? Mongoid From 8438b375dec2a7945997125c6bca63adf2e594a8 Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 13:46:36 -0300 Subject: [PATCH 3/7] Fix for Model.all.page in DataMapper --- lib/kaminari/models/data_mapper_extension.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kaminari/models/data_mapper_extension.rb b/lib/kaminari/models/data_mapper_extension.rb index 4778da7a9..396dba8ab 100644 --- a/lib/kaminari/models/data_mapper_extension.rb +++ b/lib/kaminari/models/data_mapper_extension.rb @@ -5,8 +5,10 @@ module DataMapperExtension module Paginatable class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{Kaminari.config.page_method_name}(num = 1) + model = self + model = self.model if self.is_a? DataMapper::Collection num = [num.to_i, 1].max - 1 - all(:limit => default_per_page, :offset => default_per_page * num).extend Paginating + all(:limit => model.default_per_page, :offset => model.default_per_page * num).extend Paginating end RUBY end From b75741b50f97197f728d99060796e3d8d6db7eaf Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 13:50:50 -0300 Subject: [PATCH 4/7] Restoring global configuration value. Without setting this back to nil, this affects all other tests. --- spec/models/data_mapper/data_mapper_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/data_mapper/data_mapper_spec.rb b/spec/models/data_mapper/data_mapper_spec.rb index aedcc6808..e16657d74 100644 --- a/spec/models/data_mapper/data_mapper_spec.rb +++ b/spec/models/data_mapper/data_mapper_spec.rb @@ -24,6 +24,7 @@ User.paginates_per(5) User.page(1).length.should == 5 User.all.page(1).length.should == 5 + User.paginates_per(nil) # reset to default } end From 0ab86e22049536a10b0a6d04bec3a98d0a5ebf2d Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 13:56:29 -0300 Subject: [PATCH 5/7] Removed invalid test. No other ORM tests for `current_page` to work on the model, and there is no documentation that indicates that this should work this way. I believe this test was added accidentally. --- spec/models/data_mapper/data_mapper_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/data_mapper/data_mapper_spec.rb b/spec/models/data_mapper/data_mapper_spec.rb index e16657d74..f81b46201 100644 --- a/spec/models/data_mapper/data_mapper_spec.rb +++ b/spec/models/data_mapper/data_mapper_spec.rb @@ -17,7 +17,6 @@ describe 'Model' do subject { User } - its(:current_page) { should == 1 } it { User.all.count.should == 100 } it { User.page(1).length.should == 25 } it { From bcb39fa03dda9c66ffb08ec06214f99ef150708a Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 14:15:56 -0300 Subject: [PATCH 6/7] Fixed tests targeting Rails ~> 4.0.0. --- .../active_record/active_record_relation_methods_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/active_record/active_record_relation_methods_spec.rb b/spec/models/active_record/active_record_relation_methods_spec.rb index 548d7c99f..c33951ff4 100644 --- a/spec/models/active_record/active_record_relation_methods_spec.rb +++ b/spec/models/active_record/active_record_relation_methods_spec.rb @@ -39,7 +39,7 @@ context "when total_count receives options" do it "should return a distinct total count for rails ~> 4.0.0" do - if ActiveRecord::VERSION::STRING < "4.1.0" + if ActiveRecord::VERSION::STRING > "4.0.0" && ActiveRecord::VERSION::STRING < "4.1.0" User.page(1).total_count(:name, :distinct => true).should == 4 end end @@ -53,7 +53,7 @@ context "when count receives options" do it "should return a distinct set by column for rails ~> 4.0.0" do - if ActiveRecord::VERSION::STRING < "4.1.0" + if ActiveRecord::VERSION::STRING > "4.0.0" && ActiveRecord::VERSION::STRING < "4.1.0" User.page(1).count(:name, :distinct => true).should == 4 end end From 06fec6598394001d70b492065fdb71d464d5c677 Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 25 Oct 2013 15:01:36 -0300 Subject: [PATCH 7/7] Tests targeting Rails < 4.1 rather than ~> 4.0.0 --- .../active_record/active_record_relation_methods_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/models/active_record/active_record_relation_methods_spec.rb b/spec/models/active_record/active_record_relation_methods_spec.rb index c33951ff4..fa45fcc46 100644 --- a/spec/models/active_record/active_record_relation_methods_spec.rb +++ b/spec/models/active_record/active_record_relation_methods_spec.rb @@ -38,8 +38,8 @@ end context "when total_count receives options" do - it "should return a distinct total count for rails ~> 4.0.0" do - if ActiveRecord::VERSION::STRING > "4.0.0" && ActiveRecord::VERSION::STRING < "4.1.0" + it "should return a distinct total count for rails < 4.1" do + if ActiveRecord::VERSION::STRING < "4.1.0" User.page(1).total_count(:name, :distinct => true).should == 4 end end @@ -52,8 +52,8 @@ end context "when count receives options" do - it "should return a distinct set by column for rails ~> 4.0.0" do - if ActiveRecord::VERSION::STRING > "4.0.0" && ActiveRecord::VERSION::STRING < "4.1.0" + it "should return a distinct set by column for rails < 4.1" do + if ActiveRecord::VERSION::STRING < "4.1.0" User.page(1).count(:name, :distinct => true).should == 4 end end