Merge pull request #6659 from opf/fix/28475/value-objects-of-subprojects

[28475] Fix usage of value objects in subproject filter

[ci skip]
pull/6660/head
Oliver Günther 6 years ago committed by GitHub
commit 543b0beffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      app/models/queries/work_packages/filter/subproject_filter.rb
  2. 2
      spec/features/work_packages/table/delete_work_packages.spec.rb
  3. 39
      spec/lib/api/v3/queries/filters/query_filter_instance_representer_spec.rb
  4. 15
      spec/models/queries/work_packages/filter/subproject_filter_spec.rb

@ -32,7 +32,7 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
Queries::WorkPackages::Filter::WorkPackageFilter
def allowed_values
@allowed_values ||= begin
visible_subprojects.map { |id, name| [name, id.to_s] }
visible_subproject_array.map { |id, name| [name, id.to_s] }
end
end
@ -71,7 +71,7 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
def value_objects
value_ints = values.map(&:to_i)
visible_subprojects.select { |id,_| value_ints.include?(id) }
visible_subprojects.where(id: value_ints)
end
def where
@ -82,7 +82,7 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
# include the selected subprojects
ids += values.each(&:to_i)
when '*'
ids += visible_subprojects.map(&:first)
ids += visible_subproject_array.map(&:first)
end
"#{Project.table_name}.id IN (%s)" % ids.join(',')
@ -90,13 +90,17 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
private
def visible_subproject_array
visible_subprojects.pluck(:id, :name)
end
def visible_subprojects
# This can be accessed even when `available?` is false
@visible_subprojects ||= begin
if project.nil?
[]
else
project.descendants.visible.pluck(:id, :name)
project.descendants.visible
end
end
end

@ -72,7 +72,7 @@ describe 'Delete work package', js: true do
wp_table.visit!
wp_table.expect_work_package_listed wp1, wp2, wp_child
wp_table.table_container.send_keys [:control, 'a']
page.find('.work-package-table').send_keys [:control, 'a']
context_menu.open_for(wp1)
context_menu.choose('Bulk delete')

@ -141,6 +141,45 @@ describe ::API::V3::Queries::Filters::QueryFilterInstanceRepresenter do
end
end
context 'with a subproject filter value_objects' do
let(:user) { FactoryBot.create :admin }
let(:project) { FactoryBot.create :project }
let(:subproject) { FactoryBot.create :project, parent: project }
let(:filter) do
subproject
project.reload
f = Queries::WorkPackages::Filter::SubprojectFilter.create!(context: project)
f.operator = '='
f.values = [subproject.id]
f
end
before do
login_as user
allow(filter)
.to receive(:value_objects)
.and_call_original
end
it "has a 'values' collection" do
expect(filter.value_objects.count).to eq 1
expect(filter.value_objects.first).to eq subproject
expected = {
href: api_v3_paths.project(subproject.id.to_s),
title: subproject.name
}
is_expected
.to be_json_eql([expected].to_json)
.at_path('_links/values')
end
end
context 'with a non ar object filter' do
let(:values) { ['lorem ipsum'] }
let(:filter) do

@ -123,20 +123,5 @@ describe Queries::WorkPackages::Filter::SubprojectFilter, type: :model do
.to be_ar_object_filter
end
end
describe '#value_objects' do
let(:subproject1) { FactoryBot.build_stubbed(:project) }
let(:subproject2) { FactoryBot.build_stubbed(:project) }
let(:projects) { [subproject1, subproject2] }
before do
instance.values = [subproject1.id.to_s, subproject2.id.to_s]
end
it 'returns an array of projects' do
expect(instance.value_objects)
.to match_array(plucked)
end
end
end
end

Loading…
Cancel
Save