Allow to modify the end date as long as it is not to the past

This commit is contained in:
Julian Herrero
2022-09-14 17:54:49 +02:00
committed by Javi Martín
parent 471096c698
commit 8b5c315eb0
2 changed files with 19 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ class Poll < ApplicationRecord
validate :date_range
validate :start_date_is_not_past_date, on: :create
validate :start_date_change, on: :update
validate :end_date_is_not_past_date, on: :update
validate :only_one_active, unless: :public?
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
@@ -161,6 +162,12 @@ class Poll < ApplicationRecord
end
end
def end_date_is_not_past_date
if will_save_change_to_ends_at? && ends_at < Time.current
errors.add(:ends_at, I18n.t("errors.messages.past_date"))
end
end
def generate_slug?
slug.nil?
end

View File

@@ -69,6 +69,18 @@ describe Poll do
poll.starts_at = 10.days.from_now
expect(poll).not_to be_valid
end
it "is valid if changing the end date to a future date" do
poll.ends_at = 1.day.from_now
expect(poll).to be_valid
end
it "is not valid if changing the end date to a past date" do
poll = create(:poll, starts_at: 10.days.ago, ends_at: 10.days.from_now)
poll.ends_at = 1.day.ago
expect(poll).not_to be_valid
end
end
end