Disallow to modify the start date for an already started poll

We need to update a couple of tests because a poll is created in the
tests with a timestamp that includes nanoseconds and in the form to edit
the time of the poll the nanoseconds are not sent, meaning it was
detected as a change.
This commit is contained in:
Julian Herrero
2022-09-14 16:40:27 +02:00
committed by Javi Martín
parent a774456b51
commit 471096c698
6 changed files with 19 additions and 4 deletions

View File

@@ -152,10 +152,14 @@ class Poll < ApplicationRecord
end end
def start_date_change def start_date_change
if will_save_change_to_starts_at? && starts_at < Time.current 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")) errors.add(:starts_at, I18n.t("errors.messages.past_date"))
end end
end end
end
def generate_slug? def generate_slug?
slug.nil? slug.nil?

View File

@@ -138,6 +138,8 @@ en:
user_not_found: User not found user_not_found: User not found
invalid_date_range: "Invalid date range" invalid_date_range: "Invalid date range"
past_date: "Must not be a past date" past_date: "Must not be a past date"
cannot_change_date:
poll_started: "Cannot be changed if voting has already started"
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

@@ -138,6 +138,8 @@ es:
user_not_found: Usuario no encontrado user_not_found: Usuario no encontrado
invalid_date_range: "El rango de fechas no es válido" invalid_date_range: "El rango de fechas no es válido"
past_date: "No puede ser una fecha pasada" past_date: "No puede ser una fecha pasada"
cannot_change_date:
poll_started: "No puede ser cambiada si la votación ha comenzado"
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

@@ -62,6 +62,13 @@ describe Poll do
poll.starts_at = 1.minute.ago poll.starts_at = 1.minute.ago
expect(poll).not_to be_valid expect(poll).not_to be_valid
end 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
end end

View File

@@ -553,7 +553,7 @@ describe "Admin polls", :admin do
end end
scenario "edit poll with sdg related list" do 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]] poll.sdg_goals = [SDG::Goal[1], SDG::Goal[17]]
visit edit_admin_poll_path(poll) visit edit_admin_poll_path(poll)

View File

@@ -242,7 +242,7 @@ describe "Admin edit translatable records", :admin do
end end
context "Change value of a translated field to blank" do 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) } let(:path) { edit_admin_poll_path(translatable) }
scenario "Updates the field to a blank value" do scenario "Updates the field to a blank value" do