diff --git a/app/models/article.rb b/app/models/article.rb index a87702346..f0e36cc05 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -790,10 +790,11 @@ class Article < ApplicationRecord def correct_published_at? return unless changes["published_at"] - # for drafts (that were never published before) or scheduled articles => allow future or current dates + # for drafts (that were never published before) or scheduled articles + # => allow future or current dates, or no published_at if !published_at_was || published_at_was > Time.current # for articles published_from_feed (exported from rss) we allow past published_at - if (!published_at || published_at < 15.minutes.ago) && !published_from_feed + if (published_at && published_at < 15.minutes.ago) && !published_from_feed errors.add(:published_at, I18n.t("models.article.future_or_current_published_at")) end else diff --git a/config/locales/models/en.yml b/config/locales/models/en.yml index fda103670..b08c8a8bf 100644 --- a/config/locales/models/en.yml +++ b/config/locales/models/en.yml @@ -21,7 +21,8 @@ en: must_not_have_spaces: must not have spaces published: Published unique_url: has already been taken. Email %{email} for further details. - future_or_current_published_at: only future or current published_at allowed when publishing an article + future_or_current_published_at: only future or current published_at allowed + immutable_published_at: updating published_at for articles that have already been published is not allowed mention_too_many: one: You cannot mention more than %{count} users in a post! diff --git a/config/locales/models/fr.yml b/config/locales/models/fr.yml index 245f290c1..a82e7c717 100644 --- a/config/locales/models/fr.yml +++ b/config/locales/models/fr.yml @@ -21,7 +21,7 @@ fr: must_not_have_spaces: must not have spaces published: Published unique_url: has already been taken. Email %{email} for further details. - future_or_current_published_at: only future or current published_at allowed when publishing an article + future_or_current_published_at: only future or current published_at allowed immutable_published_at: updating published_at for articles that have already been published is not allowed mention_too_many: one: You cannot mention more than %{count} users in a post! diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index e1e7dc410..6ee39950c 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -514,14 +514,14 @@ RSpec.describe Article, type: :model do article2 = build(:article, published_at: 10.days.ago, published: true) expect(article2.valid?).to be false expect(article2.errors[:published_at]) - .to include("only future or current published_at allowed when publishing an article") + .to include("only future or current published_at allowed") end it "doesn't allow recent published_at when publishing on create" do article2 = build(:article, published_at: 1.hour.ago, published: true) expect(article2.valid?).to be false expect(article2.errors[:published_at]) - .to include("only future or current published_at allowed when publishing an article") + .to include("only future or current published_at allowed") end it "allows recent published_at when publishing on create" do @@ -529,6 +529,12 @@ RSpec.describe Article, type: :model do expect(article2.valid?).to be true end + it "allows removing published_at when updating a scheduled draft" do + scheduled_draft = create(:article, published: false, published_at: 1.day.from_now) + scheduled_draft.published_at = nil + expect(scheduled_draft).to be_valid + end + context "when unpublishing" do let!(:published_at_was) { article.published_at }