From 75e4f7f896a68b564cbdd051bb6d4ea92aa3e2f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 12 Aug 2017 04:18:04 +0200 Subject: [PATCH] Shorten some very long lines Prevent warnings from already defined constants when reloading --- app/serializers/post_serializer.rb | 21 ++++++++----------- .../post_stream_serializer_mixin.rb | 18 +++++++--------- app/serializers/topic_view_serializer.rb | 4 +--- lib/plugin/metadata.rb | 2 +- lib/topic_view.rb | 10 ++++----- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb index ea2eec14b6d..d2e7fc7e863 100644 --- a/app/serializers/post_serializer.rb +++ b/app/serializers/post_serializer.rb @@ -1,7 +1,7 @@ class PostSerializer < BasicPostSerializer # To pass in additional information we might need - INSTANCE_VARS = [ + INSTANCE_VARS ||= [ :topic_view, :parent_post, :add_raw, @@ -310,9 +310,7 @@ class PostSerializer < BasicPostSerializer end def include_user_custom_fields? - return if @topic_view.blank? - custom_fields = @topic_view.user_custom_fields - custom_fields && custom_fields[object.user_id] + (@topic_view&.user_custom_fields || {})[object.user_id] end def static_doc @@ -354,20 +352,19 @@ class PostSerializer < BasicPostSerializer private def post_actions - @post_actions ||= (@topic_view.present? && @topic_view.all_post_actions.present?) ? @topic_view.all_post_actions[object.id] : nil + @post_actions ||= (@topic_view&.all_post_actions || {})[object.id] end def active_flags - @active_flags ||= (@topic_view.present? && @topic_view.all_active_flags.present?) ? @topic_view.all_active_flags[object.id] : nil + @active_flags ||= (@topic_view&.all_active_flags || {})[object.id] end def post_custom_fields - @post_custom_fields ||= - if @topic_view - (@topic_view.post_custom_fields && @topic_view.post_custom_fields[object.id]) || {} - else - object.custom_fields - end + @post_custom_fields ||= if @topic_view + (@topic_view.post_custom_fields || {})[object.id] || {} + else + object.custom_fields + end end end diff --git a/app/serializers/post_stream_serializer_mixin.rb b/app/serializers/post_stream_serializer_mixin.rb index d87f2feaa6c..26213f059c8 100644 --- a/app/serializers/post_stream_serializer_mixin.rb +++ b/app/serializers/post_stream_serializer_mixin.rb @@ -20,19 +20,17 @@ module PostStreamSerializerMixin end def posts - return @posts if @posts.present? - @posts = [] - if object.posts - object.posts.each do |p| - ps = PostSerializer.new(p, scope: scope, root: false) - ps.add_raw = true if @options[:include_raw] - ps.topic_view = object - p.topic = object.topic + @posts ||= begin + (object.posts || []).map do |post| + post.topic = object.topic - @posts << ps.as_json + serializer = PostSerializer.new(post, scope: scope, root: false) + serializer.add_raw = true if @options[:include_raw] + serializer.topic_view = object + + serializer.as_json end end - @posts end end diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb index 49be96b8c7e..228a19c8352 100644 --- a/app/serializers/topic_view_serializer.rb +++ b/app/serializers/topic_view_serializer.rb @@ -255,9 +255,7 @@ class TopicViewSerializer < ApplicationSerializer end def topic_timer - TopicTimerSerializer.new( - object.topic.public_topic_timer, root: false - ) + TopicTimerSerializer.new(object.topic.public_topic_timer, root: false) end def tags diff --git a/lib/plugin/metadata.rb b/lib/plugin/metadata.rb index 123fccc396b..134f82d1701 100644 --- a/lib/plugin/metadata.rb +++ b/lib/plugin/metadata.rb @@ -3,7 +3,7 @@ module Plugin; end class Plugin::Metadata - OFFICIAL_PLUGINS = Set.new([ + OFFICIAL_PLUGINS ||= Set.new([ "customer-flair", "discourse-adplugin", "discourse-akismet", diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 3095127049c..9a268f96682 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -67,15 +67,13 @@ class TopicView filter_posts(options) if @posts - added_fields = User.whitelisted_user_custom_fields(@guardian) - if added_fields.present? + if (added_fields = User.whitelisted_user_custom_fields(@guardian)).present? @user_custom_fields = User.custom_fields_for_ids(@posts.map(&:user_id), added_fields) end - end - whitelisted_fields = TopicView.whitelisted_post_custom_fields(@user) - if whitelisted_fields.present? && @posts - @post_custom_fields = Post.custom_fields_for_ids(@posts.map(&:id), whitelisted_fields) + if (whitelisted_fields = TopicView.whitelisted_post_custom_fields(@user)).present? + @post_custom_fields = Post.custom_fields_for_ids(@posts.map(&:id), whitelisted_fields) + end end @draft_key = @topic.draft_key