diff --git a/app/models/poll.rb b/app/models/poll.rb index 58ad1b2eb..9087b64d0 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -152,8 +152,12 @@ class Poll < ApplicationRecord end def start_date_change - if will_save_change_to_starts_at? && starts_at < Time.current - errors.add(:starts_at, I18n.t("errors.messages.past_date")) + if will_save_change_to_starts_at? + if starts_at_in_database < Time.current + errors.add(:starts_at, I18n.t("errors.messages.cannot_change_date.poll_started")) + elsif starts_at < Time.current + errors.add(:starts_at, I18n.t("errors.messages.past_date")) + end end end diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index ea032e72f..122175846 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -138,6 +138,8 @@ en: user_not_found: User not found invalid_date_range: "Invalid date range" past_date: "Must not be a past date" + cannot_change_date: + poll_started: "Cannot be changed if voting has already started" 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 e1c01bc8d..b20e7a22b 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -138,6 +138,8 @@ es: user_not_found: Usuario no encontrado invalid_date_range: "El rango de fechas no es válido" past_date: "No puede ser una fecha pasada" + cannot_change_date: + poll_started: "No puede ser cambiada si la votación ha comenzado" 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 8b2224364..daa30aca1 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -62,6 +62,13 @@ describe Poll do poll.starts_at = 1.minute.ago expect(poll).not_to be_valid end + + it "is not valid if changing the start date for an already started poll" do + poll = create(:poll, starts_at: 10.days.ago) + + poll.starts_at = 10.days.from_now + expect(poll).not_to be_valid + end end end diff --git a/spec/system/admin/poll/polls_spec.rb b/spec/system/admin/poll/polls_spec.rb index b9f6df575..202c6b394 100644 --- a/spec/system/admin/poll/polls_spec.rb +++ b/spec/system/admin/poll/polls_spec.rb @@ -553,7 +553,7 @@ describe "Admin polls", :admin do end scenario "edit poll with sdg related list" do - poll = create(:poll, name: "Upcoming poll with SDG related content") + poll = create(:poll, :future, name: "Upcoming poll with SDG related content") poll.sdg_goals = [SDG::Goal[1], SDG::Goal[17]] visit edit_admin_poll_path(poll) diff --git a/spec/system/admin/translatable_spec.rb b/spec/system/admin/translatable_spec.rb index 09602ae16..1dab5cbbd 100644 --- a/spec/system/admin/translatable_spec.rb +++ b/spec/system/admin/translatable_spec.rb @@ -242,7 +242,7 @@ describe "Admin edit translatable records", :admin do end context "Change value of a translated field to blank" do - let(:translatable) { create(:poll) } + let(:translatable) { create(:poll, :future) } let(:path) { edit_admin_poll_path(translatable) } scenario "Updates the field to a blank value" do