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

introduce loader object for historic_attributes

ulfertsf2251ba1a742
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index f22f08a2856..ce3a3a25099 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -70,12 +70,16 @@
# 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, :exists_at_timestamps,
- :journables_by_timestamp
-
-
- def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
+ attr_accessor :timestamps,
+ :query,
+ :include_only_changed_attributes,
+ :loader
+
+ def initialize(journable,
+ timestamps: nil,
+ query: nil,
+ include_only_changed_attributes: false,
+ loader: Loader.new(journable))
super(journable)
if query and not journable.is_a? WorkPackage
@@ -88,104 +92,59 @@ class Journable::WithHistoricAttributes < SimpleDelegator
self.timestamps = timestamps || query.try(:timestamps) || []
self.include_only_changed_attributes = include_only_changed_attributes
- self.attributes_by_timestamp = {}
- self.matches_query_filters_at_timestamps = []
- self.exists_at_timestamps = []
- self.journables_by_timestamp = {}
+ self.loader = loader
end
+ private_class_method :new
- def self.wrap(journable_or_journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
- case journable_or_journables
- when Array, ActiveRecord::Relation
- wrap_multiple(journable_or_journables, timestamps:, query:, include_only_changed_attributes:)
- else
- wrap_one(journable_or_journables, timestamps:, query:, include_only_changed_attributes:)
+ class << self
+ def wrap(journable_or_journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
+ case journable_or_journables
+ when Array, ActiveRecord::Relation
+ wrap_multiple(journable_or_journables, timestamps:, query:, include_only_changed_attributes:)
+ else
+ wrap_one(journable_or_journables, timestamps:, query:, include_only_changed_attributes:)
+ end
end
- end
- def self.wrap_one(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
- timestamps ||= query.try(:timestamps) || []
- journable = journable.at_timestamp(timestamps.last) if timestamps.last.try(:historic?)
- journable = new(journable, timestamps:, query:, include_only_changed_attributes:)
- 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)
- )
+ private
+
+ def wrap_one(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
+ wrap_multiple(Array(journable), timestamps:, query:, include_only_changed_attributes:).first
+ end
+
+ def wrap_multiple(journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
+ timestamps ||= query.try(:timestamps) || []
+ loader = Loader.new(journables)
+
+ journables = loader.at_timestamp(timestamps.last).values if timestamps.last.try(:historic?)
+
+ journables.map { |j| new(j, timestamps:, query:, include_only_changed_attributes:, loader:) }
end
- journable
- end
-
- # rubocop:disable Metrics/AbcSize
- def self.wrap_multiple(journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
- timestamps ||= query.try(:timestamps) || []
- 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.each do |timestamp|
- assign_historic_attributes_to(
- journables,
- timestamp:,
- historic_journables: WorkPackage.at_timestamp(timestamp).where(id: journables.map(&:id)),
- matching_journables: (query_work_packages(query:, timestamp:) if query),
- query:
- )
+ end
+
+ def attributes_by_timestamp
+ @attributes_by_timestamp ||= Hash.new do |h, t|
+ attributes = if include_only_changed_attributes
+ changes_at_timestamp(t)&.transform_values(&:last)
+ else
+ historic_attributes_at(t)
+ end
+
+ h[t] = attributes ? Hashie::Mash.new(attributes) : nil
end
- journables
end
- # rubocop:enable Metrics/AbcSize
- def assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)
- attributes_by_timestamp[timestamp.to_s] = extract_historic_attributes_from(historic_journable:) if historic_journable
- journables_by_timestamp[timestamp.to_s] = historic_journable
- matches_query_filters_at_timestamps << timestamp if matching_journable
- exists_at_timestamps << timestamp if historic_journable
+ def changed_at_timestamp(timestamp)
+ changes_at_timestamp(timestamp)&.keys || []
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
+ def matches_query_filters_at_timestamps
+ timestamps.select { |timestamp| loader.work_package_ids_of_query_at_timestamp(query:, timestamp:).include?(__getobj__.id) }
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
@@ -211,18 +170,8 @@ class Journable::WithHistoricAttributes < SimpleDelegator
query && matches_query_filters_at_timestamps.include?(timestamp)
end
- def changed_at_timestamp(timestamp)
- return [] unless journables_by_timestamp[timestamp.to_s]
-
- ::Acts::Journalized::JournableDiffer
- .changes(__getobj__, journables_by_timestamp[timestamp.to_s])
- .keys
- 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 to_ary
@@ -233,5 +182,28 @@ class Journable::WithHistoricAttributes < SimpleDelegator
__getobj__.inspect.gsub(/#<(.+)>/m, "#<#{self.class.name} \\1>")
end
+ private
+
+ def historic_attributes_at(timestamp)
+ historic_journable = at_timestamp(Timestamp.parse(timestamp))
+
+ return unless historic_journable
+
+ historic_journable
+ .attributes
+ .select do |key, _|
+ respond_to?(key)
+ end
+ end
+
+ def changes_at_timestamp(timestamp)
+ historic_journable = at_timestamp(Timestamp.parse(timestamp))
+
+ return unless historic_journable
+
+ ::Acts::Journalized::JournableDiffer
+ .changes(__getobj__, historic_journable)
+ end
+
class NotImplemented < StandardError; end
end
diff --git a/app/models/journable/with_historic_attributes/loader.rb b/app/models/journable/with_historic_attributes/loader.rb
new file mode 100644
index 00000000000..6f8096d10e0
--- /dev/null
+++ b/app/models/journable/with_historic_attributes/loader.rb
@@ -0,0 +1,69 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+class Journable::WithHistoricAttributes
+ class Loader
+ def initialize(journables)
+ @journables = Array(journables)
+ end
+
+ def journable_at_timestamp(journable, timestamp)
+ at_timestamp(timestamp)[journable.id]
+ end
+
+ def at_timestamp(timestamp)
+ @at_timestamp ||= Hash.new do |h, t|
+ h[t] = journables.first.class.at_timestamp(t).where(id: journables.map(&:id)).index_by(&:id)
+ end
+
+ @at_timestamp[timestamp]
+ end
+
+ def work_package_ids_of_query_at_timestamp(query:, timestamp: nil)
+ @work_package_ids_of_query_at_timestamp ||= Hash.new do |qh, q|
+ qh[q] = Hash.new do |ht, t|
+ ht[t] = work_package_ids_of_query_at_timestamp_calculation(q, t)
+ end
+ end
+
+ @work_package_ids_of_query_at_timestamp[query][timestamp]
+ end
+
+ private
+
+ def work_package_ids_of_query_at_timestamp_calculation(query, timestamp)
+ query = query.dup
+ query.timestamps = [timestamp] if timestamp
+
+ query.results.work_packages.where(id: journables.map(&:id)).pluck(:id)
+ end
+
+ attr_accessor :journables
+ end
+ private_constant :Loader
+end
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 602dcf4eddf..c559e835285 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -38,9 +38,10 @@ module API::V3::WorkPackages::EagerLoading
work_package_with_historic_attributes.timestamps.last.to_s)
work_package.at_timestamps = work_package_with_historic_attributes
- .journables_by_timestamp
- .map do |timestamp, wp|
- wrapped_wp = HistoricAttributesDelegator.new(wp)
+ .timestamps
+ .map do |timestamp|
+ wrapped_wp = HistoricAttributesDelegator
+ .new(work_package_with_historic_attributes.at_timestamp(timestamp))
wrapped_wp.timestamp = timestamp.dup
set_non_delegated_properties(wrapped_wp,
@@ -68,7 +69,7 @@ module API::V3::WorkPackages::EagerLoading
@work_packages_with_historic_attributes ||= begin
@timestamps ||= @query.try(:timestamps) || []
Journable::WithHistoricAttributes \
- .wrap_multiple(work_packages, timestamps: @timestamps, query: @query, include_only_changed_attributes: true)
+ .wrap(work_packages, timestamps: @timestamps, query: @query, include_only_changed_attributes: true)
.index_by(&:id)
end
end
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index bec9371db95..ff9528a6d61 100644
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@ -84,6 +84,17 @@ describe Journable::WithHistoricAttributes do
member_in_project: work_package1.project,
member_with_permissions: %i[view_work_packages view_file_links])
end
+ let(:build_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(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
+ let(:query) { nil }
+ let(:include_only_changed_attributes) { nil }
def create_journal(journable:, version:, timestamp:, attributes: {})
work_package_attributes = journable.attributes.except("id")
@@ -95,163 +106,131 @@ describe Journable::WithHistoricAttributes do
data: build(:journal_work_package_journal, journal_attributes))
end
- 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_packages, timestamps:, query:, include_only_changed_attributes:) }
- subject { described_class.wrap(work_package1, timestamps:, query:, include_only_changed_attributes:) }
+ describe ".wrap" do
+ 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 1"
- 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 1"
- expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
+ 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 }
+ 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 "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 1"
+ it "provides access to the work-package attributes" do
+ expect(subject.subject).to eq "The current work package 1"
+ end
end
+ 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
+ context "with an array of work packages" do
+ let(:work_packages) { [work_package1, work_package2] }
- 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.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
end
end
- describe "when requesting only historic data" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
-
- it "provides access to the historic work-package attributes" do
- expect(subject.subject).to eq "The original work package 1"
- end
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
- it "provides access to the historic work-package attributes at timestamps" do
- expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ it "provides access to the work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
end
- it "determines whether the journable attributes are historic" do
- expect(subject.historic?).to be true
- end
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- it "includes the timestamp in the exists_at_timestamps array" do
- expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ it "provides access to the historic work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The original work package 1", "The original work package 2"]
+ end
end
end
+ 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")] }
+ describe '#attributes_by_timestamp' do
+ let(:work_packages) { work_package1 }
- it "provides access to the work-package attributes" do
- expect(subject.subject).to eq "The current work package 1"
+ context "with a single work package" do
+ 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 1"
+ expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
end
- it "has no attributes at the baseline date" do
- expect(subject.attributes_by_timestamp["2021-01-01T00:00:00Z"]).to be_nil
- end
+ describe "with include_only_changed_attributes: true" do
+ let(:include_only_changed_attributes) { true }
- it "has no baseline attributes" do
- expect(subject.baseline_attributes).to be_nil
- end
+ 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 1"
+ end
- 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")
+ 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
end
- end
- end
- describe ".wrap_multiple" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:query) { nil }
- let(:include_only_changed_attributes) { nil }
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- subject { described_class.wrap_multiple(work_packages, timestamps:, query:, include_only_changed_attributes:) }
+ it "provides access to the historic work-package attributes at timestamps" do
+ expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ end
+ end
- context "with a single work package" do
- let(:work_packages) { [work_package1, work_package2] }
+ 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 "returns an array of Journable::WithHistoricAttributes instances" do
- expect(subject).to all be_a described_class
+ it "has no attributes at the baseline date" do
+ expect(subject.attributes_by_timestamp["2021-01-01T00:00:00Z"]).to be_nil
+ end
end
+ end
- it "provides access to the work-package attributes" do
- expect(subject.first.subject).to eq "The current work package 1"
- 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.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
- 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" }
-
- it "determines for each timestamp whether the journables matches the query at that timestamp" do
- expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
- end
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ expect(subject.last.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 2"
end
describe "with include_only_changed_attributes: true" do
@@ -270,30 +249,15 @@ describe Journable::WithHistoricAttributes do
describe "when requesting only historic data" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- it "provides access to the historic work-package attributes" do
- expect(subject.first.subject).to eq "The original work package 1"
- end
-
it "provides access to the historic work-package attributes at timestamps" do
expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
- end
-
- it "determines whether the journable attributes are historic" do
- expect(subject.first.historic?).to be true
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
end
end
end
- context "with an array of work packages" do
- let(:work_packages) { [work_package1, work_package2] }
-
- it "returns an array of Journable::WithHistoricAttributes instances" do
- expect(subject).to all be_a described_class
- end
-
- it "provides access to the work-package attributes" do
- expect(subject.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
- end
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
it "provides access to the work-package attributes at timestamps" do
expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
@@ -302,24 +266,6 @@ describe Journable::WithHistoricAttributes do
expect(subject.last.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 2"
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" }
-
- it "determines for each timestamp whether the journables matches the query at that timestamp" do
- expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
- expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
- end
- end
-
describe "with include_only_changed_attributes: true" do
let(:include_only_changed_attributes) { true }
@@ -336,81 +282,100 @@ describe Journable::WithHistoricAttributes do
describe "when requesting only historic data" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- it "provides access to the historic work-package attributes" do
- expect(subject.map(&:subject)).to eq ["The original work package 1", "The original work package 2"]
- end
-
it "provides access to the historic work-package attributes at timestamps" do
expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
end
-
- it "determines whether the journable attributes are historic" do
- expect(subject).to all be_historic
- end
end
end
+ end
- context "with an AR of work packages" do
- let(:work_packages) { WorkPackage.all }
-
- it "returns an array of Journable::WithHistoricAttributes instances" do
- expect(subject).to all be_a described_class
- end
-
- it "provides access to the work-package attributes" do
- expect(subject.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
- end
+ describe '#exists_at_timestamps' do
+ context "with a single work package" do
+ let(:work_packages) { work_package1 }
- it "provides access to the work-package attributes at timestamps" do
- expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
- expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
- expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
- expect(subject.last.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 2"
+ 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
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(:query) { build_query }
let(:search_term) { "original" }
- it "determines for each timestamp whether the journables matches the query at that timestamp" do
- expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
- expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
- expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ 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 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")
+ 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.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ 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")
end
+ 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_by_timestamp["PT0S"].subject).to be_nil
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
+ it "includes the timestamp in the exists_at_timestamps array" do
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
end
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 "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")
+ end
+ end
+ end
+ end
+
+ describe '#historic?' do
+ context "with a single work package" do
+ let(:work_packages) { work_package1 }
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject.historic?).to be false
+ end
+
describe "when requesting only historic data" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
- it "provides access to the historic work-package attributes" do
- expect(subject.map(&:subject)).to eq ["The original work package 1", "The original work package 2"]
+ it "determines whether the journable attributes are historic" do
+ expect(subject.historic?).to be true
end
+ end
+ end
- it "provides access to the historic work-package attributes at timestamps" do
- expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
- expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ context "with an array of work packages" do
+ let(:work_packages) { [work_package1, work_package2] }
+
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject).to all be_historic
end
+ end
+ end
+
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
+
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
it "determines whether the journable attributes are historic" do
expect(subject).to all be_historic
@@ -419,8 +384,59 @@ describe Journable::WithHistoricAttributes do
end
end
+ describe '#matches_query_filters_at_timestamps' do
+ let(:query) { build_query }
+ let(:search_term) { "original" }
+
+ context "with a single work package" do
+ let(:work_packages) { work_package1 }
+
+ describe "when providing a query" do
+ 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")
+ 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")] }
+ 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
+ end
+ end
+ end
+
+ context "with an array of work packages" do
+ let(:work_packages) { [work_package1, work_package2] }
+
+ describe "when providing a query" do
+ it "determines for each timestamp whether the journables matches the query at that timestamp" do
+ expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ end
+ end
+ end
+
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
+
+ describe "when providing a query" do
+ it "determines for each timestamp whether the journables matches the query at that timestamp" do
+ expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ end
+ end
+ end
+ end
+
describe "#baseline_timestamp" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.baseline_timestamp }
@@ -431,7 +447,6 @@ describe Journable::WithHistoricAttributes do
end
describe "#baseline_attributes" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.baseline_attributes }
@@ -439,18 +454,19 @@ describe Journable::WithHistoricAttributes do
it "provides access to the work-package attributes at the baseline timestamp" do
expect(subject.subject).to eq "The original work package 1"
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 "has no baseline attributes" do
+ expect(subject).to be_nil
+ end
+ end
end
describe "#matches_query_filters_at_baseline_timestamp?" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package1, timestamps:, query:) }
- 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(:query) { build_query }
subject { journable.matches_query_filters_at_baseline_timestamp? }
@@ -472,7 +488,6 @@ describe Journable::WithHistoricAttributes do
end
describe "#current_timestamp" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.current_timestamp }
@@ -482,39 +497,7 @@ describe Journable::WithHistoricAttributes do
end
end
- describe "#matches_query_filters_at_current_timestamp?" do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package1, timestamps:, query:) }
- 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
-
- subject { journable.matches_query_filters_at_current_timestamp? }
-
- describe "providing a filter that matches at the baseline timestamp" do
- let(:search_term) { "original" }
-
- it "determines whether the journable matches the query at the current timestamp" do
- expect(subject).to be false
- end
- end
-
- describe "providing a filter that matches at the current timestamp" do
- let(:search_term) { "current" }
-
- it "determines whether the journable matches the query at the current timestamp" do
- expect(subject).to be true
- end
- end
- end
-
describe '.changed_at_timestamp' do
- let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
-
subject { described_class.wrap(work_package1, timestamps:) }
context 'for a timestamp where the work package did exist' do