Skip to content

Commit

Permalink
Fixes #37987 - Filters dont inherit taxonomy on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Nov 20, 2024
1 parent 7b14f55 commit 1579997
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
12 changes: 6 additions & 6 deletions app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,24 @@ def inherit_taxonomies!
build_taxonomy_search
end

private

def build_taxonomy_search
orgs = build_taxonomy_search_string('organization')
locs = build_taxonomy_search_string('location')
orgs = build_taxonomy_search_string_from_ids('organization')
locs = build_taxonomy_search_string_from_ids('location')

taxonomies = [orgs, locs].reject { |t| t.blank? }
self.taxonomy_search = taxonomies.join(' and ').presence
end

def build_taxonomy_search_string(name)
def build_taxonomy_search_string_from_ids(name)
return '' unless send("resource_taxable_by_#{name}?")
relation = send(name.pluralize).pluck(:id)
relation = send("#{name}_ids")
return '' if relation.empty?

parenthesize("#{name}_id ^ (#{relation.join(',')})")
end

private

def nilify_empty_searches
self.search = nil if search.empty? || unlimited == '1'
self.taxonomy_search = nil if taxonomy_search.empty?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class RegenerateTaxonomySearchForFiltersWithOverrideFalse < ActiveRecord::Migration[6.1]
def change
filters = Filter.where(role_id: Role.where(origin: nil).or(Role.where(builtin: 2))).where(override: false).where(taxonomy_search: nil)

filters.each do |filter|
filter.build_taxonomy_search
filter.update_column(:taxonomy_search, filter.taxonomy_search)
end
end
end
11 changes: 10 additions & 1 deletion test/models/filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,17 @@ class FilterTest < ActiveSupport::TestCase

test 'saving nilifies empty taxonomy search' do
f = FactoryBot.build(:filter, :resource_type => 'Domain')
f.role = FactoryBot.build(:role, :organizations => [FactoryBot.build(:organization)])
f.role = FactoryBot.build(:role)
f.save
assert_nil f.taxonomy_search
end

test 'creating a filter inherits taxonomies from the role' do
f = FactoryBot.create(:filter, :resource_type => 'Domain')
organization_test = FactoryBot.create(:organization)
location_test = FactoryBot.create(:location)
f.role = FactoryBot.create(:role, :organizations => [organization_test], :locations => [location_test])
f.save
assert_equal f.taxonomy_search, "(organization_id ^ (#{organization_test.id})) and (location_id ^ (#{location_test.id}))"
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect, useMemo } from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import {
Expand Down Expand Up @@ -39,14 +39,26 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
show_organizations: showOrgs = false,
show_locations: showLocations = false,
} = type;

const isTaxonomySearch = useMemo(
() => chosenOrgs.length || chosenLocations.length,
[chosenOrgs.length, chosenLocations.length]
);

useEffect(() => {
if (isTaxonomySearch) {
setIsUnlimited(false);
}
}, [isTaxonomySearch]);
const dispatch = useDispatch();
const [autocompleteQuery, setAutocompleteQuery] = useState(data.search || '');
const submit = async () => {
const params = {
filter: {
role_id: role,
search: isUnlimited ? null : autocompleteQuery,
unlimited: isUnlimited,
unlimited:
isUnlimited || (!autocompleteQuery.length && !isTaxonomySearch),
override: isOverride,
permission_ids: chosenPermissions,
organization_ids: chosenOrgs,
Expand Down Expand Up @@ -163,8 +175,20 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
defaultLocations={data.locations}
/>
)}

{isGranular ? (
<>
{!!isTaxonomySearch && (
<>
<Alert
ouiaId="taxonomy-search-alert"
variant="info"
title={__(
"The filter is scoped to the selected organizations and locations, therefore can't be unlimited"
)}
/>
</>
)}
<FormGroup
label={__('Unlimited?')}
labelIcon={
Expand All @@ -190,11 +214,12 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
>
<Checkbox
ouiaId="unlimited-checkbox"
isChecked={isUnlimited}
isChecked={isTaxonomySearch ? isUnlimited : false}
onChange={checked => {
setAutocompleteQuery('');
setIsUnlimited(checked);
}}
isDisabled={isTaxonomySearch}
aria-label="is unlimited"
id="unlimited-check"
name="unlimited"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FiltersForm } from './FiltersForm';
const NewFiltersFormPage = ({ location: { search }, history }) => {
const { role_id: urlRoleId } = URI.parseQuery(search);
const {
response: { name: roleName, id: roleId },
response: { name: roleName, id: roleId, locations, organizations },
} = useAPI('get', `/api/v2/roles/${urlRoleId}`);
const breadcrumbOptions = {
breadcrumbItems: [
Expand All @@ -29,7 +29,7 @@ const NewFiltersFormPage = ({ location: { search }, history }) => {
isNew
roleName={roleName || ''}
roleId={urlRoleId}
data={{}}
data={{ locations, organizations }}
history={history}
/>
) : (
Expand Down

0 comments on commit 1579997

Please sign in to comment.