From 179ecee11e28325b301096770ab819adf433c5f3 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 25 Jan 2018 14:39:55 -0500 Subject: [PATCH] FIX: featured links are 'undefined' in suggested topics --- app/serializers/suggested_topic_serializer.rb | 6 +++- .../suggested_topic_serializer_spec.rb | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 spec/serializers/suggested_topic_serializer_spec.rb diff --git a/app/serializers/suggested_topic_serializer.rb b/app/serializers/suggested_topic_serializer.rb index a0817917f5b..85eeea94996 100644 --- a/app/serializers/suggested_topic_serializer.rb +++ b/app/serializers/suggested_topic_serializer.rb @@ -7,7 +7,7 @@ class SuggestedTopicSerializer < ListableTopicSerializer has_one :user, serializer: BasicUserSerializer, embed: :objects end - attributes :archetype, :like_count, :views, :category_id, :tags, :featured_link + attributes :archetype, :like_count, :views, :category_id, :tags, :featured_link, :featured_link_root_domain has_many :posters, serializer: SuggestedPosterSerializer, embed: :objects def posters @@ -29,4 +29,8 @@ class SuggestedTopicSerializer < ListableTopicSerializer def featured_link object.featured_link end + + def include_featured_link_root_domain? + SiteSetting.topic_featured_link_enabled && object.featured_link + end end diff --git a/spec/serializers/suggested_topic_serializer_spec.rb b/spec/serializers/suggested_topic_serializer_spec.rb new file mode 100644 index 00000000000..b9fcdb6d52f --- /dev/null +++ b/spec/serializers/suggested_topic_serializer_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +describe SuggestedTopicSerializer do + let(:user) { Fabricate(:user) } + + describe '#featured_link and #featured_link_root_domain' do + let(:featured_link) { 'http://meta.discourse.org' } + let(:topic) { Fabricate(:topic, featured_link: featured_link, category: Fabricate(:category, topic_featured_link_allowed: true)) } + subject(:json) { described_class.new(topic, scope: Guardian.new(user), root: false).as_json } + + context 'when topic featured link is disable' do + before do + SiteSetting.topic_featured_link_enabled = true + topic + SiteSetting.topic_featured_link_enabled = false + end + + it 'should not return featured link attrs' do + expect(json[:featured_link]).to eq(nil) + expect(json[:featured_link_root_domain]).to eq(nil) + end + end + + context 'when topic featured link is enabled' do + before do + SiteSetting.topic_featured_link_enabled = true + end + + it 'should return featured link attrs' do + expect(json[:featured_link]).to eq(featured_link) + expect(json[:featured_link_root_domain]).to eq('discourse.org') + end + end + end +end