From 6148cb39a23f2b9a4eedec402a21cff80f37ae41 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 17 Oct 2018 16:11:44 -0500 Subject: [PATCH] Fix nested blank search params not being stripped from searches. From https://danbooru.donmai.us/forum_topics/9127?page=258#forum_post_151308: When you do a user search (from https://danbooru.donmai.us/users/search) the results only include those with someone in the inviter field. The bug was that nested blank search params (/users?search[inviter][name_matches]=) didn't get stripped from the search. --- app/controllers/application_controller.rb | 18 ++++++++++-------- test/functional/users_controller_test.rb | 5 +++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 31b2fbb0e..163565ab8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -199,15 +199,17 @@ class ApplicationController < ActionController::Base # /tags?search[name]=touhou&search[category]=&search[order]= # => /tags?search[name]=touhou def normalize_search - if request.get? - if params[:search].blank? - params[:search] = ActionController::Parameters.new - end + return unless request.get? + params[:search] ||= ActionController::Parameters.new - if params[:search].is_a?(ActionController::Parameters) && params[:search].values.any?(&:blank?) - params[:search].reject! {|k,v| v.blank?} - redirect_to url_for(params: params.except(:controller, :action, :index).permit!) - end + deep_reject_blank = lambda do |hash| + hash.reject { |k, v| v.blank? || (v.is_a?(Hash) && deep_reject_blank.call(v).blank?) } + end + nonblank_search_params = deep_reject_blank.call(params[:search]) + + if nonblank_search_params != params[:search] + params[:search] = nonblank_search_params + redirect_to url_for(params: params.except(:controller, :action, :index).permit!) end end diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 7f3ccd426..9843ddd8f 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -26,6 +26,11 @@ class UsersControllerTest < ActionDispatch::IntegrationTest get users_path, params: {:search => {:name_matches => @user.name}} assert_response :success end + + should "list all users (with blank search parameters)" do + get users_path, params: { search: { inviter: { name_matches: "" }, level: "", name: "test" } } + assert_redirected_to users_path(search: { name: "test" }) + end end context "show action" do