Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

refactoring: improve readability

Christophe Bliarda407f61b4def
diff --git a/app/models/query/results/sums.rb b/app/models/query/results/sums.rb
index 3eaf4dec7cc..9cdbf1494fc 100644
--- a/app/models/query/results/sums.rb
+++ b/app/models/query/results/sums.rb
@@ -48,7 +48,7 @@ module ::Query::Results::Sums
private
def sums_by_group_id
- sums_select(true).inject({}) do |result, group_sum|
+ sums_select(grouped: true).inject({}) do |result, group_sum|
result[group_sum["group_id"]] = {}
query.summed_up_columns.each do |column|
@@ -59,7 +59,7 @@ module ::Query::Results::Sums
end
end
- def sums_select(grouped = false)
+ def sums_select(grouped: false)
select = if grouped
["work_packages.group_id"]
else
diff --git a/app/services/api/v3/work_package_collection_from_query_service.rb b/app/services/api/v3/work_package_collection_from_query_service.rb
index 268db1162f6..2f97c957750 100644
--- a/app/services/api/v3/work_package_collection_from_query_service.rb
+++ b/app/services/api/v3/work_package_collection_from_query_service.rb
@@ -122,17 +122,19 @@ module API
end
def format_column_keys(hash_by_column)
- hash_by_column.map do |column, value|
- match = /cf_(\d+)/.match(column.name.to_s)
+ hash_by_column.transform_keys do |column|
+ column_name(column)
+ end
+ end
- column_name = if match
- "custom_field_#{match[1]}"
- else
- column.name.to_s
- end
+ def column_name(column)
+ match = /cf_(\d+)/.match(column.name.to_s)
- [column_name, value]
- end.to_h
+ if match
+ "custom_field_#{match[1]}"
+ else
+ column.name.to_s
+ end
end
def collection_representer(work_packages, params:, project:, groups:, sums:)
diff --git a/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb
index a7d775bc456..37909dca498 100644
--- a/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb
@@ -37,15 +37,16 @@ RSpec.describe API::V3::WorkPackages::WorkPackageSumsRepresenter do
end
end
let(:sums) do
- double("sums",
- story_points: 5,
- remaining_hours: 10,
- estimated_hours: 5,
- material_costs: 5,
- labor_costs: 10,
- overall_costs: 15,
- custom_field_1: 5,
- available_custom_fields: [custom_field])
+ API::ParserStruct.new(
+ story_points: 5,
+ remaining_hours: 10,
+ estimated_hours: 5,
+ material_costs: 5,
+ labor_costs: 10,
+ overall_costs: 15,
+ custom_field_1: 5,
+ available_custom_fields: [custom_field]
+ )
end
let(:current_user) { build_stubbed(:user) }
let(:representer) do
@@ -54,48 +55,48 @@ RSpec.describe API::V3::WorkPackages::WorkPackageSumsRepresenter do
subject { representer.to_json }
- context "estimated_time" do
+ describe "estimated_time" do
it "is represented" do
expected = "PT5H"
expect(subject).to be_json_eql(expected.to_json).at_path("estimatedTime")
end
end
- context "remainingTime" do
+ describe "remainingTime" do
it "is represented" do
expected = "PT10H"
expect(subject).to be_json_eql(expected.to_json).at_path("remainingTime")
end
end
- context "storyPoints" do
+ describe "storyPoints" do
it "is represented" do
expect(subject).to be_json_eql(sums.story_points.to_json).at_path("storyPoints")
end
end
- context "materialCosts" do
+ describe "materialCosts" do
it "is represented" do
expected = "5.00 EUR"
expect(subject).to be_json_eql(expected.to_json).at_path("materialCosts")
end
end
- context "laborCosts" do
+ describe "laborCosts" do
it "is represented" do
expected = "10.00 EUR"
expect(subject).to be_json_eql(expected.to_json).at_path("laborCosts")
end
end
- context "overallCosts" do
+ describe "overallCosts" do
it "is represented" do
expected = "15.00 EUR"
expect(subject).to be_json_eql(expected.to_json).at_path("overallCosts")
end
end
- context "custom field x" do
+ describe "custom field x" do
it "is represented" do
expect(subject).to be_json_eql(sums.custom_field_1.to_json).at_path("customField1")
end