From dd2f66f49d9ac1054e1746767ed0dc970fe8811e Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 30 Nov 2018 15:08:08 +0100 Subject: [PATCH] Score is now MatchScore & Score Attribute is Points Attribute --- app/controllers/match_scores_controller.rb | 33 ++++++++++++++++++ app/controllers/scores_controller.rb | 33 ------------------ config/routes.rb | 2 +- ...pec.rb => match_scores_controller_spec.rb} | 34 +++++++++---------- spec/routing/match_scores_routing_spec.rb | 19 +++++++++++ spec/routing/scores_routing_spec.rb | 19 ----------- 6 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 app/controllers/match_scores_controller.rb delete mode 100644 app/controllers/scores_controller.rb rename spec/controllers/{scores_controller_spec.rb => match_scores_controller_spec.rb} (53%) create mode 100644 spec/routing/match_scores_routing_spec.rb delete mode 100644 spec/routing/scores_routing_spec.rb diff --git a/app/controllers/match_scores_controller.rb b/app/controllers/match_scores_controller.rb new file mode 100644 index 0000000..2f62bfc --- /dev/null +++ b/app/controllers/match_scores_controller.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class MatchScoresController < ApplicationController + before_action :set_match_score, only: %i[show update] + before_action :authenticate_user!, only: %i[update] + before_action -> { require_owner! @match_score.owner }, only: %i[update] + + # GET /scores/1 + def show + render json: @match_score + end + + # PATCH/PUT /scores/1 + def update + if @match_score.update(match_score_params) + render json: @match_score + else + render json: @match_score.errors, status: :unprocessable_entity + end + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_match_score + @match_score = MatchScore.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def match_score_params + deserialize_params only: %i[points] + end +end diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb deleted file mode 100644 index 08e1350..0000000 --- a/app/controllers/scores_controller.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -class ScoresController < ApplicationController - before_action :set_score, only: %i[show update] - before_action :authenticate_user!, only: %i[update] - before_action -> { require_owner! @score.owner }, only: %i[update] - - # GET /scores/1 - def show - render json: @score - end - - # PATCH/PUT /scores/1 - def update - if @score.update(score_params) - render json: @score - else - render json: @score.errors, status: :unprocessable_entity - end - end - - private - - # Use callbacks to share common setup or constraints between actions. - def set_score - @score = Score.find(params[:id]) - end - - # Only allow a trusted parameter "white list" through. - def score_params - deserialize_params only: %i[score] - end -end diff --git a/config/routes.rb b/config/routes.rb index f3f3498..e3097b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,5 +6,5 @@ Rails.application.routes.draw do resources :matches, only: %i[show] resources :teams, only: %i[show update] resources :tournaments - resources :scores, only: %i[show update] + resources :match_scores, only: %i[show update] end diff --git a/spec/controllers/scores_controller_spec.rb b/spec/controllers/match_scores_controller_spec.rb similarity index 53% rename from spec/controllers/scores_controller_spec.rb rename to spec/controllers/match_scores_controller_spec.rb index 0b13b2a..d76263b 100644 --- a/spec/controllers/scores_controller_spec.rb +++ b/spec/controllers/match_scores_controller_spec.rb @@ -2,24 +2,24 @@ require 'rails_helper' -RSpec.describe ScoresController, type: :controller do +RSpec.describe MatchScoresController, type: :controller do before do - @score = create(:score) - @owner = @score.owner + @match_score = create(:match_score) + @owner = @match_score.owner end describe 'GET #show' do it 'returns a success response' do - get :show, params: { id: @score.to_param } + get :show, params: { id: @match_score.to_param } expect(response).to be_successful end it 'should return the correct score' do - get :show, params: { id: @score.to_param } + get :show, params: { id: @match_score.to_param } body = deserialize_response response - expect(body[:score]).to eq(@score.score) - expect(body[:team_id]).to eq(@score.team.id.to_s) - expect(body[:match_id]).to eq(@score.match.id.to_s) + expect(body[:points]).to eq(@match_score.points) + expect(body[:team_id]).to eq(@match_score.team.id.to_s) + expect(body[:match_id]).to eq(@match_score.match.id.to_s) end end @@ -27,10 +27,10 @@ RSpec.describe ScoresController, type: :controller do let(:valid_update) do { data: { - id: @score.id, - type: 'scores', + id: @match_score.id, + type: 'match_scores', attributes: { - score: 42 + points: 42 } } } @@ -43,16 +43,16 @@ RSpec.describe ScoresController, type: :controller do end it 'updates the requested score' do - put :update, params: { id: @score.to_param }.merge(valid_update) - @score.reload - expect(@score.score).to eq(valid_update[:data][:attributes][:score]) + put :update, params: { id: @match_score.to_param }.merge(valid_update) + @match_score.reload + expect(@match_score.points).to eq(valid_update[:data][:attributes][:points]) end it 'renders a response with the updated team' do - put :update, params: { id: @score.to_param }.merge(valid_update) + put :update, params: { id: @match_score.to_param }.merge(valid_update) expect(response).to be_successful body = deserialize_response response - expect(body[:score]).to eq(valid_update[:data][:attributes][:score]) + expect(body[:points]).to eq(valid_update[:data][:attributes][:points]) end end @@ -62,7 +62,7 @@ RSpec.describe ScoresController, type: :controller do end it 'renders a forbidden error response' do - put :update, params: { id: @score.to_param }.merge(valid_update) + put :update, params: { id: @match_score.to_param }.merge(valid_update) expect(response).to have_http_status(:forbidden) end end diff --git a/spec/routing/match_scores_routing_spec.rb b/spec/routing/match_scores_routing_spec.rb new file mode 100644 index 0000000..96ca543 --- /dev/null +++ b/spec/routing/match_scores_routing_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe MatchScoresController, type: :routing do + describe 'routing' do + it 'routes to #show' do + expect(get: '/match_scores/1').to route_to('match_scores#show', id: '1') + end + + it 'routes to #update via PUT' do + expect(put: '/match_scores/1').to route_to('match_scores#update', id: '1') + end + + it 'routes to #update via PATCH' do + expect(patch: '/match_scores/1').to route_to('match_scores#update', id: '1') + end + end +end diff --git a/spec/routing/scores_routing_spec.rb b/spec/routing/scores_routing_spec.rb deleted file mode 100644 index 679f0c9..0000000 --- a/spec/routing/scores_routing_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe ScoresController, type: :routing do - describe 'routing' do - it 'routes to #show' do - expect(get: '/scores/1').to route_to('scores#show', id: '1') - end - - it 'routes to #update via PUT' do - expect(put: '/scores/1').to route_to('scores#update', id: '1') - end - - it 'routes to #update via PATCH' do - expect(patch: '/scores/1').to route_to('scores#update', id: '1') - end - end -end