diff --git a/Gemfile b/Gemfile index 92d7a16..96ec8fd 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,8 @@ gem 'devise_token_auth' gem 'rack-cors' +gem 'active_model_serializers' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: %i[mri mingw x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 77022e1..8799818 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.8) + actionpack (>= 4.1, < 6) + activemodel (>= 4.1, < 6) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (5.2.1) activesupport (= 5.2.1) globalid (>= 0.3.6) @@ -49,6 +54,8 @@ GEM msgpack (~> 1.0) builder (3.2.3) byebug (10.0.2) + case_transform (0.2) + activesupport coderay (1.1.2) concurrent-ruby (1.1.3) coveralls (0.7.1) @@ -90,6 +97,7 @@ GEM concurrent-ruby (~> 1.0) jaro_winkler (1.5.1) json (2.1.0) + jsonapi-renderer (0.2.0) kramdown (1.17.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) @@ -246,6 +254,7 @@ PLATFORMS ruby DEPENDENCIES + active_model_serializers bootsnap (>= 1.1.0) byebug coveralls diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb new file mode 100644 index 0000000..a00c1c7 --- /dev/null +++ b/app/controllers/matches_controller.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class MatchesController < ApplicationController + # GET /matches/1 + def show + render json: Match.find(params[:id]), include: ['scores.score', 'scores.team'], status: status + end + + private + + def match_params + ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:state]) + end +end diff --git a/app/serializers/application_serializer.rb b/app/serializers/application_serializer.rb new file mode 100644 index 0000000..589d634 --- /dev/null +++ b/app/serializers/application_serializer.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class ApplicationSerializer < ActiveModel::Serializer +end diff --git a/app/serializers/match_serializer.rb b/app/serializers/match_serializer.rb new file mode 100644 index 0000000..d608775 --- /dev/null +++ b/app/serializers/match_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class MatchSerializer < ApplicationSerializer + attributes :state + + has_many :scores +end diff --git a/app/serializers/score_serializer.rb b/app/serializers/score_serializer.rb new file mode 100644 index 0000000..08ef59d --- /dev/null +++ b/app/serializers/score_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ScoreSerializer < ApplicationSerializer + attributes :score + + has_one :team +end diff --git a/app/serializers/team_serializer.rb b/app/serializers/team_serializer.rb new file mode 100644 index 0000000..cf33a86 --- /dev/null +++ b/app/serializers/team_serializer.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class TeamSerializer < ApplicationSerializer + attributes :name +end diff --git a/config/initializers/active_model_serializers.rb b/config/initializers/active_model_serializers.rb new file mode 100644 index 0000000..6f85768 --- /dev/null +++ b/config/initializers/active_model_serializers.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +ActiveModelSerializers.config.adapter = :json_api diff --git a/config/routes.rb b/config/routes.rb index bca9222..b10df31 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,6 @@ Rails.application.routes.draw do mount_devise_token_auth_for 'User', at: 'users' + + resources :matches, only: %i[show] end diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb new file mode 100644 index 0000000..47fc9fb --- /dev/null +++ b/spec/controllers/matches_controller_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe MatchesController, type: :controller do + let(:valid_attributes) do + skip('Add a hash of attributes valid for your model') + end + + let(:invalid_attributes) do + skip('Add a hash of attributes invalid for your model') + end + + let(:valid_session) { {} } + + before do + @match = create(:match) + @match.scores = create_pair(:score) + end + + describe 'GET #show' do + it 'should return success' do + get :show, params: { id: @match.to_param } + expect(response).to be_successful + expect(response.content_type).to eq('application/json') + end + + it 'should return the correct state' do + get :show, params: { id: @match.to_param } + body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body)) + expect(body[:state]).to be(@match.state) + expect(body[:score_ids]).to eq(@match.scores.map { |score| score.id.to_s }) + end + end +end diff --git a/spec/routing/matches_routing_spec.rb b/spec/routing/matches_routing_spec.rb new file mode 100644 index 0000000..f0f4806 --- /dev/null +++ b/spec/routing/matches_routing_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe MatchesController, type: :routing do + describe 'routing' do + it 'routes to #show' do + expect(get: '/matches/1').to route_to('matches#show', id: '1') + end + end +end