searchable: add support for searching enum fields.

Allow searching enum fields by string, by id, or by array of
comma-separated values. The category field in modactions is an example
of an enum field that can be searched this way.
This commit is contained in:
evazion
2020-08-07 19:21:02 -05:00
parent afc07d8adf
commit 2b0cd3c90b
2 changed files with 39 additions and 5 deletions

View File

@@ -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