diff --git a/app/models/article.rb b/app/models/article.rb index a4d6a2082..45986717b 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -45,6 +45,7 @@ class Article < ApplicationRecord validate :validate_video validate :validate_collection_permission validate :validate_liquid_tag_permissions + validate :past_or_present_date validates :video_state, inclusion: { in: %w[PROGRESSING COMPLETED] }, allow_nil: true validates :cached_tag_list, length: { maximum: 126 } validates :main_image, url: { allow_blank: true, schemes: %w[https http] } @@ -497,7 +498,7 @@ class Article < ApplicationRecord remove_tag_adjustments_from_tag_list end self.published = front_matter["published"] if %w[true false].include?(front_matter["published"].to_s) - self.published_at = parsed_date(front_matter["date"]) if published + self.published_at = parse_date(front_matter["date"]) if published self.main_image = front_matter["cover_image"] if front_matter["cover_image"].present? self.canonical_url = front_matter["canonical_url"] if front_matter["canonical_url"].present? self.description = front_matter["description"] || description || "#{body_text[0..80]}..." @@ -506,14 +507,9 @@ class Article < ApplicationRecord self.automatically_renew = front_matter["automatically_renew"] if front_matter["automatically_renew"].present? && tag_list.include?("hiring") end - def parsed_date(date) - now = Time.current - return published_at || now unless date - - error_msg = "must be entered in DD/MM/YYYY format with current or past date" - return errors.add(:date_time, error_msg) if date > now - - date + def parse_date(date) + # once published_at exist, it can not be adjusted + published_at || date || Time.current end def validate_tag @@ -543,6 +539,12 @@ class Article < ApplicationRecord errors.add(:collection_id, "must be one you have permission to post to") if collection && collection.user_id != user_id end + def past_or_present_date + if published_at && published_at > Time.current + errors.add(:date_time, "must be entered in DD/MM/YYYY format with current or past date") + end + end + # Admin only beta tags etc. def validate_liquid_tag_permissions errors.add(:body_markdown, "must only use permitted tags") if liquid_tags_used.include?(PollTag) && !(user.has_role?(:super_admin) || user.has_role?(:admin)) diff --git a/app/services/rss_reader.rb b/app/services/rss_reader.rb index ebb90d393..37a6a847b 100644 --- a/app/services/rss_reader.rb +++ b/app/services/rss_reader.rb @@ -73,7 +73,6 @@ class RssReader article = Article.create!( feed_source_url: feed_source_url, user_id: user.id, - published_at: item.published, published_from_feed: true, show_comments: true, body_markdown: RssReader::Assembler.call(item, user, feed, feed_source_url), diff --git a/app/services/rss_reader/assembler.rb b/app/services/rss_reader/assembler.rb index 71e3ce7f0..6b8275c21 100644 --- a/app/services/rss_reader/assembler.rb +++ b/app/services/rss_reader/assembler.rb @@ -18,6 +18,7 @@ class RssReader --- title: #{@title} published: false + date: #{@item.published} tags: #{get_tags} canonical_url: #{@user.feed_mark_canonical ? @feed_source_url : ''} --- diff --git a/spec/fixtures/approvals/rssreader/get_all_articles/parses_correctly.approved.txt b/spec/fixtures/approvals/rssreader/get_all_articles/parses_correctly.approved.txt index bc25d2603..6f6ec1d58 100644 --- a/spec/fixtures/approvals/rssreader/get_all_articles/parses_correctly.approved.txt +++ b/spec/fixtures/approvals/rssreader/get_all_articles/parses_correctly.approved.txt @@ -1,6 +1,7 @@ --- title: Testing RSS Feed published: false +date: 2018-01-02 19:06:30 UTC tags: test canonical_url: --- diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 53f7d09d7..4284d2a92 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -40,7 +40,11 @@ RSpec.describe Article, type: :model do end it "reject future dates" do - expect(build(:article, with_date: true, date: "01/01/2020").valid?).to be(false) + expect(build(:article, with_date: true, date: Date.tomorrow).valid?).to be(false) + end + + it "reject future dates even when it's published at" do + expect(build(:article, published_at: Date.tomorrow).valid?).to be(false) end it "has proper username" do