diff --git a/app/models/poll.rb b/app/models/poll.rb index abaaf5d2d..eafa7d14c 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -40,6 +40,7 @@ class Poll < ApplicationRecord 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 :end_date_change, on: :update validate :only_one_active, unless: :public? accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true @@ -168,6 +169,12 @@ class Poll < ApplicationRecord 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? slug.nil? end diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 122175846..4a70c1a3e 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -140,6 +140,7 @@ en: past_date: "Must not be a past date" cannot_change_date: poll_started: "Cannot be changed if voting has already started" + poll_ended: "Cannot be changed if voting has already ended" form: 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 diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index b20e7a22b..d731f4bd4 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -140,6 +140,7 @@ es: past_date: "No puede ser una fecha pasada" cannot_change_date: 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: accept_terms: Acepto la %{policy} y las %{conditions} accept_terms_title: Acepto la Política de privacidad y las Condiciones de uso diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index ca79d47ff..00f97602e 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -70,7 +70,7 @@ describe Poll do expect(poll).not_to be_valid 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 expect(poll).to be_valid end @@ -81,6 +81,20 @@ describe Poll do poll.ends_at = 1.day.ago expect(poll).not_to be_valid 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