Skip to content

Commit

Permalink
Update games serializer and add slug validation with default generati…
Browse files Browse the repository at this point in the history
…on (#575)

* lint

* Update games serializer and add slug validation with default generation

* Fix slug generation to handle nil names in game model

* bundler updoot

* lint

* fix test

* unregisters
  • Loading branch information
thatguyinabeanie authored Dec 9, 2024
1 parent 4e647df commit 9d367ac
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 68 deletions.
40 changes: 20 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ GEM
hashdiff (1.1.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
io-console (0.8.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.8.2)
json (2.9.0)
json-schema (5.1.1)
addressable (~> 2.8)
bigdecimal (~> 3.1)
Expand Down Expand Up @@ -193,7 +193,7 @@ GEM
net-smtp
marcel (1.0.4)
mini_mime (1.1.5)
minitest (5.25.2)
minitest (5.25.4)
msgpack (1.7.5)
net-imap (0.5.1)
date
Expand All @@ -205,17 +205,17 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.4)
nokogiri (1.16.7-aarch64-linux)
nokogiri (1.17.0-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm-linux)
nokogiri (1.17.0-arm-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.17.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86-linux)
nokogiri (1.17.0-x86-linux)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
nokogiri (1.17.0-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
nokogiri (1.17.0-x86_64-linux)
racc (~> 1.4)
openssl (3.2.0)
ostruct (0.6.1)
Expand Down Expand Up @@ -274,9 +274,9 @@ GEM
activesupport (>= 4.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
Expand All @@ -291,7 +291,7 @@ GEM
rb-inotify (0.11.1)
ffi (~> 1.0)
rb-readline (0.5.5)
rbs (3.6.1)
rbs (3.7.0)
logger
rdoc (6.8.1)
psych (>= 4.0.0)
Expand Down Expand Up @@ -339,7 +339,7 @@ GEM
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rspec-support (3.13.2)
rspec_junit_formatter (0.6.0)
rspec-core (>= 2, < 4, != 2.12.0)
rswag-api (2.16.0)
Expand All @@ -353,21 +353,21 @@ GEM
rswag-ui (2.16.0)
actionpack (>= 5.2, < 8.1)
railties (>= 5.2, < 8.1)
rubocop (1.69.0)
rubocop (1.69.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.36.1, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.36.2)
parser (>= 3.3.1.0)
rubocop-checkstyle_formatter (0.6.0)
rubocop (>= 1.14.0)
rubocop-erb (0.5.4)
rubocop-erb (0.5.5)
better_html
rubocop (~> 1.45)
rubocop-factory_bot (2.26.1)
Expand Down Expand Up @@ -463,7 +463,7 @@ GEM
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
smart_properties (1.17.0)
sorbet-runtime (0.5.11670)
sorbet-runtime (0.5.11690)
spring (4.2.1)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
Expand All @@ -475,7 +475,7 @@ GEM
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.2)
svix (1.42.0)
svix (1.43.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.2)
Expand All @@ -492,7 +492,7 @@ GEM
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
uri (1.0.2)
useragent (0.16.10)
useragent (0.16.11)
versionist (2.0.1)
activesupport (>= 3)
railties (>= 3)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Api
module V1
class GamesController < AbstractApplicationController
self.klass = ::Game
self.serializer_klass = Serializers::Game
self.serializer_klass = Serializers::GameDetails
self.detail_serializer_klass = Serializers::GameDetails
self.enable_pagination = true
protected
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/api/v1/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def list_tournaments
def post_tournament
authorize @organization, :create_tournament?

@tournament = @organization.tournaments.new tournaments_permitted_params

@tournament = @organization.tournaments.new tournaments_permitted_params.except(:id, :format, :organization)
if @tournament.save
render json: @tournament, status: :created, serializer: Serializers::TournamentDetails
else
Expand All @@ -78,13 +77,12 @@ def set_organization
end

def permitted_params
params.require(:organization).permit(:name, :description, :owner_id, :format, :logo_url, :partner, :hidden)
params.require(:organization).permit(:name, :description, :owner_id, :logo_url, :partner, :hidden)
end

def tournaments_permitted_params
params.require(:tournament).permit(
params.permit(
:tournament_id,
:format,
:name,
:start_at, :end_at,
:game_id, :format_id,
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/tournaments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ def show

def create
authorize @organization, :create_tournament?

@tournament = ::Tournament.new permitted_params
if @tournament.save

render json: serialize_details, status: :created
else

render json: @tournament.errors, status: :unprocessable_entity
end
rescue ActionController::ParameterMissing => e
Expand Down Expand Up @@ -96,10 +99,12 @@ def set_tournaments

def set_organization
@organization = if permitted_params[:organization_id].present?

::Organization.find(permitted_params[:organization_id])
else
::Tournament.find(params[:id]).organization
end

@organization
rescue ActiveRecord::RecordNotFound
render json: { error: "Organization not found" }, status: :not_found
Expand Down
7 changes: 7 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ def self.policy_class
GamePolicy
end
validates :name, presence: true
validates :slug, presence: true, uniqueness: true
has_many :formats, class_name: "Format", dependent: :nullify
has_many :tournaments, class_name: "Tournament", dependent: :nullify

before_validation :set_defaults, on: :create

def set_defaults
self.slug = name&.parameterize if slug.blank? && name.present?
end
end
7 changes: 3 additions & 4 deletions app/models/tournament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.policy_class
validate :check_in_start_at_before_start_at, if: -> { check_in_start_at.present? && start_at.present? }

has_many :players, class_name: "Player", dependent: :destroy_async
validates :player_cap, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
validates :player_cap, numericality: { only_integer: true, greater_than: 3 }, allow_nil: true
validates :limitless_id, uniqueness: true, allow_nil: true

before_validation :set_defaults, on: :create
Expand Down Expand Up @@ -116,11 +116,10 @@ def started?
private

def set_defaults
self.name ||= "#{organization.name}'s Tournament ##{organization.tournaments.count + 1}" if organization.present?

self.format ||= game.formats.last if game.present?
self.name ||= "#{organization.name}'s Tournament ##{organization.tournaments.count + 1}" if organization.present?

return if start_at.blank?
return if self.start_at.blank?

self.late_registration ||= true

Expand Down
2 changes: 1 addition & 1 deletion app/policies/organization_policy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class OrganizationPolicy < ApplicationPolicy
def owner?
record.owner == account
record&.owner&.id == account.id
end

def admin?
Expand Down
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ words:
- deepcode
- logpanel
- jacoco
- unregisters
- sqltools
- sonarlint
ignoreWords: []
Expand Down
44 changes: 44 additions & 0 deletions db/migrate/20241209035641_insert_values_into_games_and_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class InsertValuesIntoGamesAndFormat < ActiveRecord::Migration[7.2]
def change
add_column :games, :slug, :string, null: false
add_index :games, :slug, unique: true

sv = Game.create(name: "Scarlet & Violet", slug: "sv")
swsh = Game.create(name: "Sword & Shield", slug: "swsh")
lgpe = Game.create(name: "Let's Go Pikachu & Eevee", slug: "lgpe")
bdsp = Game.create(name: "Brilliand Diamond & Shining Pearl", slug: "bdsp")
pogo = Game.create(name: "Pokemon Go", slug: "pogo")

Format.create(name: "Regulation A", game: sv)
Format.create(name: "Regulation B", game: sv)
Format.create(name: "Regulation C", game: sv)
Format.create(name: "Regulation D", game: sv)
Format.create(name: "Regulation E", game: sv)
Format.create(name: "Regulation F", game: sv)
Format.create(name: "Regulation G", game: sv)
Format.create(name: "Regulation H", game: sv)
Format.create(name: "Regulation I", game: sv)
Format.create(name: "Regulation J", game: sv)

Format.create(name: "Series 1", game: swsh)
Format.create(name: "Series 2", game: swsh)
Format.create(name: "Series 3", game: swsh)
Format.create(name: "Series 4", game: swsh)
Format.create(name: "Series 5", game: swsh)
Format.create(name: "Series 6", game: swsh)
Format.create(name: "Series 7", game: swsh)
Format.create(name: "Series 8", game: swsh)
Format.create(name: "Series 9", game: swsh)
Format.create(name: "Series 10", game: swsh)
Format.create(name: "Series 11", game: swsh)
Format.create(name: "Series 12", game: swsh)
Format.create(name: "Series 13", game: swsh)

Format.create(name: "National Dex", game: lgpe)
Format.create(name: "National Dex", game: bdsp)

Format.create(name: "Great League", game: pogo)
Format.create(name: "Ultra League", game: pogo)
Format.create(name: "Master League", game: pogo)
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def create_format(name:, game:)
Format.find_or_create_by!(name:, game:)
end

game = Game.find_or_create_by!(name: "Pokemon VGC")
game = Game.find_or_create_by!(name: "Scarlet & Violet", slug: "sv")

format = Format.find_or_create_by!(name: "Regulation H", game: game)

Expand Down
17 changes: 3 additions & 14 deletions openapi/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,13 @@ paths:
application/json:
schema:
"$ref": "#/components/schemas/TournamentDetails"
'400':
description: bad request
'422':
description: unprocessable_entity
requestBody:
content:
application/json:
schema:
"$ref": "#/components/schemas/TournamentDetails"
"$ref": "#/components/schemas/TournamentPostRequest"
"/tournaments/{tournament_id}/phases":
parameters:
- name: tournament_id
Expand Down Expand Up @@ -2320,10 +2320,6 @@ components:
type: string
format: date-time
nullable: true
registration_end_at:
type: string
format: date-time
nullable: true
late_registration:
type: boolean
check_in_start_at:
Expand Down Expand Up @@ -2351,9 +2347,6 @@ components:
type: object
title: Tournament Post Request
properties:
organization_id:
type: integer
format: int64
name:
type: string
game_id:
Expand All @@ -2374,10 +2367,6 @@ components:
type: string
format: date-time
nullable: true
registration_end_at:
type: string
format: date-time
nullable: true
late_registration:
type: boolean
check_in_start_at:
Expand Down
2 changes: 1 addition & 1 deletion spec/models/tournament_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
it { is_expected.to validate_presence_of(:organization) }
it { is_expected.to validate_presence_of(:game) }

it { is_expected.to validate_numericality_of(:player_cap).only_integer.is_greater_than(0).allow_nil }
it { is_expected.to validate_numericality_of(:player_cap).only_integer.is_greater_than(3).allow_nil }

context "when game is present" do
before { tournament.game = game }
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/games_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
parameter name: :id, in: :path, type: :integer, description: "ID of the game", required: true
parameter VERCEL_TOKEN_HEADER_PARAMETER

let(:test_game) { create(:game, name: "Test Game") }
let(:test_game) { create(:game, name: "Test Game", slug: "tg") }
let(:id) { test_game.id }

get("Show Game") do
Expand Down
Loading

0 comments on commit 9d367ac

Please sign in to comment.