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
implementing `Journable.at_timestamp`
Sebastian Fiedlschuster142dedc9d51d
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbnew file mode 100644index 00000000000..7a58dc88ca0--- /dev/null+++ b/app/models/journable/historic_active_record_relation.rb@@ -0,0 +1,137 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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::HistoricActiveRecordRelation < ActiveRecord::Relation+ attr_accessor :timestamp++ include ActiveRecord::Delegation::ClassSpecificRelation++ def initialize(relation, timestamp:)+ raise ArgumentError, "Expected ActiveRecord::Relation" unless relation.kind_of? ActiveRecord::Relation+ relation.instance_variables.each do |key|+ self.instance_variable_set key, relation.instance_variable_get(key)+ end++ self.timestamp = timestamp+ self.readonly!+ self.instance_variable_set :@table, model.journal_class.arel_table++ return self+ end++ # We need to patch the `pluck` method of an active-record relation that+ # queries historic data (i.e. journal data). Otherwise, `pluck(:id)`+ # would return the `id` of the journal table rather than the `id` of the+ # journable table, which would be expected from the syntax:+ #+ # WorkPackage.where(assigned_to_id: 123).at_timestamp(1.year.ago).pluck(:id)+ #+ def pluck(*column_names)+ column_names.map! { |column_name| column_name == :id ? 'journals.journable_id' : column_name }+ arel+ super+ end++ # Patch the arel object, which is used to construct the sql query, in order+ # to modify the query to search for historic data.+ #+ alias_method :original_build_arel, :build_arel+ def call_original_build_arel+ original_build_arel+ end+ def build_arel(aliases = nil)+ relation = self++ # Switch the database table from `work_packages` to `work_package_journals`.+ relation.instance_variable_set :@table, model.journal_class.arel_table++ # Modify there where clauses such that the work-packages table is substituted+ # with the work-package-journals table.+ relation.where_clause.instance_variable_get(:@predicates).each do |predicate|+ if predicate.left.relation == self.arel_table+ predicate.left.relation = self.journal_class.arel_table+ end+ end++ # Add a timestamp condition: Select the work package journals that are the+ # current ones at the given timestamp.+ relation = relation \+ .joins("INNER JOIN \"journals\" ON \"journals\".\"data_type\" = '#{model.journal_class.name}' AND \"journals\".\"data_id\" = \"#{model.journal_class.table_name}\".\"id\"") \+ .merge(Journal.at_timestamp(timestamp))++ # Join the journables table itself because we need to take the 'created_at' attribute from that.+ relation = relation \+ .joins("INNER JOIN (SELECT id, created_at FROM \"#{model.table_name}\") AS journables ON \"journables\".\"id\" = \"journals\".\"journable_id\"")++ # At this point, the id is wrong. The sql statement returns the id of the work package journal,+ # but active record instantiates this into the WorkPackage model. Because the resulting model+ # will be a WorkPackage, we need a work-package id, which we take from the journals table.+ if relation.select_values.count == 0+ relation = relation.select("'#{timestamp}' as timestamp, #{model.journal_class.table_name}.*, journals.journable_id as id, journables.created_at as created_at, journals.created_at as updated_at")+ end++ # Based on the previous modifications, build the algebra object.+ @arel = relation.call_original_build_arel++ # Modify order clauses to use the work-pacakge-journals table.+ @arel.instance_variable_get(:@ast).instance_variable_get(:@orders).each do |order_clause|+ if order_clause.expr.relation == model.arel_table+ order_clause.expr.relation = model.journal_class.arel_table+ end+ end++ # Move the journals join to the beginning because other joins depend on it.+ @arel.instance_variable_get(:@ast).instance_variable_get(:@cores).each do |core|+ array_of_joins = core.instance_variable_get(:@source).right+ if journals_join_index = array_of_joins.find_index { |join| join.kind_of?(Arel::Nodes::StringJoin) && join.left.include?("INNER JOIN \"journals\" ON \"journals\".\"data_type\"") }+ journals_join = array_of_joins[journals_join_index]+ array_of_joins.delete_at(journals_join_index)+ array_of_joins.insert(0, journals_join)+ end+ end++ # Modify the joins to point to the journable_id.+ @arel.instance_variable_get(:@ast).instance_variable_get(:@cores).each do |core|+ core.instance_variable_get(:@source).right.each do |node|+ if node.kind_of?(Arel::Nodes::Join) and node.right.kind_of?(Arel::Nodes::On)+ [node.right.expr.left, node.right.expr.right].each do |attribute|+ if (attribute.relation == journal_class.arel_table) and (attribute.name == "id")+ attribute.relation = Journal.arel_table+ attribute.name = "journable_id"+ end+ end+ end+ end+ end++ return @arel+ end++ # TODO: Add tests when having several work packages++end\ No newline at end of filediff --git a/app/models/journable/timestamps.rb b/app/models/journable/timestamps.rbnew file mode 100644index 00000000000..011fed2d5ea--- /dev/null+++ b/app/models/journable/timestamps.rb@@ -0,0 +1,84 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++module Journable::Timestamps+ extend ActiveSupport::Concern++ class_methods do++ # Allows to query historic data of journables.+ #+ # For example, to check which work packages were assigned to a+ # specific user one year ago, run:+ #+ # WorkPackage.where(assigned_to_id: 123).at_timestamp(1.year.ago)+ #+ def at_timestamp(timestamp)+ relation = self.all+ relation = Journable::HistoricActiveRecordRelation.new(relation, timestamp:)+ return relation+ end++ end++ # Instantiates a journable with historic data from the given timestap.+ #+ # WorkPackage.find(1).at_timestamp(1.year.ago)+ #+ def at_timestamp(timestamp)+ if journal = self.journals.at_timestamp(timestamp).first+ attributes = journal.data.attributes.merge({+ "id" => self.id,+ "created_at" => self.created_at,+ "updated_at" => journal.updated_at,+ "timestamp" => timestamp,+ "position" => (self.position if self.respond_to? :position)+ })+ journable = self.class.instantiate(attributes)+ journable.readonly!+ return journable+ end+ end++ def historic?+ attributes["timestamp"].present?+ end+ def historical?+ historic?+ end++ # Rollback a journable record to a historic state of that record.+ #+ # WorkPackage.find(2).at_timestamp(1.year.ago).rollback!+ #+ def rollback!+ raise ActiveRecord::RecordNotSaved, "This is no historic data. You can only revert to historic data." unless historic?+ self.class.find(id).update! self.attributes.except("id", "timestamp")+ end++end\ No newline at end of filediff --git a/app/models/work_package.rb b/app/models/work_package.rbindex 83580a1ca71..84c7d44769a 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -186,6 +186,7 @@ class WorkPackage < ApplicationRecordmethod(:cleanup_time_entries_before_destruction_of)include WorkPackage::Journalized+ prepend Journable::Timestampsdef self.done_ratio_disabled?Setting.work_package_done_ratio == 'disabled'diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbnew file mode 100644index 00000000000..02f5a4d20ff--- /dev/null+++ b/spec/models/journable/timestamps_spec.rb@@ -0,0 +1,494 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++require 'spec_helper'++describe Journable::Timestamps, type: :model do++ before do+ @before_monday = "2022-01-01".to_datetime+ @monday = "2022-08-01".to_datetime+ @tuesday = "2022-08-02".to_datetime+ @wednesday = "2022-08-03".to_datetime+ @thursday = "2022-08-04".to_datetime+ @friday = "2022-08-05".to_datetime+ end++ let!(:work_package) do+ new_work_package = create :work_package, description: "The work package as it is since Friday", estimated_hours: 10+ new_work_package.update_columns created_at: @monday+ new_work_package+ end+ let(:journable) { work_package }++ describe "when there are journals for Monday, Wednesday, and Friday" do+ def create_journal(journable:, timestamp:, attributes: {})+ work_package_attributes = work_package.attributes.except("id")+ journal_attributes = work_package_attributes.extract!(*Journal::WorkPackageJournal.attribute_names).symbolize_keys.merge(attributes)+ create :work_package_journal, journable:, created_at: timestamp, updated_at: timestamp,+ data: build(:journal_work_package_journal, journal_attributes)+ end++ before do+ @before_monday = "2022-01-01".to_datetime+ @monday = "2022-08-01".to_datetime+ @tuesday = "2022-08-02".to_datetime+ @wednesday = "2022-08-03".to_datetime+ @thursday = "2022-08-04".to_datetime+ @friday = "2022-08-05".to_datetime+ work_package.journals.destroy_all+ @monday_journal = create_journal(journable: work_package, timestamp: @monday, attributes: {description: "The work package as it has been on Monday", estimated_hours: 5})+ @wednesday_journal = create_journal(journable: work_package, timestamp: @wednesday, attributes: {description: "The work package as it has been on Wednesday", estimated_hours: 10})+ @friday_journal = create_journal(journable: work_package, timestamp: @friday, attributes: {description: "The work package as it is since Friday", estimated_hours: 10})+ work_package.reload+ end++ describe ".at_timestamp" do+ before { @timestamp = @monday }+ subject { WorkPackage.at_timestamp(@timestamp) }++ it "returns a historic active-record relation" do+ expect(subject).to be_kind_of Journable::HistoricActiveRecordRelation+ expect(subject).to be_kind_of ActiveRecord::Relation+ end++ describe "chaining a where clause" do+ subject { WorkPackage.at_timestamp(@timestamp).where(assigned_to_id: 1) }+ it "still returns a historic active-record relation" do+ expect(subject).to be_kind_of Journable::HistoricActiveRecordRelation+ expect(subject).to be_kind_of ActiveRecord::Relation+ end+ end++ it "returns readonly objects" do+ expect(subject.first.readonly?).to eq true+ end++ it "returns the records with the journable id rather than the id of the journal record" do+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.id).to eq work_package.id+ end++ describe ".at_timestamp(something before Monday)" do+ before { @timestamp = @before_monday }+ it "returns [] because before Monday, no journal exists" do+ expect(subject).to eq []+ end+ end+ describe ".at_timestamp(Monday)" do+ before { @timestamp = @monday }+ it "returns the work packages in their state of Monday" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end+ describe ".at_timestamp(Tuesday)" do+ before { @timestamp = @tuesday }+ it "returns the work packages in their state of Monday because this is the most current on Tuesday" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end+ describe ".at_timestamp(Wednesday)" do+ before { @timestamp = @wednesday }+ it "returns the work packages in their state of Wednesday" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Wednesday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end+ describe ".at_timestamp(Thursday)" do+ before { @timestamp = @thursday }+ it "returns the work packages in their state of Wednesday because this is the most current on Thursday" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Wednesday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end+ describe ".at_timestamp(Friday)" do+ before { @timestamp = @friday }+ it "returns the work packages in their state of Friday" do+ expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end++ describe "when filtering on a column that only matches in the past" do+ before { @relation = WorkPackage.where(description: "The work package as it has been on Monday") }+ describe "when querying the timestamp where the column matches" do+ subject { @relation.at_timestamp(@monday) }+ it "returns the matching records in their historic states" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ end+ end+ describe "when querying the timestamp where the column does not match" do+ subject { @relation.at_timestamp(@wednesday) }+ it "returns [] because nothing matches" do+ expect(subject).to eq []+ end+ end+ describe "when querying without at_timestamp" do+ subject { @relation }+ it "returns [] because nothing matches" do+ expect(subject).to eq []+ end+ end+ end++ describe "when filtering on a column that only matches in the present" do+ before { @relation = WorkPackage.where(description: "The work package as it is since Friday") }+ describe "when querying the timestamp where the column matches" do+ subject { @relation.at_timestamp(@friday) }+ it "returns the matching records in their historic states" do+ expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq true+ end+ end+ describe "when querying the timestamp where the column does not match" do+ subject { @relation.at_timestamp(@wednesday) }+ it "returns [] because nothing matches" do+ expect(subject).to eq []+ end+ end+ describe "when querying without at_timestamp" do+ subject { @relation }+ it "returns the matching records" do+ expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq false+ end+ end+ end++ describe "when filtering with a negative condition" do+ before { @relation = WorkPackage.where.not(description: "The work package as it is since Friday") }+ describe "when querying a timestamp where the column matches" do+ subject { @relation.at_timestamp(@tuesday) }+ it "returns the matching records in their historic states" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq true+ end+ end+ describe "when querying a timestamp where the column does not match" do+ subject { @relation.at_timestamp(@friday) }+ it "returns [] because nothing matches" do+ expect(subject).to eq []+ end+ end+ end++ describe "when filtering on a column of an associated table" do+ before do+ @time_entry = work_package.time_entries.create! user_id: User.first.id, hours: 1, activity_id: 1, project_id: create(:project).id, spent_on: @monday+ @relation = WorkPackage.joins(:time_entries).where(time_entries: {id: @time_entry.id})+ end+ describe "when querying with at_timestamp" do+ subject { @relation.at_timestamp(@friday) }+ it "joins the journable table rather than the journal-data table" do+ expect(subject.to_sql).not_to include "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"work_package_journals\".\"id\""+ expect(subject.to_sql).to include "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""+ end+ it "returns the matching records in their historic states" do+ expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq true+ end+ end+ describe "when querying with a past timestamp" do+ # This is a case we have to watch out. Even if the association has been created later+ # this still finds the historic record.+ subject { @relation.at_timestamp(@wednesday) }+ it "returns still matches because at_timestamp only filters the journable journal and not the associated journal" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Wednesday"]+ end+ end+ describe "when querying without at_timestamp" do+ subject { @relation }+ it "returns the matching records" do+ expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq false+ end+ end+ end++ describe "when filtering on a historic relation" do+ subject { WorkPackage.at_timestamp(@monday).where(description: @description) }+ describe "when the data on that point in history has no matches" do+ before { @description = "The work package as it is since Friday" }+ it "returns []" do+ expect(subject).to eq []+ end+ end+ describe "when the data on that point in history has matches" do+ before { @description = "The work package as it has been on Monday" }+ it "returns the matching records" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq true+ end+ end+ end++ describe "when filtering on a historic relation with a negative condition" do+ subject { WorkPackage.at_timestamp(@timestamp).where.not(description: "The work package as it is since Friday") }+ describe "when querying a timestamp where the column matches" do+ before { @timestamp = @tuesday }+ it "returns the matching records in their historic states" do+ expect(subject.pluck(:description)).to eq ["The work package as it has been on Monday"]+ expect(subject.pluck(:id)).to eq [work_package.id]+ expect(subject.first.historic?).to eq true+ end+ end+ describe "when querying a timestamp where the column does not match" do+ before { @timestamp = @friday }+ it "returns [] because nothing matches" do+ expect(subject).to eq []+ end+ end+ end++ describe "when using the calculational function 'maximum'" do+ describe "without at_timestamp" do+ subject { WorkPackage.maximum(:estimated_hours) }+ it "returns the correct result for today" do+ expect(subject).to eq 10+ end+ end+ describe "after at_timestamp" do+ subject { WorkPackage.at_timestamp(@monday).maximum(:estimated_hours) }+ it "returns the correct result for that timestamp" do+ expect(subject).to eq 5+ end+ end+ describe "before at_timestamp" do+ subject { WorkPackage.maximum(:estimated_hours).at_timestamp(@monday) }+ it "returns an error because maximum returns a float rather than a relation" do+ expect { subject }.to raise_error NoMethodError+ expect(WorkPackage.maximum(:estimated_hours)).to be_kind_of Float+ end+ end+ end++ describe "when using a GROUP BY COUNT query" do+ describe "without at_timestamp" do+ subject { WorkPackage.group(:estimated_hours).count }+ it "returns the correct result for today" do+ expect(subject).to eq({10.0 => 1})+ end+ end+ describe "after at_timestamp" do+ subject { WorkPackage.at_timestamp(@monday).group(:estimated_hours).count }+ it "returns the correct result for that timestamp" do+ expect(subject).to eq({5.0 => 1})+ end+ end+ end++ describe "when using a GROUP BY COUNT HAVING query" do+ describe "without at_timestamp" do+ subject { WorkPackage.group(:estimated_hours).having('estimated_hours > 3').count }+ it "returns the correct result for today" do+ expect(subject).to eq({10.0 => 1})+ end+ end+ describe "after at_timestamp" do+ subject { WorkPackage.at_timestamp(@monday).group(:estimated_hours).having('estimated_hours > 3').count }+ it "returns the correct result for that timestamp" do+ expect(subject).to eq({5.0 => 1})+ end+ end+ end++ describe "#created_at" do+ subject { WorkPackage.at_timestamp(@timestamp).first.created_at }+ describe "for at_timestamp(before monday)" do+ before { @timestamp = @before_monday }+ it "raises an error because no result exists for that timestamp" do+ expect { subject }.to raise_error NoMethodError+ end+ end+ describe "for at_timestamp(monday)" do+ before { @timestamp = @monday }+ it "returns the date the record itself has been created" do+ expect(subject).to eq work_package.created_at+ expect(subject).to eq @monday+ end+ end+ describe "for at_timestamp(tuesday)" do+ before { @timestamp = @tuesday }+ it "returns the date the record itself has been created" do+ expect(subject).to eq work_package.created_at+ expect(subject).to eq @monday+ end+ end+ describe "for at_timestamp(wednesday)" do+ before { @timestamp = @wednesday }+ it "returns the date the record itself has been created" do+ expect(subject).to eq work_package.created_at+ expect(subject).to eq @monday+ end+ end+ end++ describe "#updated_at" do+ subject { WorkPackage.at_timestamp(@timestamp).first.updated_at }+ describe "for at_timestamp(before monday)" do+ before { @timestamp = @before_monday }+ it "raises an error because no result exists for that timestamp" do+ expect { subject }.to raise_error NoMethodError+ end+ end+ describe "for at_timestamp(monday)" do+ before { @timestamp = @monday }+ it "returns the date of the monday journal entry" do+ expect(subject).to eq @monday+ end+ end+ describe "for at_timestamp(tuesday)" do+ before { @timestamp = @tuesday }+ it "returns the date of the monday journal entry" do+ expect(subject).to eq @monday+ end+ end+ describe "for at_timestamp(wednesday)" do+ before { @timestamp = @wednesday }+ it "returns the date of the wednesday journal entry" do+ expect(subject).to eq @wednesday+ end+ end+ end++ end++ describe "#at_timestamp" do+ subject { work_package.at_timestamp(@timestamp) }++ describe "#at_timestamp(something before monday)" do+ before { @timestamp = @before_monday}+ it "returns nil because before Monday, no journal exists" do+ expect(subject).to eq nil+ end+ end++ describe "#at_timestamp(Monday)" do+ before { @timestamp = @monday }++ it "returns a record of the same type as the journable" do+ expect(subject.class).to eq journable.class+ end++ it "has the same id as the journable" do+ expect(subject.id).to eq journable.id+ end++ it "has the same created_at attribute as the journable" do+ expect(subject.created_at).to eq journable.created_at+ end++ it "has an older updated_at attribute as the journable" do+ expect(subject.updated_at).to be < journable.updated_at+ end++ it "has the attributes from monday" do+ expect(subject.description).to eq "The work package as it has been on Monday"+ expect(journable.description).to eq "The work package as it is since Friday"+ end+ end+ end++ describe "#historic?" do+ describe "when querying historic values of a journable" do+ subject { work_package.at_timestamp(@monday).historic? }+ it "returns true" do+ expect(subject).to eq true+ end+ end++ describe "for the current journable without querying historic data" do+ subject { work_package.historic? }+ it "returns false" do+ expect(subject).to eq false+ end+ end+ end++ describe "#valid?" do+ describe "when querying historic values of a journable" do+ subject { work_package.at_timestamp(@monday).valid? }+ it "returns true" do+ expect(subject).to eq true+ end+ end+ end++ describe "#save" do+ describe "when trying to save a historic record" do+ subject do+ @work_package = work_package.at_timestamp(@monday)+ @work_package.description = "New description"+ @work_package.save+ end+ it "raises an error" do+ expect { subject }.to raise_error ActiveRecord::ReadOnlyRecord+ end+ end+ end++ describe "#update" do+ describe "when trying to update a historic record" do+ subject do+ @work_package = work_package.at_timestamp(@monday)+ @work_package.update description: "New description"+ end+ it "raises an error" do+ expect { subject }.to raise_error ActiveRecord::ReadOnlyRecord+ end+ end+ end++ describe "#rollback!" do+ describe "when trying to rollback to a current (non-historic) record" do+ subject { work_package.rollback! }+ it "raises an error" do+ expect { subject }.to raise_error ActiveRecord::RecordNotSaved+ end+ end+ describe "when rolling back to historic data" do+ subject { work_package.at_timestamp(@wednesday).rollback! }+ it "updates the journable records to its historic values" do+ expect(work_package.reload.description).to eq "The work package as it is since Friday"+ subject+ expect(work_package.reload.description).to eq "The work package as it has been on Wednesday"+ end+ end+ end++ end+end\ No newline at end of file