diff --git a/app/controllers/bets_controller.rb b/app/controllers/bets_controller.rb index cc6e250..3a63c3e 100644 --- a/app/controllers/bets_controller.rb +++ b/app/controllers/bets_controller.rb @@ -6,12 +6,7 @@ class BetsController < ApplicationController rescue_from UserServiceError, with: :handle_user_service_error def index - render json: @match.bets.group_by(&:team).map { |team, bets| - { - team: ActiveModelSerializers::SerializableResource.new(team).as_json, - bets: bets.size - } - } + render json: @match.bets, serializer: BetsSerializer end def create diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index ca5275d..778fca9 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -7,7 +7,7 @@ class MatchesController < ApplicationController # GET /matches/1 def show - render json: @match, include: ['match_scores.points', 'match_scores.team'] + render json: @match, include: ['match_scores.points', 'match_scores.team', 'bets'] end # PATCH/PUT /matches/1 diff --git a/app/serializers/bets_serializer.rb b/app/serializers/bets_serializer.rb new file mode 100644 index 0000000..a456976 --- /dev/null +++ b/app/serializers/bets_serializer.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class BetsSerializer < ActiveModel::Serializer::CollectionSerializer + def serializable_hash(_adapter_options, _options, _adapter_instance) + @object.group_by(&:team).map do |team, bets| + { + team: ActiveModelSerializers::SerializableResource.new(team).as_json, + bets: bets.size + } + end + end +end diff --git a/app/serializers/match_serializer.rb b/app/serializers/match_serializer.rb index 28b7d1a..6b8d21f 100644 --- a/app/serializers/match_serializer.rb +++ b/app/serializers/match_serializer.rb @@ -7,5 +7,10 @@ class MatchSerializer < ApplicationSerializer ActiveModelSerializers::SerializableResource.new(object.winner).as_json end + def bets + ActiveModelSerializers::SerializableResource.new(object.bets, serializer: BetsSerializer).as_json + end + has_many :match_scores + has_many :bets end