searchable: add more enum attribute search options.
Add `<enum>_not` and `<enum>_id_<op>` search options: * https://danbooru.donmai.us/mod_actions?search[category_not]=post_regenerate,post_regenerate_iqdb * https://danbooru.donmai.us/mod_actions?search[category_not]=48,49 * https://danbooru.donmai.us/mod_actions?search[category_id]=40..50 * https://danbooru.donmai.us/mod_actions?search[category_id_not]=40..50 * https://danbooru.donmai.us/mod_actions?search[category_id_gt]=40&search[category_id_lt]=50
This commit is contained in:
@@ -213,7 +213,7 @@ module Searchable
|
|||||||
when :boolean
|
when :boolean
|
||||||
search_boolean_attribute(name, params)
|
search_boolean_attribute(name, params)
|
||||||
when :integer, :float, :datetime
|
when :integer, :float, :datetime
|
||||||
search_numeric_attribute(name, params)
|
search_numeric_attribute(name, params, type: type)
|
||||||
when :inet
|
when :inet
|
||||||
search_inet_attribute(name, params)
|
search_inet_attribute(name, params)
|
||||||
when :enum
|
when :enum
|
||||||
@@ -225,39 +225,39 @@ module Searchable
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_numeric_attribute(attr, params)
|
def search_numeric_attribute(attr, params, key: attr, type: :integer)
|
||||||
relation = all
|
relation = all
|
||||||
|
|
||||||
if params[attr].present?
|
if params[key].present?
|
||||||
relation = relation.numeric_attribute_matches(attr, params[attr])
|
relation = relation.where_numeric_matches(attr, params[key], type)
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_not"].present?
|
if params[:"#{key}_not"].present?
|
||||||
relation = relation.where.not(id: numeric_attribute_matches(attr, params[:"#{attr}_not"]))
|
relation = relation.where.not(id: relation.where_numeric_matches(attr, params[:"#{key}_not"], type))
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_eq"].present?
|
if params[:"#{key}_eq"].present?
|
||||||
relation = relation.where_operator(attr, :eq, params[:"#{attr}_eq"])
|
relation = relation.where_operator(attr, :eq, params[:"#{key}_eq"])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_not_eq"].present?
|
if params[:"#{key}_not_eq"].present?
|
||||||
relation = relation.where_operator(attr, :not_eq, params[:"#{attr}_not_eq"])
|
relation = relation.where_operator(attr, :not_eq, params[:"#{key}_not_eq"])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_gt"].present?
|
if params[:"#{key}_gt"].present?
|
||||||
relation = relation.where_operator(attr, :gt, params[:"#{attr}_gt"])
|
relation = relation.where_operator(attr, :gt, params[:"#{key}_gt"])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_gteq"].present?
|
if params[:"#{key}_gteq"].present?
|
||||||
relation = relation.where_operator(attr, :gteq, params[:"#{attr}_gteq"])
|
relation = relation.where_operator(attr, :gteq, params[:"#{key}_gteq"])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_lt"].present?
|
if params[:"#{key}_lt"].present?
|
||||||
relation = relation.where_operator(attr, :lt, params[:"#{attr}_lt"])
|
relation = relation.where_operator(attr, :lt, params[:"#{key}_lt"])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:"#{attr}_lteq"].present?
|
if params[:"#{key}_lteq"].present?
|
||||||
relation = relation.where_operator(attr, :lteq, params[:"#{attr}_lteq"])
|
relation = relation.where_operator(attr, :lteq, params[:"#{key}_lteq"])
|
||||||
end
|
end
|
||||||
|
|
||||||
relation
|
relation
|
||||||
@@ -396,13 +396,12 @@ module Searchable
|
|||||||
relation = relation.where(name => value)
|
relation = relation.where(name => value)
|
||||||
end
|
end
|
||||||
|
|
||||||
if params["#{name}_id"].present?
|
if params[:"#{name}_not"].present?
|
||||||
relation = relation.numeric_attribute_matches(name, params["#{name}_id"])
|
value = params[:"#{name}_not"].split(/[, ]+/).map(&:downcase)
|
||||||
|
relation = relation.where.not(name => value)
|
||||||
end
|
end
|
||||||
|
|
||||||
if params["#{name}_id_not"].present?
|
relation = relation.search_numeric_attribute(name, params, key: :"#{name}_id")
|
||||||
relation = relation.where.not(id: relation.numeric_attribute_matches(name, params["#{name}_id_not"]))
|
|
||||||
end
|
|
||||||
|
|
||||||
relation
|
relation
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -123,7 +123,19 @@ class SearchableTest < ActiveSupport::TestCase
|
|||||||
assert_search_equals(@pf1, status: "pending")
|
assert_search_equals(@pf1, status: "pending")
|
||||||
assert_search_equals(@pf1, status: "pending,blah")
|
assert_search_equals(@pf1, status: "pending,blah")
|
||||||
assert_search_equals(@pf1, status: "pending blah")
|
assert_search_equals(@pf1, status: "pending blah")
|
||||||
assert_search_equals(@pf1, status_id: PostFlag.statuses[:pending])
|
|
||||||
|
assert_search_equals(@pf2, status_not: "pending")
|
||||||
|
assert_search_equals([], status_not: "pending,rejected")
|
||||||
|
|
||||||
|
assert_search_equals(@pf1, status_id: "0")
|
||||||
|
assert_search_equals(@pf1, status_id_eq: "0")
|
||||||
|
assert_search_equals([@pf2, @pf1], status_id: "0 2")
|
||||||
|
assert_search_equals([@pf2, @pf1], status_id: "0,2")
|
||||||
|
assert_search_equals([@pf2, @pf1], status_id: "0..2")
|
||||||
|
assert_search_equals([@pf2], status_id: ">0")
|
||||||
|
|
||||||
|
assert_search_equals(@pf2, status_id_not: "0")
|
||||||
|
assert_search_equals(@pf2, status_id_not_eq: "0")
|
||||||
end
|
end
|
||||||
|
|
||||||
should "support multiple operators on the same attribute" do
|
should "support multiple operators on the same attribute" do
|
||||||
|
|||||||
Reference in New Issue
Block a user