Disallow to modify the end date for an ended poll

This commit is contained in:
Julian Herrero
2022-09-14 18:06:34 +02:00
committed by Javi Martín
parent 8b5c315eb0
commit ecd22131f1
4 changed files with 24 additions and 1 deletions

View File

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

View File

@@ -140,6 +140,7 @@ en:
past_date: "Must not be a past date" past_date: "Must not be a past date"
cannot_change_date: cannot_change_date:
poll_started: "Cannot be changed if voting has already started" poll_started: "Cannot be changed if voting has already started"
poll_ended: "Cannot be changed if voting has already ended"
form: form:
accept_terms: I agree to the %{policy} and the %{conditions} accept_terms: I agree to the %{policy} and the %{conditions}
accept_terms_title: I agree to the Privacy Policy and the Terms and conditions of use accept_terms_title: I agree to the Privacy Policy and the Terms and conditions of use

View File

@@ -140,6 +140,7 @@ es:
past_date: "No puede ser una fecha pasada" past_date: "No puede ser una fecha pasada"
cannot_change_date: cannot_change_date:
poll_started: "No puede ser cambiada si la votación ha comenzado" poll_started: "No puede ser cambiada si la votación ha comenzado"
poll_ended: "No puede ser cambiada si la votación ha acabado"
form: form:
accept_terms: Acepto la %{policy} y las %{conditions} accept_terms: Acepto la %{policy} y las %{conditions}
accept_terms_title: Acepto la Política de privacidad y las Condiciones de uso accept_terms_title: Acepto la Política de privacidad y las Condiciones de uso

View File

@@ -70,7 +70,7 @@ describe Poll do
expect(poll).not_to be_valid expect(poll).not_to be_valid
end end
it "is valid if changing the end date to a future date" do it "is valid if changing the end date for a non-expired poll to a future date" do
poll.ends_at = 1.day.from_now poll.ends_at = 1.day.from_now
expect(poll).to be_valid expect(poll).to be_valid
end end
@@ -81,6 +81,20 @@ describe Poll do
poll.ends_at = 1.day.ago poll.ends_at = 1.day.ago
expect(poll).not_to be_valid expect(poll).not_to be_valid
end end
it "is valid if the past end date is the same as it was" do
poll = create(:poll, starts_at: 3.days.ago, ends_at: 2.days.ago)
poll.ends_at = poll.ends_at
expect(poll).to be_valid
end
it "is not valid if changing the end date for an expired poll" do
poll = create(:poll, :expired)
poll.ends_at = 1.day.from_now
expect(poll).not_to be_valid
end
end end
end end