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

baseline api: rubocop refactoring

Sebastian Fiedlschuster8bbbebaafc5f
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index 82e53104c5c..1b8244f3436 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -70,7 +70,8 @@
# work_package.subject # => "Subject at PT0S (current time)"
#
class Journable::WithHistoricAttributes < SimpleDelegator
- attr_accessor :timestamps, :query, :include_only_changed_attributes, :attributes_by_timestamp, :matches_query_filters_at_timestamps
+ attr_accessor :timestamps, :query, :include_only_changed_attributes, :attributes_by_timestamp,
+ :matches_query_filters_at_timestamps
def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
super(journable)
@@ -172,7 +173,7 @@ class Journable::WithHistoricAttributes < SimpleDelegator
def convert_attributes_hash_to_struct(attributes)
Class.new(OpenStruct) do
def as_json(options = nil)
- self.to_h.as_json(options)
+ to_h.as_json(options)
end
end.new(attributes)
end
diff --git a/app/services/api/v3/parse_query_params_service.rb b/app/services/api/v3/parse_query_params_service.rb
index 00c1f4021f0..31eef768fdc 100644
--- a/app/services/api/v3/parse_query_params_service.rb
+++ b/app/services/api/v3/parse_query_params_service.rb
@@ -60,6 +60,7 @@ module API
result
end
+ # rubocop:disable Metrics/AbcSize
def parsed_params(params)
ServiceResult.success result: {
group_by: group_by_from_params(params),
@@ -79,6 +80,7 @@ module API
result.errors.add(:base, e.message)
result
end
+ # rubocop:enable Metrics/AbcSize
def determine_allowed_empty(params)
allow_empty = params.keys
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 81460d79b07..2fabe95eed4 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -487,7 +487,7 @@ module API
def self.work_package(id, timestamps: nil)
"#{root}/work_packages/#{id}" + \
if (param_value = timestamps_to_param_value(timestamps)).present?
- "?" + {timestamps: param_value}.to_query
+ "?#{{ timestamps: param_value }.to_query}"
end.to_s
end
@@ -546,12 +546,13 @@ module API
def self.timestamps_to_param_value(timestamps)
timestamps = Timestamp.parse_multiple(timestamps) if timestamps.is_a? String
timestamps = [timestamps] if timestamps.is_a? Timestamp
- if timestamps.present? and timestamps.kind_of? Array and timestamps != [Timestamp.now]
+ if timestamps.present? and timestamps.is_a? Array and timestamps != [Timestamp.now]
timestamps.collect { |timestamp| timestamp.absolute.iso8601 }.join(",")
end
end
- def self.path_for(path, filters: nil, sort_by: nil, group_by: nil, page_size: nil, offset: nil, select: nil, timestamps: nil)
+ def self.path_for(path, filters: nil, sort_by: nil, group_by: nil, page_size: nil, offset: nil,
+ select: nil, timestamps: nil)
timestamps = timestamps_to_param_value(timestamps)
query_params = {
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index ad336644731..c008259c698 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -26,6 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
+# rubocop:disable Metrics/AbcSize
module API::V3::WorkPackages::EagerLoading
class HistoricAttributes < Base
attr_accessor :timestamps, :query
@@ -36,7 +37,8 @@ module API::V3::WorkPackages::EagerLoading
work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributes
work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp
work_package.baseline_timestamp = work_package_with_historic_attributes.baseline_timestamp
- work_package.matches_query_filter_at_baseline_timestamp = work_package_with_historic_attributes.matches_query_filter_at_baseline_timestamp?
+ work_package.matches_query_filter_at_baseline_timestamp = \
+ work_package_with_historic_attributes.matches_query_filter_at_baseline_timestamp?
work_package.matches_query_filters_at_timestamps = work_package_with_historic_attributes.matches_query_filters_at_timestamps
end
@@ -52,7 +54,6 @@ module API::V3::WorkPackages::EagerLoading
Journable::WithHistoricAttributes.wrap_multiple(work_packages, timestamps: @timestamps, query: @query)
end
end
-
end
module HistoricAttributesAccessors
@@ -79,3 +80,4 @@ module API::V3::WorkPackages::EagerLoading
end
end
end
+# rubocop:enable Metrics/AbcSize
diff --git a/lib/api/v3/work_packages/show_end_point.rb b/lib/api/v3/work_packages/show_end_point.rb
index 0cfd21f17a7..261b9258ce9 100644
--- a/lib/api/v3/work_packages/show_end_point.rb
+++ b/lib/api/v3/work_packages/show_end_point.rb
@@ -36,4 +36,4 @@ module API::V3::WorkPackages
timestamps: Timestamp.parse_multiple(request.params[:timestamps]))
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb
index 3db7a34a38c..7e5dc59b274 100644
--- a/lib/api/v3/work_packages/work_package_collection_representer.rb
+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb
@@ -82,8 +82,8 @@ module API
# and set those to be the represented collection.
# A potential ordering is reapplied to the work package collection in ruby.
- # TODO: Should we pass the timestamps only through the `query` rather than passing them separately?
- @represented = ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap(represented, current_user, timestamps:, query: _query)
+ @represented = ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper \
+ .wrap(represented, current_user, timestamps:, query: _query)
end
link :sumsSchema do
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 76cea344485..ff378688693 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -452,14 +452,14 @@ module API
if: ->(*) { respond_to?(:baseline_attributes) and baseline_attributes.present? },
getter: ->(*) do
baseline_attributes.to_h.merge({
- "_links": {
- "self": {
- "href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: baseline_timestamp)
+ '_links': {
+ 'self': {
+ 'href': API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: baseline_timestamp)
}
},
- "_meta": {
- "timestamp": baseline_timestamp.to_s,
- "matchesFilters": matches_query_filter_at_baseline_timestamp?
+ '_meta': {
+ 'timestamp': baseline_timestamp.to_s,
+ 'matchesFilters': matches_query_filter_at_baseline_timestamp?
}.compact
})
end,
@@ -472,13 +472,13 @@ module API
getter: ->(*) do
attributes_by_timestamp.to_a.collect do |timestamp, attributes|
[timestamp, attributes.to_h.merge({
- "_links": {
- "self": {
- "href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: timestamp)
+ '_links': {
+ 'self': {
+ 'href': API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: timestamp)
}
},
- "_meta": {
- "matchesFilters": matches_query_filter_at_timestamp?(timestamp)
+ '_meta': {
+ 'matchesFilters': matches_query_filter_at_timestamp?(timestamp)
}.compact
})]
end.to_h
diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
index 580ef598ebe..21f00737fd7 100644
--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
@@ -85,7 +85,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
it 'has the absolute timestamps within the self link' do
Timecop.freeze do
- is_expected
+ expect(subject)
.to include_json(absolute_timestamps_query_param.to_json)
.at_path('_links/self/href')
end
@@ -97,7 +97,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
it 'has no timestamps within the self link' do
Timecop.freeze do
- is_expected
+ expect(subject)
.not_to include_json("timestamps".to_json)
.at_path('_links/self/href')
end
@@ -276,7 +276,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
describe 'ancestors' do
it 'are being eager loaded' do
representer.represented.each do |wp|
- expect(wp.work_package_ancestors).to be_kind_of(Array)
+ expect(wp.work_package_ancestors).to be_a(Array)
expect(wp.ancestors).to eq(wp.work_package_ancestors)
end
end
@@ -528,8 +528,8 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
.extract!(*Journal::WorkPackageJournal.attribute_names) \
.symbolize_keys.merge(attributes)
create(:work_package_journal, version:,
- journable:, created_at: timestamp, updated_at: timestamp,
- data: build(:journal_work_package_journal, journal_attributes))
+ journable:, created_at: timestamp, updated_at: timestamp,
+ data: build(:journal_work_package_journal, journal_attributes))
end
before do
@@ -552,13 +552,13 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
it 'embeds the properties of the baseline work package in attributesByTimestamp' do
expect(collection)
.to be_json_eql("The original work package".to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first.to_s}/subject")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/subject")
end
it 'embeds the link to the baseline work package in attributesByTimestamp' do
expect(collection)
.to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first.to_s}/_links/self/href")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_links/self/href")
end
it 'embeds the properties of the baseline work package in baselineAttributes' do
@@ -579,46 +579,51 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
let(:absolute_timestamps_query_param) { { timestamps: absolute_timestamp_strings.join(",") }.to_query }
it 'has the absolute timestamps within the self link' do
- is_expected
+ expect(subject)
.to include_json(absolute_timestamps_query_param.to_json)
.at_path('_embedded/elements/0/_links/self/href')
end
end
- context 'baseline and current' do
+ context 'with baseline and current timestamps' do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
+
it_behaves_like 'includes the properties of the current work package'
it_behaves_like 'embeds the properties of the baseline work package'
it_behaves_like 'has the absolute timestamps within the self link'
end
- context 'current only' do
+ context 'with current timestamp only' do
let(:timestamps) { [Timestamp.parse("PT0S")] }
+
it_behaves_like 'includes the properties of the current work package'
it 'has no timestamps within the self link' do
- is_expected
+ expect(subject)
.not_to include_json("timestamps".to_json)
.at_path('_embedded/elements/0/_links/self/href')
end
end
- context 'baseline only' do
+ context 'with baseline timestamp only' do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
it 'includes the properties of the baseline work package' do
expect(collection)
.to be_json_eql("The original work package".to_json)
.at_path('_embedded/elements/0/subject')
end
+
it_behaves_like 'has the absolute timestamps within the self link'
end
- context 'empty' do
+ context 'with empty timestamp' do
let(:timestamps) { [] }
+
it_behaves_like 'includes the properties of the current work package'
it 'has no timestamps within the self link' do
- is_expected
+ expect(subject)
.not_to include_json("timestamps".to_json)
.at_path('_embedded/elements/0/_links/self/href')
end
@@ -642,7 +647,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
member_with_permissions: %i[view_work_packages view_file_links])
end
- context 'baseline and current' do
+ context 'with baseline and current timestamps' do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
describe 'baselineAttributes' do
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index 89bce741742..45ce3bf9008 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1328,8 +1328,8 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
.extract!(*Journal::WorkPackageJournal.attribute_names) \
.symbolize_keys.merge(attributes)
create(:work_package_journal, version:,
- journable:, created_at: timestamp, updated_at: timestamp,
- data: build(:journal_work_package_journal, journal_attributes))
+ journable:, created_at: timestamp, updated_at: timestamp,
+ data: build(:journal_work_package_journal, journal_attributes))
end
before do
@@ -1337,7 +1337,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
# in spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb.
# However, I feel more comfortable if we test the real thing here.
#
- allow(::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper)
+ allow(API::V3::WorkPackages::WorkPackageEagerLoadingWrapper)
.to receive(:wrap_one)
.and_call_original
@@ -1369,8 +1369,8 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
it 'has no information about whether the work package matches the query filters at the baseline time' \
'because there are no filters without a query' do
- expect(subject)
- .not_to have_json_path('_embedded/baselineAttributes/_meta/matchesFilters')
+ expect(subject)
+ .not_to have_json_path('_embedded/baselineAttributes/_meta/matchesFilters')
end
end
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index ddbaf9428ae..68bb40ab5a7 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -384,6 +384,7 @@ describe Journable::HistoricActiveRecordRelation do
describe "#first" do
describe "for columns that don't exist in the journal-data table" do
let(:column_name) { :lock_version }
+
subject { historic_relation.first.send(column_name) }
specify "the column name does exist in the journable table" do
@@ -395,7 +396,7 @@ describe Journable::HistoricActiveRecordRelation do
end
it "has the attribute with null value" do
- expect(historic_relation.first.attributes_before_type_cast[column_name]).to be nil
+ expect(historic_relation.first.attributes_before_type_cast[column_name]).to be_nil
end
it "has the typecasted value matching the journable class's data type" do
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index d3002ef3521..1fd6677e490 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -560,7 +560,7 @@ describe Journable::Timestamps do
end
it "has the attribute with null value" do
- expect(subject.attributes_before_type_cast[column_name]).to be nil
+ expect(subject.attributes_before_type_cast[column_name]).to be_nil
end
it "has the typecasted value matching the journable class's data type" do
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index 696d7e70f1e..66b3aaadfd7 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -368,7 +368,7 @@ describe Timestamp do
subject { Query.where("updated_at < ?", timestamp.to_time) }
it "raises no error" do
- expect { subject }.not_to raise_error TypeError
+ expect { subject }.not_to raise_error
end
end
end
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index 87428aa519e..8abe6edd0db 100644
--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb
@@ -229,13 +229,13 @@ describe 'API v3 Work package resource',
end
let(:original_journal) do
create_journal(journable: work_package, timestamp: baseline_time - 1.day,
- version: 1,
- attributes: { subject: "The original work package" })
+ version: 1,
+ attributes: { subject: "The original work package" })
end
let(:current_journal) do
create_journal(journable: work_package, timestamp: 1.day.ago,
- version: 2,
- attributes: { subject: "The current work package" })
+ version: 2,
+ attributes: { subject: "The current work package" })
end
def create_journal(journable:, version:, timestamp:, attributes: {})
@@ -244,8 +244,8 @@ describe 'API v3 Work package resource',
.extract!(*Journal::WorkPackageJournal.attribute_names) \
.symbolize_keys.merge(attributes)
create(:work_package_journal, version:,
- journable:, created_at: timestamp, updated_at: timestamp,
- data: build(:journal_work_package_journal, journal_attributes))
+ journable:, created_at: timestamp, updated_at: timestamp,
+ data: build(:journal_work_package_journal, journal_attributes))
end
before do
@@ -269,10 +269,10 @@ describe 'API v3 Work package resource',
it 'embeds the attributesByTimestamp' do
expect(subject)
.to be_json_eql("The original work package".to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps.first.to_s}/subject")
+ .at_path("_embedded/attributesByTimestamp/#{timestamps.first}/subject")
expect(subject)
.to be_json_eql("The current work package".to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps.last.to_s}/subject")
+ .at_path("_embedded/attributesByTimestamp/#{timestamps.last}/subject")
end
it 'has the current attributes as attributes' do
diff --git a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb b/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
index fff79363b41..60cb7cbcda3 100644
--- a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
+++ b/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
@@ -237,7 +237,7 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
context 'timestamps' do
let(:timestamps) { [Timestamp.parse("P-1Y"), Timestamp.now] }
- let(:query) { build_stubbed(:query, timestamps: timestamps) }
+ let(:query) { build_stubbed(:query, timestamps:) }
it 'has the query timestamps' do
expect(subject.timestamps)