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 Fiedlschusterefe70c2f606c
diff --git a/app/models/journable/with_attributes_at_timestamps.rb b/app/models/journable/with_attributes_at_timestamps.rb
index fc2df642f1d..54e0c1bdacd 100644
--- a/app/models/journable/with_attributes_at_timestamps.rb
+++ b/app/models/journable/with_attributes_at_timestamps.rb
@@ -70,15 +70,15 @@
# work_package.subject # => "Subject at PT0S (current time)"
#
class Journable::WithAttributesAtTimestamps < SimpleDelegator
- attr_accessor :timestamps, :query, :include_only_changed_attributes
- attr_accessor :attributes_at_timestamps, :matches_query_at_timestamps
+ attr_accessor :timestamps, :query, :include_only_changed_attributes, :attributes_at_timestamps, :matches_query_at_timestamps
def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
super(journable)
if query and not journable.is_a? WorkPackage
- raise Journable::NotImplementedError, "Journable::WithAttributesAtTimestamps with query is only implemented for WorkPackages" \
- "at the moment because Query objects currently only support work packages."
+ raise Journable::NotImplementedError, "Journable::WithAttributesAtTimestamps with query " \
+ "is only implemented for WorkPackages at the moment " \
+ "because Query objects currently only support work packages."
end
self.query = query
@@ -92,25 +92,27 @@ class Journable::WithAttributesAtTimestamps < SimpleDelegator
def self.wrap(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
journable = journable.at_timestamp(timestamps.last) if timestamps.last.try(:historic?)
journable = new(journable, timestamps:, query:, include_only_changed_attributes:)
- timestamps.each do |timestamp|
- historic_journable = journable.at_timestamp(timestamp)
- matching_journable = query_work_packages(query:, timestamp:).find_by(id: journable.id) if query
- journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)
+ (timestamps || query.timestamps || []).each do |timestamp|
+ journable.assign_historic_attributes(
+ timestamp:,
+ historic_journable: journable.at_timestamp(timestamp),
+ matching_journable: (query_work_packages(query:, timestamp:).find_by(id: journable.id) if query)
+ )
end
journable
end
def self.wrap_multiple(journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
- journables = journables.first.class.at_timestamp(timestamps.last).where(id: journables.map(&:id)) if timestamps.last.try(:historic?)
+ journables = journables.first.class.at_timestamp(timestamps.last).where(id: journables) if timestamps.last.try(:historic?)
journables = journables.map { |j| new(j, timestamps:, query:, include_only_changed_attributes:) }
(timestamps || query.timestamps || []).each do |timestamp|
- historic_journables = WorkPackage.at_timestamp(timestamp).where(id: journables.map(&:id))
- matching_journables = query_work_packages(query:, timestamp:) if query
- journables.each do |journable|
- historic_journable = historic_journables.detect { |j| j.id == journable.id }
- matching_journable = matching_journables.detect { |j| j.id == journable.id } if query
- journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)
- end
+ assign_historic_attributes_to(
+ journables,
+ timestamp:,
+ historic_journables: WorkPackage.at_timestamp(timestamp).where(id: journables),
+ matching_journables: (query_work_packages(query:, timestamp:) if query),
+ query:
+ )
end
journables
end
@@ -120,8 +122,16 @@ class Journable::WithAttributesAtTimestamps < SimpleDelegator
matches_query_at_timestamps << timestamp if matching_journable
end
+ def self.assign_historic_attributes_to(journables, timestamp:, historic_journables:, matching_journables:, query:)
+ journables.each do |journable|
+ historic_journable = historic_journables.find_by(id: journable.id)
+ matching_journable = matching_journables.find_by(id: journable.id) if query
+ journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)
+ end
+ end
+
def extract_historic_attributes_from(historic_journable:)
- OpenStruct.new(
+ convert_attributes_hash_to_struct(
historic_journable.attributes.select do |key, value|
not include_only_changed_attributes \
or not respond_to?(key) \
@@ -130,6 +140,26 @@ class Journable::WithAttributesAtTimestamps < SimpleDelegator
)
end
+ # This allows us to use the historic attributes in the same way as the current attributes
+ # using methods rather than hash keys.
+ #
+ # Example:
+ # work_package.baseline_attributes.subject
+ # work_package.baseline_attributes["subject"]
+ #
+ # Rubocop complains about OpenStruct because it is slightly slower than Struct.
+ # https://docs.rubocop.org/rubocop/cops_style.html#styleopenstructuse
+ #
+ # However, I prefer OpenStruct here because it makes it easier to deal with the
+ # non existing attributes when using `include_only_changed_attributes: true`.
+ #
+ # rubocop:disable Style/OpenStructUse
+ #
+ def convert_attributes_hash_to_struct(attributes)
+ OpenStruct.new(attributes)
+ end
+ # rubocop:enable Style/OpenStructUse
+
def baseline_timestamp
timestamps.first
end
diff --git a/spec/models/journable/with_attributes_at_timestamps_spec.rb b/spec/models/journable/with_attributes_at_timestamps_spec.rb
index 459e877dc9d..60e08a7f97e 100644
--- a/spec/models/journable/with_attributes_at_timestamps_spec.rb
+++ b/spec/models/journable/with_attributes_at_timestamps_spec.rb
@@ -60,8 +60,8 @@ describe Journable::WithAttributesAtTimestamps 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
@@ -76,10 +76,11 @@ describe Journable::WithAttributesAtTimestamps do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:query) { nil }
let(:include_only_changed_attributes) { nil }
+
subject { described_class.wrap(work_package, timestamps:, query:, include_only_changed_attributes:) }
it "returns a Journable::WithAttributesAtTimestamps instance" do
- is_expected.to be_a described_class
+ expect(subject).to be_a described_class
end
it "provides access to the work-package attributes" do
@@ -92,7 +93,7 @@ describe Journable::WithAttributesAtTimestamps do
end
it "determines whether the journable attributes are historic" do
- expect(subject.historic?).to eq false
+ expect(subject.historic?).to be false
end
describe "when providing a query" do
@@ -114,12 +115,13 @@ describe Journable::WithAttributesAtTimestamps do
describe "with include_only_changed_attributes: true" do
let(:include_only_changed_attributes) { true }
- it "provides access to the work-package attributes at timestamps where the attribute is different from the work package's attribute" do
+ it "provides access to the work-package attributes at timestamps " \
+ "where the attribute is different from the work package's attribute" do
expect(subject.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
end
specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
- expect(subject.attributes_at_timestamps["PT0S"].subject).to eq nil
+ expect(subject.attributes_at_timestamps["PT0S"].subject).to be_nil
end
end
@@ -135,7 +137,7 @@ describe Journable::WithAttributesAtTimestamps do
end
it "determines whether the journable attributes are historic" do
- expect(subject.historic?).to eq true
+ expect(subject.historic?).to be true
end
end
@@ -156,13 +158,14 @@ describe Journable::WithAttributesAtTimestamps do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:query) { nil }
let(:include_only_changed_attributes) { nil }
+
subject { described_class.wrap_multiple(work_packages, timestamps:, query:, include_only_changed_attributes:) }
context "with a single work package" do
let(:work_packages) { [work_package] }
it "returns an array of Journable::WithAttributesAtTimestamps instances" do
- is_expected.to all be_a described_class
+ expect(subject).to all be_a described_class
end
it "provides access to the work-package attributes" do
@@ -193,12 +196,13 @@ describe Journable::WithAttributesAtTimestamps do
describe "with include_only_changed_attributes: true" do
let(:include_only_changed_attributes) { true }
- it "provides access to the work-package attributes at timestamps where the attribute is different from the work package's attribute" do
+ it "provides access to the work-package attributes at timestamps " \
+ "where the attribute is different from the work package's attribute" do
expect(subject.first.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
end
specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
- expect(subject.first.attributes_at_timestamps["PT0S"].subject).to eq nil
+ expect(subject.first.attributes_at_timestamps["PT0S"].subject).to be_nil
end
end
@@ -214,7 +218,7 @@ describe Journable::WithAttributesAtTimestamps do
end
it "determines whether the journable attributes are historic" do
- expect(subject.first.historic?).to eq true
+ expect(subject.first.historic?).to be true
end
end
end
@@ -223,6 +227,7 @@ describe Journable::WithAttributesAtTimestamps do
describe "#baseline_timestamp" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package, timestamps:) }
+
subject { journable.baseline_timestamp }
it "provides simplified access to the baseline timestamp, which is the first given timestamp" do
@@ -233,6 +238,7 @@ describe Journable::WithAttributesAtTimestamps do
describe "#baseline_attributes" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package, timestamps:) }
+
subject { journable.baseline_attributes }
it "provides access to the work-package attributes at the baseline timestamp" do
@@ -242,7 +248,7 @@ describe Journable::WithAttributesAtTimestamps do
describe "#matches_query_filter_at_baseline_timestamp?" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps: timestamps, query: query) }
+ let(:journable) { described_class.wrap(work_package, timestamps:, query:) }
let(:query) do
login_as(user1)
build(:query, user: nil, project: nil).tap do |query|
@@ -257,7 +263,7 @@ describe Journable::WithAttributesAtTimestamps do
let(:search_term) { "original" }
it "determines whether the journable matches the query at the baseline timestamp" do
- expect(subject).to eq true
+ expect(subject).to be true
end
end
@@ -265,7 +271,7 @@ describe Journable::WithAttributesAtTimestamps do
let(:search_term) { "current" }
it "determines whether the journable matches the query at the baseline timestamp" do
- expect(subject).to eq false
+ expect(subject).to be false
end
end
end
@@ -273,6 +279,7 @@ describe Journable::WithAttributesAtTimestamps do
describe "#current_timestamp" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package, timestamps:) }
+
subject { journable.current_timestamp }
it "provides simplified access to the current timestamp, which is the last given timestamp" do
@@ -282,7 +289,7 @@ describe Journable::WithAttributesAtTimestamps do
describe "#matches_query_filter_at_current_timestamp?" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps: timestamps, query: query) }
+ let(:journable) { described_class.wrap(work_package, timestamps:, query:) }
let(:query) do
login_as(user1)
build(:query, user: nil, project: nil).tap do |query|
@@ -297,7 +304,7 @@ describe Journable::WithAttributesAtTimestamps do
let(:search_term) { "original" }
it "determines whether the journable matches the query at the current timestamp" do
- expect(subject).to eq false
+ expect(subject).to be false
end
end
@@ -305,8 +312,8 @@ describe Journable::WithAttributesAtTimestamps do
let(:search_term) { "current" }
it "determines whether the journable matches the query at the current timestamp" do
- expect(subject).to eq true
+ expect(subject).to be true
end
end
end
-end
\ No newline at end of file
+end