discourse/app/serializers/suggested_topics_mixin.rb
Ted Johansson 25a226279a
DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)
The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method.

There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
2023-02-13 12:39:45 +08:00

52 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module SuggestedTopicsMixin
def self.included(klass)
klass.attributes :related_messages
klass.attributes :suggested_topics
klass.attributes :suggested_group_name
end
def include_related_messages?
return false if @options[:exclude_suggested_and_related]
object.next_page.nil? && object.related_messages&.topics
end
def include_suggested_topics?
return false if @options[:exclude_suggested_and_related]
object.next_page.nil? && object.suggested_topics&.topics
end
def include_suggested_group_name?
return false unless include_suggested_topics?
object.topic.private_message? && scope.user
end
def suggested_group_name
return if object.topic.topic_allowed_users.exists?(user_id: scope.user.id)
if object.topic_allowed_group_ids.present?
Group
.joins(:group_users)
.where(
"group_users.group_id IN (?) AND group_users.user_id = ?",
object.topic_allowed_group_ids,
scope.user.id,
)
.pick(:name)
end
end
def related_messages
object.related_messages.topics.map do |t|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
end
end
def suggested_topics
object.suggested_topics.topics.map do |t|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
end
end
end