diff --git a/app/logical/concerns/searchable.rb b/app/logical/concerns/searchable.rb index 640692263..37813e40f 100644 --- a/app/logical/concerns/searchable.rb +++ b/app/logical/concerns/searchable.rb @@ -149,7 +149,10 @@ module Searchable type = column.type || reflect_on_association(name)&.class_name if column.try(:array?) - return search_array_attribute(name, type, params) + subtype = type + type = :array + elsif defined_enums.has_key?(name.to_s) + type = :enum end case type @@ -165,6 +168,10 @@ module Searchable numeric_attribute_matches(name, params[name]) when :inet search_inet_attribute(name, params) + when :enum + search_enum_attribute(name, params) + when :array + search_array_attribute(name, subtype, params) else raise NotImplementedError, "unhandled attribute type: #{name}" end @@ -232,6 +239,19 @@ module Searchable relation end + def search_enum_attribute(name, params) + relation = all + + if params[name].present? + value = params[name].split(/[, ]+/).map(&:downcase) + relation = relation.where(name => value) + elsif params["#{name}_id"].present? + relation = relation.numeric_attribute_matches(name, params["#{name}_id"]) + end + + relation + end + def search_array_attribute(name, type, params) relation = all diff --git a/test/functional/mod_actions_controller_test.rb b/test/functional/mod_actions_controller_test.rb index 8d5466a45..9052cdf36 100644 --- a/test/functional/mod_actions_controller_test.rb +++ b/test/functional/mod_actions_controller_test.rb @@ -2,19 +2,33 @@ require 'test_helper' class ModActionsControllerTest < ActionDispatch::IntegrationTest context "The mod actions controller" do - setup do - @mod_action = create(:mod_action) - end - context "index action" do + setup do + @ban = create(:mod_action, category: :post_ban) + @unban = create(:mod_action, category: :post_unban) + end + should "work" do get mod_actions_path assert_response :success end + + context "category enum searches" do + should respond_to_search(category: "post_ban").with { [@ban] } + should respond_to_search(category: "post_unban").with { [@unban] } + should respond_to_search(category: "Post_ban").with { [@ban] } + should respond_to_search(category: "post_ban post_unban").with { [@unban, @ban] } + should respond_to_search(category: "post_ban,post_unban").with { [@unban, @ban] } + should respond_to_search(category: "44").with { [@ban] } + should respond_to_search(category_id: "44").with { [@ban] } + should respond_to_search(category_id: "44,45").with { [@unban, @ban] } + should respond_to_search(category_id: ">=44").with { [@unban, @ban] } + end end context "show action" do should "work" do + @mod_action = create(:mod_action) get mod_action_path(@mod_action) assert_redirected_to mod_actions_path(search: { id: @mod_action.id }) end