-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15400 from opf/task/54459-investigate-queries-and…
…-filters-to-display-the-project-list-in-projects-attribute-settings-view [#54459] Add `AvailableProjectAttributesFilter` to project queries to display the mapped projects list in projects attribute settings view https://community.openproject.org/work_packages/54459
- Loading branch information
Showing
9 changed files
with
330 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2010-2023 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# ++ | ||
|
||
module Projects::Scopes | ||
module AvailableCustomFields | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def with_available_custom_fields(custom_field_ids) | ||
subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:) | ||
where(id: subquery) | ||
end | ||
|
||
def without_available_custom_fields(custom_field_ids) | ||
subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:) | ||
where.not(id: subquery) | ||
end | ||
|
||
private | ||
|
||
def project_custom_fields_project_mapping_subquery(custom_field_ids:) | ||
ProjectCustomFieldProjectMapping.select(:project_id) | ||
.where(custom_field_id: custom_field_ids) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/models/queries/projects/filters/available_project_attributes_filter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
# -- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2023 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# ++ | ||
# | ||
|
||
class Queries::Projects::Filters::AvailableProjectAttributesFilter < Queries::Projects::Filters::ProjectFilter | ||
def self.key | ||
:available_project_attributes | ||
end | ||
|
||
def type | ||
:list | ||
end | ||
|
||
def allowed_values | ||
@allowed_values ||= ProjectCustomFieldProjectMapping | ||
.includes(:project_custom_field) | ||
.distinct | ||
.pluck(:name, :custom_field_id) | ||
end | ||
|
||
def available? | ||
User.current.admin? | ||
end | ||
|
||
def scope | ||
case operator | ||
when "=" | ||
model.with_available_custom_fields(values) | ||
when "!" | ||
model.without_available_custom_fields(values) | ||
else | ||
raise "unsupported operator" | ||
end | ||
end | ||
|
||
def human_name | ||
I18n.t(:label_available_project_attributes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
spec/models/projects/scopes/available_custom_fields_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2010-2023 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# ++ | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe Projects::Scopes::AvailableCustomFields do | ||
shared_let(:project) { create(:project) } | ||
shared_let(:project_custom_field) { create(:project_custom_field) } | ||
|
||
shared_let(:project_custom_field_mapping) do | ||
create(:project_custom_field_project_mapping, project:, project_custom_field:) | ||
end | ||
|
||
describe ".with_available_custom_fields" do | ||
it "returns projects with the given custom fields" do | ||
expect(Project.with_available_custom_fields([project_custom_field.id])).to contain_exactly(project) | ||
end | ||
end | ||
|
||
describe ".without_available_custom_fields" do | ||
it "returns projects without the given custom fields" do | ||
expect(Project.without_available_custom_fields([project_custom_field.id])).to be_empty | ||
end | ||
end | ||
end |
Oops, something went wrong.