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

enable Timestamp to be used as a hash key

ulfertsc5ccdc6cdc4b
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index ce0298474dc..1e45492d0b1 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -140,6 +140,8 @@ class Timestamp
self != Timestamp.now
end
+ delegate :hash, to: :iso8601
+
class Exception < StandardError; end
# rubocop:disable Metrics/AbcSize
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index 4c21999634a..a6c23e7aeae 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -374,6 +374,40 @@ describe Timestamp do
end
end
+ describe "#hash" do
+ # rubocop:disable RSpec/IdenticalEqualityAssertion
+ context 'for two instance of relative time representing the same point in time' do
+ it 'is eql' do
+ expect(described_class.new("PT0S").hash)
+ .to eql described_class.new("PT0S").hash
+ end
+ end
+
+ context 'for two instance of relative time representing different points in time' do
+ it 'is different' do
+ expect(described_class.new("PT0S").hash)
+ .not_to eql described_class.new("PT10S").hash
+ end
+ end
+
+ context 'for two instance of absolute time representing the same point in time' do
+ let(:time) { Time.zone.now }
+
+ it 'is equal' do
+ expect(described_class.new(time).hash)
+ .to eql described_class.new(time).hash
+ end
+ end
+
+ context 'for two instance of absolute time representing different points in time' do
+ it 'is different' do
+ expect(described_class.new(10.seconds.ago).hash)
+ .not_to eql described_class.new(5.seconds.ago).hash
+ end
+ end
+ # rubocop:enable RSpec/IdenticalEqualityAssertion
+ end
+
describe "passing a described_class to a where clause" do
subject { Query.where("updated_at < ?", timestamp) }