/ip_addresses: allow searching multiple IPs at once.

Fixes #4208. Allow searching for multiple IPs or user IDs. Separate
using spaces or commas.
This commit is contained in:
evazion
2019-11-11 20:11:36 -06:00
parent 68e1140b0d
commit 73c02021a1
4 changed files with 17 additions and 4 deletions

View File

@@ -51,8 +51,13 @@ class ApplicationRecord < ActiveRecord::Base
end
def where_inet_matches(attr, value)
ip = IPAddress.parse(value)
where("#{qualified_column_for(attr)} <<= ?", ip.to_string)
if value.match?(/[, ]/)
ips = value.split(/[, ]+/).map { |ip| IPAddress.parse(ip).to_string }
where("#{qualified_column_for(attr)} = ANY(ARRAY[?]::inet[])", ips)
else
ip = IPAddress.parse(value)
where("#{qualified_column_for(attr)} <<= ?", ip.to_string)
end
end
def where_array_includes_any(attr, values)

View File

@@ -371,8 +371,8 @@ class Tag < ApplicationRecord
when /\A>(.+)/
return [:gt, parse_cast($1, type)]
when /,/
return [:in, range.split(/,/).map {|x| parse_cast(x, type)}]
when /[, ]/
return [:in, range.split(/[, ]+/).map {|x| parse_cast(x, type)}]
else
return [:eq, parse_cast(range, type)]

View File

@@ -1,3 +1,7 @@
<p>
<%= link_to "Find all users associated with the top 10 IP addresses", ip_addresses_path(search: { ip_addr: @ip_addresses.map(&:to_s).take(10).join(" "), group_by: "user" }) %>
</p>
<table class="striped autofit">
<thead>
<tr>

View File

@@ -1,3 +1,7 @@
<p>
<%= link_to "Find all IP addresses associated with these users", ip_addresses_path(search: { user_id: @ip_addresses.map(&:user_id).join(" "), group_by: "ip_addr" }) %>
</p>
<table class="striped autofit">
<thead>
<tr>