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
apply rubocop corrections
Sebastian Fiedlschusterf8b3039b4373
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex dc9ca0bf685..611d598a039 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -1,56 +1,59 @@class Timestamp-def initialize(arg = Timestamp.now.to_s)- if arg.kind_of? String+ if arg.is_a? String@timestamp_iso8601_string = argelsif arg.respond_to? :iso8601@timestamp_iso8601_string = arg.iso8601else- raise Timestamp::Exception, "Argument type not supported. Please provide a String or anything that responds to :iso8601, e.g. a Time."+ raise Timestamp::Exception, \+ "Argument type not supported. " \+ "Please provide an ISO-8601 String or anything that responds to :iso8601, e.g. a Time."endend-+def self.parse(iso8601_string)if iso8601_string.start_with? "P" # ISO8601 "Period"ActiveSupport::Duration.parse(iso8601_string)else- Time.parse(iso8601_string)+ if Time.zone.parse(iso8601_string).blank?+ raise ArgumentError, "The string \"#{iso8601_string}\" cannot be parsed to a Time."+ endendTimestamp.new(iso8601_string)end-+def self.now- self.new(ActiveSupport::Duration.build(0).iso8601)+ new(ActiveSupport::Duration.build(0).iso8601)end-+def relative?to_s.first == "P" # ISO8601 "Period"end-+def to_siso8601end-+def to_strto_send-+def iso8601@timestamp_iso8601_string.to_send-+def inspect- "#<Timestamp \"#{to_s}\">"+ "#<Timestamp \"#{iso8601}\">"end-+def to_timeif relative?- Time.zone.now - to_duration * (to_duration.to_i.positive? ? 1 : -1)+ Time.zone.now - (to_duration * (to_duration.to_i.positive? ? 1 : -1))else- Time.parse(self)+ Time.zone.parse(self)endend-+def to_durationif relative?ActiveSupport::Duration.parse(self)@@ -58,18 +61,18 @@ class Timestampraise Timestamp::Exception, "This timestamp is absolute and cannot be represented as ActiveSupport::Duration."endend-+def as_jsonto_send-- def to_json++ def to_json(*_args)to_send-- def ==(other_timestamp)- self.iso8601 == other_timestamp.iso8601++ def ==(other)+ iso8601 == other.iso8601end-+class Exception < StandardError; end-end\ No newline at end of file+enddiff --git a/spec/models/query/results_filter_on_historic_data_spec.rb b/spec/models/query/results_filter_on_historic_data_spec.rbindex 6148d189ac3..c3873a64076 100644--- a/spec/models/query/results_filter_on_historic_data_spec.rb+++ b/spec/models/query/results_filter_on_historic_data_spec.rb@@ -28,12 +28,13 @@require 'spec_helper'-describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: false do+describe ::Query::Results, 'Filter on historic data', with_mail: false dolet(:historic_time) { "2022-08-01".to_datetime }let(:pre_historic_time) { historic_time - 1.day }let(:recent_time) { 1.hour.ago }let!(:work_package) do- new_work_package = create :work_package, description: "This is the original description of the work package", project: project_1+ new_work_package = create(:work_package, description: "This is the original description of the work package",+ project: project1)new_work_package.update_columns created_at: historic_timenew_work_package.journals.first.update_columns created_at: historic_time, updated_at: historic_timenew_work_package.reload@@ -43,16 +44,15 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fnew_work_packageend- let(:project_1) { create :project }- let(:user_1) do+ let(:project1) { create(:project) }+ let(:user1) docreate(:user,firstname: 'user',lastname: '1',- member_in_project: project_1,- member_with_permissions: [:view_work_packages, :view_file_links])+ member_in_project: project1,+ member_with_permissions: %i[view_work_packages view_file_links])end-describe "[prelims]" dospecify "the work package has a journal entry with the historic description" doexpect(work_package.journals.count).to eq 2@@ -66,17 +66,19 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "#work_packages" dolet(:query) do- login_as(user_1)- build(:query, user: user_1, project: nil).tap do |query|+ login_as(user1)+ build(:query, user: user1, project: nil).tap do |query|query.filters.clearquery.add_filter 'description', '~', search_termendendlet(:results) { query.results }+subject { results.work_packages }describe "filter for description containing 'current'" dolet(:search_term) { 'current' }+it "includes the work package matching today" doexpect(subject).to include work_packageend@@ -84,6 +86,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'original'" dolet(:search_term) { 'original' }+it "does not include the work package matching only in the past" doexpect(subject).not_to include work_packageend@@ -94,6 +97,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'current'" dolet(:search_term) { 'current' }+it "includes the work package matching today" doexpect(subject).to include work_packageend@@ -101,6 +105,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'original'" dolet(:search_term) { 'original' }+it "includes the work package matching in the past" doexpect(subject).to include work_packageend@@ -112,6 +117,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'current'" dolet(:search_term) { 'current' }+it "does not include the work package matching only in the past" doexpect(subject).not_to include work_packageend@@ -143,6 +149,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'current'" dolet(:search_term) { 'current' }+it "does not include the work package matching only in the past" doexpect(subject).not_to include work_packageend@@ -150,6 +157,7 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "filter for description containing 'original'" dolet(:search_term) { 'original' }+it "does not include the work package because it does not exist yet at that time" doexpect(subject).not_to include work_packageend@@ -159,21 +167,21 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fdescribe "when filtering for file links" do# https://github.com/opf/openproject/pull/11678#issuecomment-1326171087- let(:storage_1) { create(:storage, creator: user_1) }- let(:file_link_1) { create(:file_link, creator: user_1, container: work_package, storage: storage_1) }- let(:project_storage_1) { create(:project_storage, project: project_1, storage: storage_1) }- before do- project_storage_1- file_link_1- end-+ let(:storage1) { create(:storage, creator: user1) }let(:query) do- login_as(user_1)- build(:query, user: user_1, project: nil).tap do |query|+ login_as(user1)+ build(:query, user: user1, project: nil).tap do |query|query.filters.clear- query.add_filter 'file_link_origin_id', '=', [file_link_1.origin_id.to_s]+ query.add_filter 'file_link_origin_id', '=', [file_link1.origin_id.to_s]endend+ let(:file_link1) { create(:file_link, creator: user1, container: work_package, storage: storage1) }+ let(:project_storage1) { create(:project_storage, project: project1, storage: storage1) }++ before do+ project_storage1+ file_link1+ endit "includes the work package" doexpect(subject).to include work_package@@ -184,15 +192,16 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fenddescribe "when having second reference to the same external file" do- let(:storage_2) { create(:storage, creator: user_1) }- let(:file_link_2) { create(:file_link, creator: user_1, container: work_package, storage: storage_2) }- let(:project_storage_2) { create(:project_storage, project: project_2, storage: storage_2) }- let(:project_2) { create :project }- let(:file_link_2) { create(:file_link, creator: user_1, container: work_package, storage: storage_2, origin_id: file_link_1.origin_id) }+ let(:storage2) { create(:storage, creator: user1) }+ let(:project_storage2) { create(:project_storage, project: project2, storage: storage2) }+ let(:project2) { create(:project) }+ let(:file_link2) do+ create(:file_link, creator: user1, container: work_package, storage: storage2, origin_id: file_link1.origin_id)+ endbefore do- project_storage_2- file_link_2+ project_storage2+ file_link2endit "includes the work package" do@@ -204,6 +213,5 @@ describe ::Query::Results, 'Filter on historic data', type: :model, with_mail: fendendend-endenddiff --git a/spec/models/query/results_sort_intergration_spec.rb b/spec/models/query/results_sort_intergration_spec.rbindex 84118e6a057..f4f51d00779 100644--- a/spec/models/query/results_sort_intergration_spec.rb+++ b/spec/models/query/results_sort_intergration_spec.rb@@ -264,6 +264,7 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false dolet(:work_package1) { create(:work_package, project: project1) }let(:work_package2) { create(:work_package, project: project2) }let(:work_package3) { create(:work_package, project: project3) }+before { [work_package1, work_package2, work_package3] }context 'when ascending' do@@ -298,6 +299,7 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false dolet(:work_package1) { create(:work_package, project: project1, category: category1) }let(:work_package2) { create(:work_package, project: project1, category: category2) }let(:work_package3) { create(:work_package, project: project1, category: category3) }+before { [work_package1, work_package2, work_package3] }context 'when ascending' do@@ -332,6 +334,7 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false dolet(:work_package1) { create(:work_package, project: project1, subject: 'WorkPackage A') }let(:work_package2) { create(:work_package, project: project1, subject: 'WorkPackage b') }let(:work_package3) { create(:work_package, project: project1, subject: 'WorkPackage C') }+before { [work_package1, work_package2, work_package3] }context 'when ascending' do@@ -366,6 +369,7 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false dolet(:work_package1) { create(:work_package, project: project1, due_date: 3.days.ago) }let(:work_package2) { create(:work_package, project: project1, due_date: 2.days.ago) }let(:work_package3) { create(:work_package, project: project1, due_date: 1.day.ago) }+before { [work_package1, work_package2, work_package3] }context 'when ascending' do@@ -418,9 +422,10 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false docustomized: work_package3,value: 'String C')end- before { [work_package1, work_package2, work_package3] }before do+ [work_package1, work_package2, work_package3]+work_package1.project.work_package_custom_fields << string_cfwork_package1.type.custom_fields << string_cf@@ -478,9 +483,10 @@ describe ::Query::Results, 'sorting and grouping', with_mail: false docustomized: work_package3,value: 3)end- before { [work_package1, work_package2, work_package3] }before do+ [work_package1, work_package2, work_package3]+work_package1.project.work_package_custom_fields << int_cfwork_package1.type.custom_fields << int_cfdiff --git a/spec/models/query/timestamps_spec.rb b/spec/models/query/timestamps_spec.rbindex 1932a876389..29767ea9f91 100644--- a/spec/models/query/timestamps_spec.rb+++ b/spec/models/query/timestamps_spec.rb@@ -28,72 +28,75 @@require 'spec_helper'-describe Query::Timestamps, type: :model do-+describe Query::Timestamps dodescribe "#timestamps" dosubject { query.timestamps }+describe "after setting timestamps to an array of ISO8601 Strings" dolet(:query) { Query.new }+before { query.timestamps = ["P-50Y", "2022-10-29T23:01:23Z"] }-+it "returns an Array of Timestamp objects" do- expect(subject).to be_kind_of Array+ expect(subject).to be_a Arrayexpect(subject.map(&:class).uniq).to eq [Timestamp]end-+it "remembers which timestamp encodes a relative time" do- expect(subject.first.relative?).to eq true- expect(subject.last.relative?).to eq false+ expect(subject.first.relative?).to be true+ expect(subject.last.relative?).to be falseendend-+describe "after setting timestamps to an array of Times" dolet(:query) { Query.new }+before { query.timestamps = [50.years.ago, Time.zone.now] }it "returns an Array of Timestamp objects" do- expect(subject).to be_kind_of Array+ expect(subject).to be_a Arrayexpect(subject.map(&:class).uniq).to eq [Timestamp]endend-+describe "when not present" dolet(:query) { Query.new }+before { query.timestamps = [] }-+it "still returns the timestamp corresponding to the present time" doexpect(subject).to eq [Timestamp.now]endend-+describe "[persistence] when saving timestamps to a record" do- let(:query) { create :query }+ let(:query) { create(:query) }+before doquery.timestamps = ["P-50Y", "2022-10-29T23:01:23Z"]query.save!end-+describe "after reloading the record from the database" dolet(:reloaded_query) { Query.find(query.id) }+subject { reloaded_query.timestamps }-+it "returns an Array of Timestamp objects" do- expect(subject).to be_kind_of Array+ expect(subject).to be_a Arrayexpect(subject.map(&:class).uniq).to eq [Timestamp]end-+it "remembers which timestamp encodes a relative time" do- expect(subject.first.relative?).to eq true- expect(subject.last.relative?).to eq false+ expect(subject.first.relative?).to be true+ expect(subject.last.relative?).to be falseend-+it "remembers the timestamp values" doexpect(subject.first.iso8601).to eq "P-50Y"expect(subject.last.iso8601).to eq "2022-10-29T23:01:23Z"endend-endend-enddiff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rbindex af1b5aef791..b2ed5870899 100644--- a/spec/models/timestamp_spec.rb+++ b/spec/models/timestamp_spec.rb@@ -28,123 +28,131 @@require 'spec_helper'-describe Timestamp, type: :model do-+describe Timestamp dodescribe ".new" dodescribe "when calling without argument" do- subject { Timestamp.new }-+ subject { described_class.new }+it "returns Timestamp.now" do- expect(subject.to_s).to eq Timestamp.now.to_s+ expect(subject.to_s).to eq described_class.now.to_sendend-+describe "when providing an ISO8601 String" do- subject { Timestamp.new("PT10S") }-- it "returns a Timestamp" do- expect(subject).to be_kind_of Timestamp+ subject { described_class.new("PT10S") }++ it "returns a described_class" do+ expect(subject).to be_a described_classend-+specify "the argument is retrievable via to_s" doexpect(subject.to_s).to eq "PT10S"endend-+describe "when providing a Time" dolet(:time) { Time.zone.now }- subject { Timestamp.new(time) }-- it "returns an absolute Timestamp representing that time (up to full seconds)" do- expect(subject).to be_kind_of Timestamp- expect(subject.relative?).to eq false- expect(subject.to_time).to eq Time.at(time.to_i)++ subject { described_class.new(time) }++ it "returns an absolute described_class representing that time (up to full seconds)" do+ expect(subject).to be_a described_class+ expect(subject.relative?).to be false+ expect(subject.to_time).to eq Time.zone.at(time.to_i)endend-+describe "when providing a non-supported object" do- subject { Timestamp.new(:foo) }-+ subject { described_class.new(:foo) }+it "raises an error" do- expect { subject }.to raise_error Timestamp::Exception+ expect { subject }.to raise_error described_class::Exceptionendendend-+describe ".now" do- subject { Timestamp.now }-- it "returns a Timestamp" do- expect(subject).to be_kind_of Timestamp+ subject { described_class.now }++ it "returns a described_class" do+ expect(subject).to be_a described_classend-+it "returns a relative timestamp" do- expect(subject.relative?).to eq true+ expect(subject.relative?).to be trueend-+it "corresponds to a duration of 0 seconds (ago)" doexpect(subject.to_duration).to eq ActiveSupport::Duration.build(0)endend-+describe ".parse" dodescribe "when providing a valid ISO8601 duration" do- subject { Timestamp.parse("PT10S") }-- it "returns a Timestamp representing a time ago that duration" do- expect(subject).to be_kind_of Timestamp+ subject { described_class.parse("PT10S") }++ it "returns a described_class representing a time ago that duration" do+ expect(subject).to be_a described_classexpect(subject.to_s).to eq "PT10S"expect(subject.to_duration).to eq ActiveSupport::Duration.build(10)- expect(subject.relative?).to eq true+ expect(subject.relative?).to be trueendend-+describe "when providing a valid ISO8601 time" do- subject { Timestamp.parse("2022-10-29T21:55:58Z") }-- it "returns a Timestamp representing that absolute time" do- expect(subject).to be_kind_of Timestamp+ subject { described_class.parse("2022-10-29T21:55:58Z") }++ it "returns a described_class representing that absolute time" do+ expect(subject).to be_a described_classexpect(subject.to_s).to eq "2022-10-29T21:55:58Z"- expect(subject.to_time).to eq Time.parse("2022-10-29T21:55:58Z")- expect(subject.relative?).to eq false+ expect(subject.to_time).to eq Time.zone.parse("2022-10-29T21:55:58Z")+ expect(subject.relative?).to be falseendend-+describe "when providing something invalid" do- subject { Timestamp.parse("foo") }-+ subject { described_class.parse("foo") }+it "raises an error" doexpect { subject }.to raise_error ArgumentErrorendendend-+describe "#relative?" do- subject { timestamp.relative?}+ subject { timestamp.relative? }+describe "for a timestamp representing an absolute time" do- let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }+ let(:timestamp) { described_class.new(1.year.ago) }+it "returns false" do- expect(subject).to eq false+ expect(subject).to be falseendend+describe "for a timestamp representing a point in time relative to now" do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }+it "returns true" do- expect(subject).to eq true+ expect(subject).to be trueendendend-+describe "#iso8601" dosubject { timestamp.iso8601 }+describe "for a timestamp representing an absolute time" do- let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }+ let(:timestamp) { described_class.new(1.year.ago) }+it "returns an ISO8601 String representing that time" doexpect(subject).to eq timestamp.to_time.iso8601endend+describe "for a timestamp representing a point in time relative to now" do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }+it "returns an ISO8601 String representing the duration between that time and now" doexpect(subject).to eq "PT10S"expect(subject).to eq ActiveSupport::Duration.build(10).iso8601@@ -154,14 +162,18 @@ describe Timestamp, type: :model dodescribe "#to_s" dosubject { timestamp.to_s }+describe "for a timestamp representing an absolute time" do- let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }+ let(:timestamp) { described_class.new(1.year.ago) }+it "returns an ISO8601 String representing that time" doexpect(subject).to eq timestamp.to_time.iso8601endend+describe "for a timestamp representing a point in time relative to now" do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }+it "returns an ISO8601 String representing the duration between that time and now" doexpect(subject).to eq "PT10S"expect(subject).to eq ActiveSupport::Duration.build(10).iso8601@@ -171,86 +183,98 @@ describe Timestamp, type: :model dodescribe "#to_json" dosubject { timestamp.to_json }+describe "for a timestamp representing an absolute time" do- let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }+ let(:timestamp) { described_class.new(1.year.ago) }+it "returns an ISO8601 String representing that time" doexpect(subject).to eq timestamp.to_time.iso8601endend+describe "for a timestamp representing a point in time relative to now" do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }+it "returns an ISO8601 String representing the duration between that time and now" doexpect(subject).to eq "PT10S"expect(subject).to eq ActiveSupport::Duration.build(10).iso8601endendend-+describe "#to_time" dosubject { timestamp.to_time }+describe "for a timestamp representing an absolute time" do- let(:time) { Time.zone.now - 1.year }- let(:timestamp) { Timestamp.new(time) }+ let(:time) { 1.year.ago }+ let(:timestamp) { described_class.new(time) }+it "returns a Time representing that time (up to full seconds)" do- expect(subject).to be_kind_of Time- expect(subject).to eq Time.at(time.to_i)+ expect(subject).to be_a Time+ expect(subject).to eq Time.zone.at(time.to_i)endend+describe "for a timestamp representing a point in time relative to now" doTimecop.freeze do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }it "returns a Time converting the relative time to an absolute time (relative to now at evaluation time)" do- expect(subject).to be_kind_of Time- expect(Time.at(subject.to_i)).to eq Time.at(10.seconds.ago.to_i)+ expect(subject).to be_a Time+ expect(Time.zone.at(subject.to_i)).to eq Time.zone.at(10.seconds.ago.to_i)endendend+describe "for a timestamp representing a point in time relative to now with negative duration" doTimecop.freeze do- let(:timestamp) { Timestamp.new("PT-10S") }+ let(:timestamp) { described_class.new("PT-10S") }it "returns a Time converting the relative time to an absolute time (relative to now at evaluation time)" do- expect(subject).to be_kind_of Time- expect(Time.at(subject.to_i)).to eq Time.at(10.seconds.ago.to_i)+ expect(subject).to be_a Time+ expect(Time.zone.at(subject.to_i)).to eq Time.zone.at(10.seconds.ago.to_i)endendendend-+describe "#to_duration" dosubject { timestamp.to_duration }+describe "for a timestamp representing an absolute time" do- let(:time) { Time.zone.now - 1.year }- let(:timestamp) { Timestamp.new(time) }- it "raises an error because the absolute time does not change when the current time progresses and therefore cannot be represented as constant duration" do- expect { subject }.to raise_error Timestamp::Exception+ let(:time) { 1.year.ago }+ let(:timestamp) { described_class.new(time) }++ it "raises an error because the absolute time does not change when the current time progresses " \+ "and therefore cannot be represented as constant duration" do+ expect { subject }.to raise_error described_class::Exceptionendend+describe "for a timestamp representing a point in time relative to now" doTimecop.freeze do- let(:timestamp) { Timestamp.new("PT10S") }+ let(:timestamp) { described_class.new("PT10S") }it "returns an ActiveSupport::Duration corresponding to the duration between the timestamp and now" do- expect(subject).to be_kind_of ActiveSupport::Duration+ expect(subject).to be_a ActiveSupport::Durationexpect(subject).to eq ActiveSupport::Duration.build(10)endendendend-- describe "passing a Timestamp to a where clause" do++ describe "passing a described_class to a where clause" dosubject { Query.where("updated_at < ?", timestamp) }- let(:timestamp) { Timestamp.new("PT10S") }-++ let(:timestamp) { described_class.new("PT10S") }+it "raises an error because the query interface requires a Time type" doexpect { subject }.to raise_error TypeErrorend-+describe "when converting the timestamp to_time" dosubject { Query.where("updated_at < ?", timestamp.to_time) }-+it "raises no error" doexpect { subject }.not_to raise_error TypeErrorendendend--end\ No newline at end of file+end