From 13d4b059638d84c78f348599e134d80b23a6a103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 5 Feb 2020 10:32:03 +0100 Subject: [PATCH] FIX: allow both meta_data and custom_fields when creating a topic For some reasons, we have two ways of associating "custom fields" to a new topic: using 'meta_data' and 'custom_fields'. However, if we were to provide both arguments, the 'meta_data' would be overwritten by any 'custom_fields' provided. This commit ensures we can use both and merges the 'custom_fields' with the 'meta_data'. --- lib/topic_creator.rb | 3 ++- spec/components/topic_creator_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 5f4aa7f16e9..654fc716f4b 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -35,8 +35,9 @@ class TopicCreator def create topic = Topic.new(setup_topic_params) setup_tags(topic) + if fields = @opts[:custom_fields] - topic.custom_fields = fields + topic.custom_fields.merge!(fields) end DiscourseEvent.trigger(:before_create_topic, topic, self) diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb index c475a08e92f..36a8717b9ad 100644 --- a/spec/components/topic_creator_spec.rb +++ b/spec/components/topic_creator_spec.rb @@ -36,6 +36,18 @@ describe TopicCreator do expect(TopicCreator.create(moderator, Guardian.new(moderator), valid_attrs)).to be_valid end + it "supports both meta_data and custom_fields" do + opts = valid_attrs.merge( + meta_data: { import_topic_id: "foo" }, + custom_fields: { import_id: "bar" } + ) + + topic = TopicCreator.create(admin, Guardian.new(admin), opts) + + expect(topic.custom_fields["import_topic_id"]).to eq("foo") + expect(topic.custom_fields["import_id"]).to eq("bar") + end + context 'regular user' do before { SiteSetting.min_trust_to_create_topic = TrustLevel[0] }