Fix #4893: Add a FIELD_present parameter variation for text fields.

Usage:

* https://danbooru.donmai.us/wiki_pages.json?search[body_present]=true
* https://danbooru.donmai.us/wiki_pages.json?search[body_present]=false
This commit is contained in:
evazion
2021-10-13 04:10:23 -05:00
parent d771f6f41b
commit c0f744f84d
2 changed files with 11 additions and 0 deletions

View File

@@ -307,6 +307,14 @@ module Searchable
relation = relation.where(attr => params[attr])
end
if params[:"#{attr}_present"].present? && params[:"#{attr}_present"].truthy?
relation = relation.where.not(attr => "")
end
if params[:"#{attr}_present"].present? && params[:"#{attr}_present"].falsy?
relation = relation.where(attr => "")
end
if params[:"#{attr}_eq"].present?
relation = relation.where(attr => params[:"#{attr}_eq"])
end

View File

@@ -82,6 +82,9 @@ class SearchableTest < ActiveSupport::TestCase
assert_search_equals([@p3, @p2], source_not_like: "a*")
assert_search_equals([@p3, @p2], source_not_ilike: "A*")
assert_search_equals([@p3, @p2], source_not_regex: "^a.*")
assert_search_equals([], source_present: "false")
assert_search_equals([@p3, @p2, @p1], source_present: "true")
end
should "support multiple operators on the same attribute" do