diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index 4abdc36f09f..228a52311b4 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -4,7 +4,7 @@ class TopicList include ActiveModel::Serialization cattr_accessor :preloaded_custom_fields - self.preloaded_custom_fields = [] + self.preloaded_custom_fields = Set.new attr_accessor :more_topics_url, :prev_topics_url, @@ -20,6 +20,8 @@ class TopicList @current_user = current_user @topics_input = topics @opts = opts || {} + + preloaded_custom_fields << DiscourseTagging::TAGS_FIELD_NAME if SiteSetting.tagging_enabled end def preload_key @@ -81,11 +83,8 @@ class TopicList ft.topic_list = self end - preload_custom_fields = TopicList.preloaded_custom_fields - preload_custom_fields << DiscourseTagging::TAGS_FIELD_NAME if SiteSetting.tagging_enabled - - if preload_custom_fields.present? - Topic.preload_custom_fields(@topics, preload_custom_fields) + if preloaded_custom_fields.present? + Topic.preload_custom_fields(@topics, preloaded_custom_fields) end @topics diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 276ee765677..fcb0bb1f8a6 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -232,7 +232,7 @@ describe TopicQuery do # returns the topics in reverse posters order if requested" do expect(ids_in_order('posters', false)).to eq([archived_topic, closed_topic, invisible_topic, future_topic, regular_topic, pinned_topic].map(&:id)) - + # sets a custom field for each topic to emulate a plugin regular_topic.custom_fields["sheep"] = 26 pinned_topic.custom_fields["sheep"] = 47 @@ -240,7 +240,7 @@ describe TopicQuery do invisible_topic.custom_fields["sheep"] = 12 closed_topic.custom_fields["sheep"] = 31 future_topic.custom_fields["sheep"] = 53 - + regular_topic.save pinned_topic.save archived_topic.save @@ -257,7 +257,7 @@ describe TopicQuery do # returns the topics in reverse sheep order if requested" do expect(ids_in_order('sheep', false)).to eq([invisible_topic, regular_topic, closed_topic, pinned_topic, future_topic, archived_topic].map(&:id)) - + end diff --git a/spec/models/topic_list_spec.rb b/spec/models/topic_list_spec.rb new file mode 100644 index 00000000000..ded7e9231a7 --- /dev/null +++ b/spec/models/topic_list_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +describe TopicList do + let!(:topic) { Fabricate(:topic) } + let(:user) { topic.user } + let(:topic_list) { TopicList.new("liked", user, [topic]) } + + after do + TopicList.preloaded_custom_fields.clear + end + + describe ".preloaded_custom_fields" do + it "should return a unique set of values" do + TopicList.preloaded_custom_fields << "test" + TopicList.preloaded_custom_fields << "test" + TopicList.preloaded_custom_fields << "apple" + + expect(TopicList.preloaded_custom_fields).to eq(Set.new(%w{test apple})) + end + end + + context "DiscourseTagging enabled" do + before do + SiteSetting.tagging_enabled = true + end + + after do + SiteSetting.tagging_enabled = false + end + + it "should add tags to preloaded custom fields" do + expect(topic_list.preloaded_custom_fields).to eq(Set.new([DiscourseTagging::TAGS_FIELD_NAME])) + end + end +end