Skip to content

Commit

Permalink
Refactor filter allowed_values
Browse files Browse the repository at this point in the history
  • Loading branch information
dombesz committed Feb 24, 2025
1 parent fafcb12 commit 569cd16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
14 changes: 7 additions & 7 deletions app/components/filter/filter_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ def additional_filter_attributes(filter)

def custom_field_list_autocomplete_options(filter)
options = if filter.custom_field.version?
{
items: filter.allowed_values.map { |name, id, project_name| { name:, id:, project_name: } },
groupBy: "project_name"
}
else
{ items: filter.allowed_values.map { |name, id| { name:, id: } } }
end
{
items: filter.allowed_values.map { |name, id, project_name| { name:, id:, project_name: } },
groupBy: "project_name"
}
else
{ items: filter.allowed_values.map { |name, id| { name:, id: } } }
end

autocomplete_options.merge(options).merge(model: filter.values)
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/queries/filters/strategies/boolean_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
Expand Down Expand Up @@ -37,7 +39,7 @@ def validate
end

def valid_values!
filter.values &= allowed_values.map { |v| v.last.to_s }
filter.values &= allowed_values.map { |_, v| v.to_s }
end

private
Expand Down
13 changes: 8 additions & 5 deletions app/models/queries/filters/strategies/list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
Expand Down Expand Up @@ -28,9 +30,6 @@

module Queries::Filters::Strategies
class List < BaseStrategy
delegate :allowed_values,
to: :filter

self.supported_operators = ["=", "!"]
self.default_operator = "="

Expand All @@ -51,12 +50,16 @@ def validate
end
end

def allowed_values
filter.allowed_values.map { |_, v| v.to_s }
end

def valid_values!
filter.values &= (allowed_values.map(&:second).map(&:to_s) + ["-1"])
filter.values &= (allowed_values + ["-1"])
end

def non_valid_values?
(values.reject(&:blank?) & (allowed_values.map(&:second).map(&:to_s) + ["-1"])) != values.reject(&:blank?)
(values.compact_blank & (allowed_values + ["-1"])) != values.compact_blank
end
end
end
4 changes: 3 additions & 1 deletion app/models/queries/filters/strategies/relation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
Expand Down Expand Up @@ -48,7 +50,7 @@ def validate
end

def valid_values!
filter.values &= allowed_values.map { |v| v.last.to_s }
filter.values &= allowed_values.map { |_, v| v.to_s }
end

private
Expand Down

0 comments on commit 569cd16

Please sign in to comment.