Merge pull request #66 from turniere/ticket/TURNIERE-256
Include bets in match serializer
This commit is contained in:
commit
e8207c95c1
|
|
@ -6,12 +6,7 @@ class BetsController < ApplicationController
|
||||||
rescue_from UserServiceError, with: :handle_user_service_error
|
rescue_from UserServiceError, with: :handle_user_service_error
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: @match.bets.group_by(&:team).map { |team, bets|
|
render json: @match.bets, serializer: BetsSerializer
|
||||||
{
|
|
||||||
team: ActiveModelSerializers::SerializableResource.new(team).as_json,
|
|
||||||
bets: bets.size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class MatchesController < ApplicationController
|
||||||
|
|
||||||
# GET /matches/1
|
# GET /matches/1
|
||||||
def show
|
def show
|
||||||
render json: @match, include: ['match_scores.points', 'match_scores.team']
|
render json: @match, include: ['match_scores.points', 'match_scores.team', 'bets']
|
||||||
end
|
end
|
||||||
|
|
||||||
# PATCH/PUT /matches/1
|
# PATCH/PUT /matches/1
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -7,5 +7,10 @@ class MatchSerializer < ApplicationSerializer
|
||||||
ActiveModelSerializers::SerializableResource.new(object.winner).as_json
|
ActiveModelSerializers::SerializableResource.new(object.winner).as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
has_many :match_scores
|
def bets
|
||||||
|
ActiveModelSerializers::SerializableResource.new(object.bets, serializer: BetsSerializer).as_json
|
||||||
|
end
|
||||||
|
|
||||||
|
has_many :match_scores
|
||||||
|
has_many :bets
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue