From f77f1dfa1c202add5d212a6fbbf1029ea4cf2c22 Mon Sep 17 00:00:00 2001 From: Tony Miller Date: Sat, 12 Oct 2024 11:14:11 -0700 Subject: [PATCH] send voting percentage to serializer --- app/controllers/api/shrimpos_controller.rb | 2 +- app/serializers/shrimpo_serializer.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/shrimpos_controller.rb b/app/controllers/api/shrimpos_controller.rb index 0605a686..6d5720c3 100644 --- a/app/controllers/api/shrimpos_controller.rb +++ b/app/controllers/api/shrimpos_controller.rb @@ -24,7 +24,7 @@ def create def show shrimpo = Shrimpo.friendly.find params[:id] - render json: shrimpo, include: ['shrimpo_entries', 'posts', 'shrimpo_entries.trophy_awards', 'shrimpo_voting_categories'] + render json: shrimpo, serializer: ShrimpoSerializer, current_user: current_user, include: ['shrimpo_entries', 'posts', 'shrimpo_entries.trophy_awards', 'shrimpo_voting_categories'] end private diff --git a/app/serializers/shrimpo_serializer.rb b/app/serializers/shrimpo_serializer.rb index fb9331f5..2fbfd0c6 100644 --- a/app/serializers/shrimpo_serializer.rb +++ b/app/serializers/shrimpo_serializer.rb @@ -1,9 +1,13 @@ class ShrimpoSerializer < ActiveModel::Serializer - attributes :title, :rule_pack, :start_at, :end_at, :status, :zip_file_url, :shrimpo_entries, :slug, :emoji, :cover_art_url, :ended_at, :duration, :username, :user_avatar, :entries_count, :entries_zip_file_url, :shrimpo_type + attributes :title, :rule_pack, :start_at, :end_at, :status, :zip_file_url, :shrimpo_entries, :slug, :emoji, :cover_art_url, :ended_at, :duration, :username, :user_avatar, :entries_count, :entries_zip_file_url, :shrimpo_type, :voting_completion_percentage has_many :shrimpo_entries, embed: :ids, key: :shrimpo_entries, embed_in_root: true, each_serializer: ShrimpoEntrySerializer has_many :posts, embed: :ids, key: :posts, embed_in_root: true, each_serializer: PostSerializer has_many :shrimpo_voting_categories, embed: :ids, key: :shrimpo_voting_categories, embed_in_root: true, each_serializer: ShrimpoVotingCategorySerializer + def voting_completion_percentage + object.voting_completion instance_options[:current_user] + end + def entries_count object.shrimpo_entries.count end