Skip to content

[#64613] Create update form configuration contract #19261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add attribute groups params transformer
  • Loading branch information
shiroginne committed Jun 20, 2025
commit 58d4600b85cbea886e4123b5e3faac7f85c90218
4 changes: 2 additions & 2 deletions app/services/base_type_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def set_active_custom_fields
type.custom_field_ids = type
.attribute_groups
.flat_map(&:members)
.select { CustomField.custom_field_attribute? _1 }
.map { _1.gsub(/^custom_field_/, "").to_i }
.select { CustomField.custom_field_attribute? it }
.map { it.gsub(/^custom_field_/, "").to_i }
.uniq
end

Expand Down
80 changes: 80 additions & 0 deletions app/services/work_package_types/attribute_groups/transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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 WorkPackageTypes
module AttributeGroups
class Transformer
def initialize(groups:, user:)
@groups = groups
@user = user
end

def call
return [] if groups.blank?

groups.map do |group|
if group["type"] == "query"
transform_query_group(group)
else
transform_attribute_group(group)
end
end
end

private

attr_reader :groups, :user

def transform_attribute_group(group)
name = group["key"]&.to_sym || group["name"]
attributes = group["attributes"].pluck("key")

[name, attributes]
end

def transform_query_group(group)
name = group["name"]
props = JSON.parse(group["query"])

query = Query.new_default(name: "Embedded table: #{name}")
query.extend(OpenProject::ChangedBySystem)
query.change_by_system { query.user = User.system }

::API::V3::UpdateQueryFromV3ParamsService
.new(query, user)
.call(props.with_indifferent_access)

query.show_hierarchies = false

[name, [query]]
end
end
end
end
126 changes: 126 additions & 0 deletions spec/services/work_package_types/attribute_groups/transformer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe WorkPackageTypes::AttributeGroups::Transformer do
subject(:transformer) { described_class.new(groups: raw_groups, user: user) }

let(:user) { double("User") }

Check notice on line 8 in spec/services/work_package_types/attribute_groups/transformer_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/services/work_package_types/attribute_groups/transformer_spec.rb#L8 <RSpec/VerifiedDoubles>

Prefer using verifying doubles over normal doubles.
Raw output
spec/services/work_package_types/attribute_groups/transformer_spec.rb:8:16: C: RSpec/VerifiedDoubles: Prefer using verifying doubles over normal doubles.

describe "#call" do
context "when groups are empty" do
let(:raw_groups) { [] }

it "returns an empty array" do
expect(transformer.call).to eq([])
end
end

context "when given a regular attribute group with key" do
let(:raw_groups) do
[
{
"name" => "Custom",
"key" => "custom",
"type" => "attribute",
"attributes" => [
{ "key" => "custom_field_1" },
{ "key" => "custom_field_2" }
]
}
]
end

it "returns transformed group with symbolized key and attribute keys" do
expect(transformer.call).to eq([
[:custom, ["custom_field_1", "custom_field_2"]]
])
end
end

context "when group has no key" do
let(:raw_groups) do
[
{
"name" => "General Info",
"type" => "attribute",
"attributes" => [
{ "key" => "subject" }
]
}
]
end

it "uses name as group name" do
expect(transformer.call).to eq([
["General Info", ["subject"]]
])
end
end

context "when given a query group with valid JSON" do
let(:query_json) do
{
"_links" => {
"columns" => [{ "href" => "/api/v3/queries/columns/id" }]
},
"filters" => [],
"groupBy" => nil,
"sortBy" => [],
"name" => "Some query"
}.to_json
end

let(:raw_groups) do
[
{
"name" => "Embedded Table",
"type" => "query",
"query" => query_json
}
]
end

let(:query_instance) { double("Query") }

Check notice on line 84 in spec/services/work_package_types/attribute_groups/transformer_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/services/work_package_types/attribute_groups/transformer_spec.rb#L84 <RSpec/VerifiedDoubles>

Prefer using verifying doubles over normal doubles.
Raw output
spec/services/work_package_types/attribute_groups/transformer_spec.rb:84:30: C: RSpec/VerifiedDoubles: Prefer using verifying doubles over normal doubles.

before do
allow(User).to receive(:system).and_return(double("SystemUser"))

Check notice on line 87 in spec/services/work_package_types/attribute_groups/transformer_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/services/work_package_types/attribute_groups/transformer_spec.rb#L87 <RSpec/VerifiedDoubles>

Prefer using verifying doubles over normal doubles.
Raw output
spec/services/work_package_types/attribute_groups/transformer_spec.rb:87:52: C: RSpec/VerifiedDoubles: Prefer using verifying doubles over normal doubles.
allow(Query).to receive(:new_default).and_return(query_instance)

allow(query_instance).to receive(:extend)
allow(query_instance).to receive(:change_by_system).and_yield
allow(query_instance).to receive(:user=)
allow(query_instance).to receive(:show_hierarchies=)

allow(API::V3::UpdateQueryFromV3ParamsService).to receive(:new)
.and_return(
double("UpdateQueryService", call: double("ServiceResult", success?: true))

Check notice on line 97 in spec/services/work_package_types/attribute_groups/transformer_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/services/work_package_types/attribute_groups/transformer_spec.rb#L97 <RSpec/VerifiedDoubles>

Prefer using verifying doubles over normal doubles.
Raw output
spec/services/work_package_types/attribute_groups/transformer_spec.rb:97:13: C: RSpec/VerifiedDoubles: Prefer using verifying doubles over normal doubles.

Check notice on line 97 in spec/services/work_package_types/attribute_groups/transformer_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/services/work_package_types/attribute_groups/transformer_spec.rb#L97 <RSpec/VerifiedDoubles>

Prefer using verifying doubles over normal doubles.
Raw output
spec/services/work_package_types/attribute_groups/transformer_spec.rb:97:48: C: RSpec/VerifiedDoubles: Prefer using verifying doubles over normal doubles.
)
end

it "returns a group with a Query instance" do
result = transformer.call
name, entries = result.first

expect(name).to eq("Embedded Table")
expect(entries.first).to eq(query_instance)
end
end

context "when given a query group with invalid JSON" do
let(:raw_groups) do
[
{
"name" => "Broken",
"type" => "query",
"query" => "not a json"
}
]
end

it "raises JSON::ParserError" do
expect { transformer.call }.to raise_error(JSON::ParserError)
end
end
end
end
Loading