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

Warn when no ordering is in place #1054

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ module ActiveRecordModelExtension
eval <<-RUBY, nil, __FILE__, __LINE__ + 1
def self.#{Kaminari.config.page_method_name}(num = nil)
per_page = max_per_page && (default_per_page > max_per_page) ? max_per_page : default_per_page
limit(per_page).offset(per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
scope = limit(per_page).offset(per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
include Kaminari::ActiveRecordRelationMethods
include Kaminari::PageScopeMethods
end
# Using limit and offset is unreliable without ordering
# Warn the developers using paginatin wihout the order clause
if scope.order_values.empty?
warn "WARNING: It seems you're using pagination without an ORDER BY clause."
warn "This might result in unexpected or random records on paginated results."
end
scope
end
RUBY
end
Expand Down
17 changes: 17 additions & 0 deletions kaminari-core/test/models/active_record/scopes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ class ActiveRecordModelExtensionTest < ActiveSupport::TestCase
Kaminari.configure {|config| config.page_method_name = :page }
end
end

test 'Show warning when using scope without ordering' do
expected_err = "WARNING: It seems you're using pagination without an ORDER BY clause.\nThis might result in unexpected or random records on paginated results.\n"
log, err = capture_output do
User.page(1)
end

assert_equal(err, expected_err)
end

test "Don't whow warning when scope is used with order" do
log, err = capture_output do
User.order(User.primary_key).page(1)
end

assert_equal(err,'')
end
end

class ActiveRecordExtensionTest < ActiveSupport::TestCase
Expand Down