docbrown/app/serializers/homepage/article_serializer.rb
Joshua Wehner 8a45a4bf67
Enable multiple reactions on #index (including front-end render) (#19169)
* Revert "(Temporarily) Remove multiple reactions from #index (#19142)"

This reverts commit a45d300639.

* Clean up reaction category count mechanism for #index

* Remove, unnecessary

* Expose reaction categories in JSON response

* Update test with new categories

* Fix flaky sample / minimum floor

* buildArticle (infinite scroll) with multiple reactions

* If we're doing #index front-end, we aren't feature-flag'd

* nbsp

* react (home feed) has multiple_reactions

* Update app/assets/javascripts/utilities/buildArticleHTML.js

Co-authored-by: Rajat Talesra <rajat@forem.com>

* Clean up Reactable#reaction_categories

* Use 'multiple_reactions_icons_container'

* Try adding a ReactionCount test

* Adapt memory fix from 76dd53d

* Try adding categories to fixture

* Attempt to eager-load distinct public categories

* Setting dependent to the default for rubocop

* Try making image assets more public?

* Revert "Fix flaky sample / minimum floor"

---------

Co-authored-by: Rajat Talesra <rajat@forem.com>
Co-authored-by: Mac Siri <mac@forem.com>
2023-03-03 15:17:19 +01:00

59 lines
1.8 KiB
Ruby

module Homepage
class ArticleSerializer < ApplicationSerializer
# @param relation [ActiveRecord::Relation<Article>]
#
# @return [Hash]
def self.serialized_collection_from(relation:)
# Unfortunately the FlareTag class sends one SQL query per each article,
# as we want to optimize by loading them in one query, we're using a different class
tag_flares = Homepage::FetchTagFlares.call(relation)
# including user and organization as the last step as they are not needed
# by the query that fetches tag flares, they are only needed by the serializer
relation = relation.includes(:user, :organization)
new(relation, params: { tag_flares: tag_flares }, is_collection: true)
.serializable_hash[:data]
.pluck(:attributes)
end
attributes(
:class_name,
:cloudinary_video_url,
:comments_count,
:id,
:path,
:public_reactions_count,
:readable_publish_date,
:reading_time,
:title,
:user_id,
:public_reaction_categories,
)
attribute :video_duration_string, &:video_duration_in_minutes
attribute :published_at_int, ->(article) { article.published_at.to_i }
attribute :tag_list, ->(article) { article.cached_tag_list.to_s.split(", ") }
attribute :flare_tag, ->(article, params) { params.dig(:tag_flares, article.id) }
attribute :user do |article|
user = article.user
{
name: user.name,
profile_image_90: user.profile_image_90,
username: user.username
}
end
attribute :organization, if: proc { |a| a.organization.present? } do |article|
organization = article.organization
{
name: organization.name,
profile_image_90: article.organization.profile_image_90,
slug: organization.slug
}
end
end
end