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
Add Project::Stage date_range setter and validation.
Dombi Attila667d1d56f821
diff --git a/app/models/project/stage.rb b/app/models/project/stage.rbindex cf9e456add5..94e29cf4fd2 100644--- a/app/models/project/stage.rb+++ b/app/models/project/stage.rb@@ -29,9 +29,22 @@class Project::Stage < Project::LifeCycleStep# This ensures the type cannot be changed after initialising the class.validates :type, inclusion: { in: %w[Project::Stage], message: :must_be_a_stage }- validates :start_date, :end_date, presence: true+ validate :validate_date_range++ def date_range=(param)+ self.start_date, self.end_date = param.split(" - ")+ self.end_date ||= start_date # Allow single dates as range+ enddef not_set?start_date.blank? || end_date.blank?end++ def validate_date_range+ if not_set?+ errors.add(:date_range, :blank)+ elsif start_date > end_date+ errors.add(:date_range, :start_date_must_be_before_end_date)+ end+ endenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 8a6d759c0e6..8999c6dbd13 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1218,7 +1218,11 @@ en:project/gate:attributes:base:- end_date_not_allowed: "Cannot assign `end_date` to a Project::Gate"+ end_date_not_allowed: "Cannot assign end date to a Project::Gate"+ project/stage:+ attributes:+ date_range:+ start_date_must_be_before_end_date: "Start date must be before the end date"query:attributes:project:diff --git a/spec/models/project/gate_spec.rb b/spec/models/project/gate_spec.rbindex f634c63c183..c0e7645e1f9 100644--- a/spec/models/project/gate_spec.rb+++ b/spec/models/project/gate_spec.rb@@ -41,7 +41,7 @@ RSpec.describe Project::Gate doexpect(subject).not_to be_validexpect(subject.errors[:base])- .to include("Cannot assign `end_date` to a Project::Gate")+ .to include("Cannot assign end date to a Project::Gate")endit "is valid if `end_date` is not present" dodiff --git a/spec/models/project/stage_spec.rb b/spec/models/project/stage_spec.rbindex 6fd727a34b2..3bd2d19cb19 100644--- a/spec/models/project/stage_spec.rb+++ b/spec/models/project/stage_spec.rb@@ -33,8 +33,6 @@ RSpec.describe Project::Stage doit_behaves_like "a Project::LifeCycleStep event"describe "validations" do- it { is_expected.to validate_presence_of(:start_date) }- it { is_expected.to validate_presence_of(:end_date) }it { is_expected.to validate_inclusion_of(:type).in_array(["Project::Stage"]).with_message(:must_be_a_stage) }it "is valid when `start_date` and `end_date` are present" do@@ -42,4 +40,58 @@ RSpec.describe Project::Stage doexpect(valid_stage).to be_validendend++ describe "#not_set?" do+ it "returns true if start_date or end_date is blank" do+ expect(subject.not_set?).to be(true)+ end++ it "returns false if both start_date and end_date are present" do+ subject.start_date = Time.zone.today+ subject.end_date = Date.tomorrow+ expect(subject.not_set?).to be(false)+ end+ end++ describe "#date_range=" do+ it "splits a valid date range string into start_date and end_date" do+ subject.date_range = "2024-11-26 - 2024-11-27"+ expect(subject.start_date).to eq(Date.parse("2024-11-26"))+ expect(subject.end_date).to eq(Date.parse("2024-11-27"))+ end++ it "sets end_date to start_date if a single date is provided" do+ subject.date_range = "2024-11-26"+ expect(subject.start_date).to eq(Date.parse("2024-11-26"))+ expect(subject.end_date).to eq(Date.parse("2024-11-26"))+ end+ end++ describe "#validate_date_range" do+ it "adds error if start_date is blank" do+ subject.end_date = Time.zone.today+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:date_range)).to include(:blank)+ end++ it "adds error if end_date is blank" do+ subject.start_date = Time.zone.today+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:date_range)).to include(:blank)+ end++ it "adds error if start_date is after end_date" do+ subject.start_date = Date.tomorrow+ subject.end_date = Time.zone.today+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:date_range)).to include(:start_date_must_be_before_end_date)+ end++ it "does not add errors if start_date is before or equal to end_date" do+ subject.start_date = Time.zone.today+ subject.end_date = Time.zone.today+ expect(subject).not_to be_valid+ expect(subject.errors[:date_range]).to be_empty+ end+ endend