Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes without_count last_page? behaviour #1009

Merged
merged 8 commits into from Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -59,11 +59,11 @@ def load
else
@values[:limit] = limit_value + 1
# FIXME: this could be removed when we're dropping AR 4 support
@arel.limit = @values[:limit] if @arel && (Integer === @arel.limit)
@arel.limit = adjusted_limit(@values[:limit]) if @arel && adjustable?(@arel.limit)
super
@values[:limit] = limit_value - 1
# FIXME: this could be removed when we're dropping AR 4 support
@arel.limit = @values[:limit] if @arel && (Integer === @arel.limit)
@arel.limit = adjusted_limit(@values[:limit]) if @arel && adjustable?(@arel.limit)

if @records.any?
@records = @records.dup if (frozen = @records.frozen?)
Expand All @@ -75,6 +75,19 @@ def load
end
end

def adjustable?(obj)
obj.class == Integer || obj.class == Fixnum || (obj.class == Arel::Nodes::BindParam && obj.respond_to?(:value))
end

def adjusted_limit(limit)
case @arel.limit.class
when Integer, Fixnum
limit
when Arel::Nodes::BindParam
Arel::Nodes::BindParam.new(@arel.limit.value.with_cast_value(limit))
end
end

# The page wouldn't be the last page if there's "limit + 1" record
def last_page?
!out_of_range? && !@_has_next
Expand Down
Expand Up @@ -70,6 +70,22 @@ def self.shutdown
assert @users.out_of_range?
end

test 'regression: call arel first' do
@user = User.page(1).without_count
@user.arel

assert_equal false, @user.last_page?
end

test 'regression: call last page first' do
@user = User.page(1).without_count

@user.last_page?
@user.arel

assert_equal false, @user.last_page?
end

def assert_no_queries
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do
raise 'A SQL query is being made to the db:'
Expand Down