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

Merge remote-tracking branch 'origin/pr/11783' into implementation/45801-api-with-subset-representer

ulfertsfda05d70da40
diff --cc app/models/journable/with_historic_attributes.rb
index 1ba4cb59c20,a63515eb4e5..dd8dcbcd25d
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@@ -145,10 -150,43 +145,9 @@@ class Journable::WithHistoricAttribute
end
end
- def extract_historic_attributes_from(historic_journable:)
- convert_attributes_hash_to_struct(
- historic_journable.attributes.select do |key, value|
- not include_only_changed_attributes \
- or not respond_to?(key) \
- or value != send(key)
- end
- )
- 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`.
- #
- # We need to patch the `as_json` method because OpenStruct's `as_json` would
- # wrap everything into a "table" hash.
- #
- # rubocop:disable Style/OpenStructUse
- #
- def convert_attributes_hash_to_struct(attributes)
- Class.new(OpenStruct) do
- def as_json(options = nil)
- to_h.as_json(options)
- end
- end.new(attributes)
+ def exists_at_timestamps
+ timestamps.select { |t| at_timestamp(t).present? }
end
- # rubocop:enable Style/OpenStructUse
--
def baseline_timestamp
timestamps.first
end
@@@ -170,13 -208,23 +169,21 @@@
end
def matches_query_filters_at_timestamp?(timestamp)
- query && matches_query_filters_at_timestamps.include?(timestamp)
+ matches_query_filters_at_timestamps.include?(timestamp)
end
- def self.query_work_packages(query:, timestamp: nil)
- query = query.dup
- query.timestamps = [timestamp] if timestamp
- query.results.work_packages
+ def at_timestamp(timestamp)
+ loader.journable_at_timestamp(__getobj__, timestamp)
end
+ def id
+ __getobj__.try(:id)
+ end
+
+ def attributes
+ __getobj__.try(:attributes)
+ end
+
def to_ary
__getobj__.send(:to_ary)
end
diff --cc spec/models/journable/with_historic_attributes_spec.rb
index ff9528a6d61,a7815493a6d..64dab3bfd9d
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@@ -102,60 -61,93 +102,60 @@@ describe Journable::WithHistoricAttribu
.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
- WorkPackage.destroy_all
- work_package
- Journal.destroy_all
- original_journal
- current_journal
- end
+ subject { described_class.wrap(work_packages, timestamps:, query:, include_only_changed_attributes:) }
describe ".wrap" 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:) }
+ context "with a single work package" do
+ let(:work_packages) { work_package1 }
- it "returns a Journable::WithHistoricAttributes instance" do
- expect(subject).to be_a described_class
+ it "returns a Journable::WithHistoricAttributes instance" do
+ expect(subject).to be_a described_class
+ end
end
- it "provides access to the work-package attributes" do
- expect(subject.subject).to eq "The current work package"
- end
+ context "with an array of work packages" do
+ let(:work_packages) { [work_package1, work_package2] }
- it "provides access to the work-package attributes at timestamps" do
- expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
- expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package"
+ it "returns an array of Journable::WithHistoricAttributes instances" do
+ expect(subject).to all be_a described_class
+ end
end
- it "determines for each timestamp whether the journable exists at that timestamp" do
- expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
- end
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
- it "determines whether the journable attributes are historic" do
- expect(subject.historic?).to be false
+ it "returns an array of Journable::WithHistoricAttributes instances" do
+ expect(subject).to all be_a described_class
+ end
end
+ end
- describe "when providing a query" do
- let(:query) do
- login_as(user1)
- build(:query, user: nil, project: nil).tap do |query|
- query.filters.clear
- query.add_filter 'subject', '~', search_term
- end
- end
- let(:search_term) { "original" }
+ describe "delegation to original object" do
+ context "with a single work package" do
+ let(:work_packages) { work_package1 }
- it "determines for each timestamp whether the journable matches the query at that timestamp" do
- expect(subject.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ it "provides access to the work-package attributes" do
+ expect(subject.subject).to eq "The current work package 1"
end
- describe "when the work package did not exist yet at the basline date" do
- let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:search_term) { "current" }
-
- it "does not include the timestamp in the matches_query_filters_at_timestamps array" do
- expect(subject.matches_query_filters_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
- expect(subject.matches_query_filters_at_timestamps).to include Timestamp.parse("PT0S")
- end
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- it "does not include the timestamp in the exists_at_timestamps array" do
- expect(subject.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
- expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ it "provides access to the historic work-package attributes" do
+ expect(subject.subject).to eq "The original work package 1"
end
end
- end
-
- 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
- expect(subject.attributes_by_timestamp["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_by_timestamp["PT0S"].subject).to be_nil
- end
+ describe "when the work package did not exist yet at the baseline date" do
+ let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- it "includes the timestamps in the exists_at_timestamps array" do
- expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ it "provides access to the work-package attributes" do
+ expect(subject.subject).to eq "The current work package 1"
+ end
end
end