We're supporting AR >= 4.1 now

This commit is contained in:
Akira Matsuda 2016-10-12 06:45:31 +09:00
parent 3eaf05444b
commit ce616894a2
2 changed files with 4 additions and 28 deletions

View File

@ -17,16 +17,12 @@ module Kaminari
# 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 OrderedHash that responds to #count
c = c.count(*args)
c = c.count(column_name)
if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
c.count
else
c.respond_to?(:count) ? c.count(*args) : c
c.respond_to?(:count) ? c.count(column_name) : c
end
end
end

View File

@ -30,11 +30,7 @@ if defined? ActiveRecord
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 pattern
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
User.includes(:books_authored).references(:books).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
end
end
@ -45,24 +41,8 @@ if defined? ActiveRecord
end
context "when total_count receives options" do
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
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
if ActiveRecord::VERSION::STRING < '4.1.0'
context 'when count receives options' do
it 'should return a distinct set by column for rails < 4.1' do
User.page(1).count(:name, :distinct => true).should == 4
end
User.page(1).total_count(:name, :distinct => true).should == 7
end
end