A platform for community discussion. Free, open, simple.
FIX: Preserve TopicCreator's timestamp resolution (#9158)
Jarek Radoszaec26ad2f01d
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rbindex 654fc716f4b..41558784f34 100644--- a/lib/topic_creator.rb+++ b/lib/topic_creator.rb@@ -118,9 +118,9 @@ class TopicCreatortopic_params[:category_id] = category.id if category.present?- topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?+ topic_params[:created_at] = convert_time(@opts[:created_at]) if @opts[:created_at].present?- topic_params[:pinned_at] = Time.zone.parse(@opts[:pinned_at].to_s) if @opts[:pinned_at].present?+ topic_params[:pinned_at] = convert_time(@opts[:pinned_at]) if @opts[:pinned_at].present?topic_params[:pinned_globally] = @opts[:pinned_globally] if @opts[:pinned_globally].present?if SiteSetting.topic_featured_link_enabled && @opts[:featured_link].present? && @guardian.can_edit_featured_link?(topic_params[:category_id])@@ -130,6 +130,14 @@ class TopicCreatortopic_paramsend+ def convert_time(timestamp)+ if timestamp.is_a?(Time)+ timestamp+ else+ Time.zone.parse(timestamp.to_s)+ end+ end+def find_category@category ||= begin# PM can't have a categorydiff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rbindex 36a8717b9ad..d13d48c3f0d 100644--- a/spec/components/topic_creator_spec.rb+++ b/spec/components/topic_creator_spec.rb@@ -3,7 +3,6 @@require 'rails_helper'describe TopicCreator do-fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }fab!(:moderator) { Fabricate(:moderator) }fab!(:admin) { Fabricate(:admin) }@@ -237,5 +236,34 @@ describe TopicCreator doendendend++ context 'setting timestamps' do+ it 'supports Time instances' do+ freeze_time++ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(+ created_at: 1.week.ago,+ pinned_at: 3.days.ago+ ))++ expect(topic.created_at).to be_within(1.second).of(1.week.ago)+ expect(topic.pinned_at).to be_within(1.second).of(3.days.ago)+ end++ it 'supports strings' do+ freeze_time++ time1 = Time.zone.parse('2019-09-02')+ time2 = Time.zone.parse('2020-03-10 15:17')++ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(+ created_at: '2019-09-02',+ pinned_at: '2020-03-10 15:17'+ ))++ expect(topic.created_at).to be_within(1.second).of(time1)+ expect(topic.pinned_at).to be_within(1.second).of(time2)+ end+ endendend