このコミットが含まれているのは:
@@ -8,7 +8,7 @@ class GekanatorGamesController < ApplicationController
|
||||
user: current_user,
|
||||
guessed_post_id: params.require(:guessed_post_id),
|
||||
correct_post_id: params[:correct_post_id].presence,
|
||||
won: bool?(:won),
|
||||
won: params[:guessed_post_id].to_i == params[:correct_post_id].to_i,
|
||||
question_count: params.require(:question_count),
|
||||
answers:)
|
||||
|
||||
|
||||
@@ -4,15 +4,7 @@ class GekanatorGame < ApplicationRecord
|
||||
belongs_to :correct_post, class_name: 'Post', optional: true
|
||||
|
||||
validates :answers, presence: true
|
||||
validates :correct_post, presence: true
|
||||
validates :question_count, numericality: { greater_than_or_equal_to: 0 }
|
||||
validates :won, inclusion: { in: [true, false] }
|
||||
validate :correct_post_required_when_lost
|
||||
|
||||
private
|
||||
|
||||
def correct_post_required_when_lost
|
||||
return if won || correct_post_id.present?
|
||||
|
||||
errors.add(:correct_post_id, '外れた時は正解の投稿を指定してくださぃ.')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ RSpec.describe 'Gekanator games API', type: :request do
|
||||
|
||||
post '/gekanator/games', params: {
|
||||
guessed_post_id: guessed_post.id,
|
||||
won: true,
|
||||
correct_post_id: guessed_post.id,
|
||||
question_count: 3,
|
||||
answers: [{ question_id: 'tag:1', answer: 'yes' }] }
|
||||
|
||||
@@ -19,7 +19,7 @@ RSpec.describe 'Gekanator games API', type: :request do
|
||||
game = GekanatorGame.find(json['id'])
|
||||
expect(game.user).to eq(user)
|
||||
expect(game.guessed_post).to eq(guessed_post)
|
||||
expect(game.correct_post).to be_nil
|
||||
expect(game.correct_post).to eq(guessed_post)
|
||||
expect(game.won).to eq(true)
|
||||
expect(game.answers).to eq([{ 'question_id' => 'tag:1', 'answer' => 'yes' }])
|
||||
end
|
||||
@@ -30,20 +30,20 @@ RSpec.describe 'Gekanator games API', type: :request do
|
||||
post '/gekanator/games', params: {
|
||||
guessed_post_id: guessed_post.id,
|
||||
correct_post_id: correct_post.id,
|
||||
won: false,
|
||||
question_count: 4,
|
||||
answers: [{ question_id: 'tag:1', answer: 'no' }] }
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(GekanatorGame.find(json['id']).correct_post).to eq(correct_post)
|
||||
game = GekanatorGame.find(json['id'])
|
||||
expect(game.correct_post).to eq(correct_post)
|
||||
expect(game.won).to eq(false)
|
||||
end
|
||||
|
||||
it 'rejects a lost game without the correct post' do
|
||||
it 'rejects a game without the correct post' do
|
||||
sign_in_as user
|
||||
|
||||
post '/gekanator/games', params: {
|
||||
guessed_post_id: guessed_post.id,
|
||||
won: false,
|
||||
question_count: 4,
|
||||
answers: [{ question_id: 'tag:1', answer: 'no' }] }
|
||||
|
||||
@@ -53,7 +53,7 @@ RSpec.describe 'Gekanator games API', type: :request do
|
||||
it 'requires a user' do
|
||||
post '/gekanator/games', params: {
|
||||
guessed_post_id: guessed_post.id,
|
||||
won: true,
|
||||
correct_post_id: guessed_post.id,
|
||||
question_count: 1,
|
||||
answers: [] }
|
||||
|
||||
|
||||
新しい課題から参照
ユーザをブロックする