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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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_timestampsdef 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."endself.query = query@@ -92,25 +92,27 @@ class Journable::WithAttributesAtTimestamps < SimpleDelegatordef 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)+ )endjournableenddef 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:+ )endjournablesend@@ -120,8 +122,16 @@ class Journable::WithAttributesAtTimestamps < SimpleDelegatormatches_query_at_timestamps << timestamp if matching_journableend+ 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_timestamptimestamps.firstenddiff --git a/spec/models/journable/with_attributes_at_timestamps_spec.rb b/spec/models/journable/with_attributes_at_timestamps_spec.rbindex 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))endbefore do@@ -76,10 +76,11 @@ describe Journable::WithAttributesAtTimestamps dolet(: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_classendit "provides access to the work-package attributes" do@@ -92,7 +93,7 @@ describe Journable::WithAttributesAtTimestamps doendit "determines whether the journable attributes are historic" do- expect(subject.historic?).to eq false+ expect(subject.historic?).to be falseenddescribe "when providing a query" do@@ -114,12 +115,13 @@ describe Journable::WithAttributesAtTimestamps dodescribe "with include_only_changed_attributes: true" dolet(: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" doexpect(subject.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"endspecify "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_nilendend@@ -135,7 +137,7 @@ describe Journable::WithAttributesAtTimestamps doendit "determines whether the journable attributes are historic" do- expect(subject.historic?).to eq true+ expect(subject.historic?).to be trueendend@@ -156,13 +158,14 @@ describe Journable::WithAttributesAtTimestamps dolet(: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" dolet(: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_classendit "provides access to the work-package attributes" do@@ -193,12 +196,13 @@ describe Journable::WithAttributesAtTimestamps dodescribe "with include_only_changed_attributes: true" dolet(: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" doexpect(subject.first.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"endspecify "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_nilendend@@ -214,7 +218,7 @@ describe Journable::WithAttributesAtTimestamps doendit "determines whether the journable attributes are historic" do- expect(subject.first.historic?).to eq true+ expect(subject.first.historic?).to be trueendendend@@ -223,6 +227,7 @@ describe Journable::WithAttributesAtTimestamps dodescribe "#baseline_timestamp" dolet(: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 dodescribe "#baseline_attributes" dolet(: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 dodescribe "#matches_query_filter_at_baseline_timestamp?" dolet(: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) dologin_as(user1)build(:query, user: nil, project: nil).tap do |query|@@ -257,7 +263,7 @@ describe Journable::WithAttributesAtTimestamps dolet(: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 trueendend@@ -265,7 +271,7 @@ describe Journable::WithAttributesAtTimestamps dolet(: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 falseendendend@@ -273,6 +279,7 @@ describe Journable::WithAttributesAtTimestamps dodescribe "#current_timestamp" dolet(: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 dodescribe "#matches_query_filter_at_current_timestamp?" dolet(: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) dologin_as(user1)build(:query, user: nil, project: nil).tap do |query|@@ -297,7 +304,7 @@ describe Journable::WithAttributesAtTimestamps dolet(: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 falseendend@@ -305,8 +312,8 @@ describe Journable::WithAttributesAtTimestamps dolet(: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 trueendendend-end\ No newline at end of file+end