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 6, 2024
1 parent 7b14f55 commit baf4134
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
17 changes: 16 additions & 1 deletion app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,22 @@ def enforce_inherited_taxonomies
def inherit_taxonomies!
self.organization_ids = role.organization_ids if resource_taxable_by_organization?
self.location_ids = role.location_ids if resource_taxable_by_location?
build_taxonomy_search
build_taxonomy_search_from_ids
end

def build_taxonomy_search_from_ids
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_from_ids(name)
return '' unless send("resource_taxable_by_#{name}?")
relation = send("#{name}_ids")
return '' if relation.empty?
parenthesize("#{name}_id ^ (#{relation.join(',')})")
end

private
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_from_ids
filter.update_column(:taxonomy_search, filter.taxonomy_search)
end
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 baf4134

Please sign in to comment.