reports: fix exception when using period option and filtering by association.
Fix an exception in reports like this: * https://betabooru.donmai.us/reports/posts?search[period]=day&search[uploader][name]=evazion Caused by the `search` method doing a left join instead of a subquery when filtering by a belongs to association.
This commit is contained in:
@@ -643,7 +643,15 @@ module Searchable
|
|||||||
end
|
end
|
||||||
|
|
||||||
if parameter_hash?(params[attr])
|
if parameter_hash?(params[attr])
|
||||||
relation = visible(relation, attr).includes(attr).references(attr).where(attr => model.visible(current_user).search(params[attr], current_user).reorder(nil))
|
if association.belongs_to?
|
||||||
|
foreign_key = association.foreign_key
|
||||||
|
primary_key = association.association_primary_key
|
||||||
|
else
|
||||||
|
foreign_key = association.association_primary_key
|
||||||
|
primary_key = association.foreign_key
|
||||||
|
end
|
||||||
|
|
||||||
|
relation = visible(relation, attr).where(foreign_key => model.visible(current_user).search(params[attr], current_user).reorder(nil).select(primary_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
relation
|
relation
|
||||||
|
|||||||
37
test/functional/reports_controller_test.rb
Normal file
37
test/functional/reports_controller_test.rb
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ReportsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
context "The reports controller" do
|
||||||
|
context "show action" do
|
||||||
|
context "posts report" do
|
||||||
|
setup do
|
||||||
|
@post = create(:post)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work" do
|
||||||
|
get report_path("posts")
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work with the period param" do
|
||||||
|
get report_path("posts", search: { period: "month" })
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work with the group param" do
|
||||||
|
get report_path("posts", search: { group: "uploader" })
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work when filtering by a nested association" do
|
||||||
|
get report_path("posts", search: { uploader: { name: @post.uploader.name }})
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user