From d80d8b1ecc1e37a6c0693e67063924c3df00175c Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Tue, 21 Mar 2017 08:49:02 +0100 Subject: [PATCH] adapt list custom field filter to alterations caused by multi select --- .../filter/custom_field_filter.rb | 28 ++++++++++++++++- .../query_filter_instance_representer.rb | 2 +- ...ng_object_filter_dependency_representer.rb | 2 +- ...ject_filter_dependency_representer_spec.rb | 2 +- .../filter/custom_field_filter_spec.rb | 31 ++++++++++++++++++- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app/models/queries/work_packages/filter/custom_field_filter.rb b/app/models/queries/work_packages/filter/custom_field_filter.rb index 9cda0e59bf..e75dfe3b35 100644 --- a/app/models/queries/work_packages/filter/custom_field_filter.rb +++ b/app/models/queries/work_packages/filter/custom_field_filter.rb @@ -111,7 +111,7 @@ class Queries::WorkPackages::Filter::CustomFieldFilter < end def ar_object_filter? - %w{user version}.include? custom_field.field_format + %w{user version list}.include? custom_field.field_format end def value_objects @@ -120,6 +120,8 @@ class Queries::WorkPackages::Filter::CustomFieldFilter < User.find(values) when 'version' Version.find(values) + when 'list' + value_objects_for_list else super end @@ -153,4 +155,28 @@ class Queries::WorkPackages::Filter::CustomFieldFilter < !self.class.custom_fields(context) .map(&:id).include? custom_field.id end + + def value_objects_for_list + objects = allowed_values.select do |value| + values.include? value.last.to_s + end + + objects.map do |value| + Queries::StringObject.new(value.last, value.first) + end + end +end + +# +# This object is only used to transport the values tothe query filter instance +# representer which expects a class and deduces the path from the classes' name. +# +class Queries::StringObject + attr_accessor :id, + :name + + def initialize(id, name) + self.id = [name, id] + self.name = name + end end diff --git a/lib/api/v3/queries/filters/query_filter_instance_representer.rb b/lib/api/v3/queries/filters/query_filter_instance_representer.rb index d66c16162d..7256c3d43c 100644 --- a/lib/api/v3/queries/filters/query_filter_instance_representer.rb +++ b/lib/api/v3/queries/filters/query_filter_instance_representer.rb @@ -55,7 +55,7 @@ module API represented.value_objects.map do |value_object| { - href: api_v3_paths.send(value_object.class.name.underscore, value_object.id), + href: api_v3_paths.send(value_object.class.name.demodulize.underscore, value_object.id), title: value_object.name } end diff --git a/lib/api/v3/queries/schemas/string_object_filter_dependency_representer.rb b/lib/api/v3/queries/schemas/string_object_filter_dependency_representer.rb index 23a21feb67..eb6893693e 100644 --- a/lib/api/v3/queries/schemas/string_object_filter_dependency_representer.rb +++ b/lib/api/v3/queries/schemas/string_object_filter_dependency_representer.rb @@ -41,7 +41,7 @@ module API required: true, visibility: false, values_callback: ->(*) { - represented.allowed_values.map(&:last) + represented.allowed_values }, value_representer: StringObjects::StringObjectRepresenter, link_factory: ->(value) { diff --git a/spec/lib/api/v3/queries/schemas/string_object_filter_dependency_representer_spec.rb b/spec/lib/api/v3/queries/schemas/string_object_filter_dependency_representer_spec.rb index ebd316a86a..98fc862841 100644 --- a/spec/lib/api/v3/queries/schemas/string_object_filter_dependency_representer_spec.rb +++ b/spec/lib/api/v3/queries/schemas/string_object_filter_dependency_representer_spec.rb @@ -55,7 +55,7 @@ describe ::API::V3::Queries::Schemas::StringObjectFilterDependencyRepresenter do let(:type) { '[]StringObject' } let(:hrefs) do filter.allowed_values.each_with_object([]) do |value, array| - array << api_v3_paths.string_object(value.last) + array << api_v3_paths.string_object(value) end end diff --git a/spec/models/queries/work_packages/filter/custom_field_filter_spec.rb b/spec/models/queries/work_packages/filter/custom_field_filter_spec.rb index fccd7620ef..10e823a711 100644 --- a/spec/models/queries/work_packages/filter/custom_field_filter_spec.rb +++ b/spec/models/queries/work_packages/filter/custom_field_filter_spec.rb @@ -398,7 +398,36 @@ describe Queries::WorkPackages::Filter::CustomFieldFilter, type: :model do context 'list cf' do let(:custom_field) { list_wp_custom_field } - it_behaves_like 'non ar filter' + describe '#ar_object_filter?' do + it 'is true' do + expect(instance) + .to be_ar_object_filter + end + end + + describe '#value_objects' do + before do + instance.values = [custom_field.custom_options.last.id, + custom_field.custom_options.first.id] + end + + it 'returns an array with custom classes' do + expect(instance.value_objects.length) + .to eql(2) + + expect(instance.value_objects[0].id) + .to match_array([custom_field.custom_options.first.value, + custom_field.custom_options.first.id]) + expect(instance.value_objects[0].name) + .to eql(custom_field.custom_options.first.value) + + expect(instance.value_objects[1].id) + .to match_array([custom_field.custom_options.last.value, + custom_field.custom_options.last.id]) + expect(instance.value_objects[1].name) + .to eql(custom_field.custom_options.last.value) + end + end end context 'bool cf' do